Module: Walrat::ParsletCombining

Included in:
Proc, Regexp, String, Symbol, Parslet, ParsletCombination, Predicate
Defined in:
lib/walrat/parslet_combining.rb

Overview

The ParsletCombining module, together with the ParsletCombination class and its subclasses, provides simple container classes for encapsulating relationships among Parslets. By storing this information outside of the Parslet objects themselves their design is kept clean and they can become immutable objects which are much more easily copied and shared among multiple rules in a Grammar.

Instance Method Summary collapse

Instance Method Details

#&(next_parslet) ⇒ Object

Shorthand for ParsletCombining.sequence(first, second).



51
52
53
# File 'lib/walrat/parslet_combining.rb', line 51

def &(next_parslet)
  self.sequence self, next_parslet
end

#>>(next_parslet) ⇒ Object

Shorthand for ParsletCombining.sequence(first, second)



69
70
71
# File 'lib/walrat/parslet_combining.rb', line 69

def >>(next_parslet)
  self.merge self, next_parslet
end

#and?Boolean

Shorthand for and_predicate Strictly speaking, this shorthand breaks with established Ruby practice that “?” at the end of a method name should indicate a method that returns true or false.

Returns:

  • (Boolean)


152
153
154
# File 'lib/walrat/parslet_combining.rb', line 152

def and?
  self.and_predicate self
end

#and_predicate(parslet) ⇒ Object

Parsing Expression Grammar support. Succeeds if parslet succeeds but consumes no input (throws an :AndPredicateSuccess symbol).



144
145
146
# File 'lib/walrat/parslet_combining.rb', line 144

def and_predicate parslet
  Walrat::AndPredicate.new parslet.to_parseable
end

#choice(left, right, *others) ⇒ Object

Defines a choice of Parslets (or ParsletCombinations). Returns a ParsletChoice instance.



75
76
77
78
# File 'lib/walrat/parslet_combining.rb', line 75

def choice left, right, *others
  Walrat::ParsletChoice.new left.to_parseable,
    right.to_parseable, *others
end

#memoizing_parse(string, options = {}) ⇒ Object

Convenience method.



34
35
36
# File 'lib/walrat/parslet_combining.rb', line 34

def memoizing_parse string, options = {}
  self.to_parseable.memoizing_parse string, options
end

#merge(first, second, *others) ⇒ Object

Defines a sequence of Parslets similar to the sequence method but with the difference that the contents of array results from the component parslets will be merged into a single array rather than being added as arrays. To illustrate:

'foo' & 'bar'.one_or_more   # returns results like ['foo', ['bar', 'bar', 'bar']]
'foo' >> 'bar'.one_or_more  # returns results like ['foo', 'bar', 'bar', 'bar']


63
64
65
66
# File 'lib/walrat/parslet_combining.rb', line 63

def merge first, second, *others
  Walrat::ParsletMerge.new first.to_parseable,
    second.to_parseable, *others
end

#not!Object

Shorthand for not_predicate. Strictly speaking, this shorthand breaks with established Ruby practice that “!” at the end of a method name should indicate a destructive behaviour on (mutation of) the receiver.



172
173
174
# File 'lib/walrat/parslet_combining.rb', line 172

def not!
  self.not_predicate self
end

#not_predicate(parslet) ⇒ Object

Parsing Expression Grammar support. Succeeds if parslet fails (throws a :NotPredicateSuccess symbol). Fails if parslet succeeds (raise a ParseError). Consumes no output. This method will almost invariably be used in conjuntion with the & operator, like this:

rule :foo, :p1 & :p2.not_predicate
rule :foo, :p1 & :p2.not!


164
165
166
# File 'lib/walrat/parslet_combining.rb', line 164

def not_predicate parslet
  Walrat::NotPredicate.new parslet.to_parseable
end

#omission(parslet) ⇒ Object

Succeeds if parsing succeeds, consuming the output, but doesn’t actually return anything.

This is for elements which are required but which shouldn’t appear in the final AST.



181
182
183
# File 'lib/walrat/parslet_combining.rb', line 181

def omission parslet
  Walrat::ParsletOmission.new parslet.to_parseable
end

#one_or_moreObject

possible synonym “plus”



137
138
139
# File 'lib/walrat/parslet_combining.rb', line 137

def one_or_more
  self.repeat 1
end

#optional(default_return_value = NoParameterMarker.instance) ⇒ Object

Shorthand for ParsletCombining.repetition(0, 1). This method optionally takes a single parameter specifying what object should be returned as a placeholder when there are no matches; this is useful for packing into ASTs where it may be better to parse an empty Array rather than nil. The specified object is cloned and returned in the event that there are no matches. As a convenience, the specified object is automatically extended using the LocationTracking module (this is a convenience so that you can specify empty Arrays, “[]”, rather than explicitly passing an “ArrayResult.new”)



114
115
116
117
118
119
120
# File 'lib/walrat/parslet_combining.rb', line 114

def optional default_return_value = NoParameterMarker.instance
  if default_return_value == NoParameterMarker.instance
    self.repeat 0, 1 # default behaviour
  else
    self.repeat_with_default 0, 1, default_return_value
  end
end

#parse(string, options = {}) ⇒ Object

Convenience method.



39
40
41
# File 'lib/walrat/parslet_combining.rb', line 39

def parse string, options = {}
  self.to_parseable.parse string, options
end

#repeat(min = nil, max = nil) ⇒ Object

Shorthand for ParsletCombining.repetition.



92
93
94
# File 'lib/walrat/parslet_combining.rb', line 92

def repeat min = nil, max = nil
  self.repetition self, min, max
end

#repeat_with_default(min = nil, max = nil, default = nil) ⇒ Object



101
102
103
# File 'lib/walrat/parslet_combining.rb', line 101

def repeat_with_default min = nil, max = nil, default = nil
  self.repetition_with_default self, min, max, default
end

#repetition(parslet, min, max) ⇒ Object

Defines a repetition of the supplied Parslet (or ParsletCombination). Returns a ParsletRepetition instance.



87
88
89
# File 'lib/walrat/parslet_combining.rb', line 87

def repetition parslet, min, max
  Walrat::ParsletRepetition.new parslet.to_parseable, min, max
end

#repetition_with_default(parslet, min, max, default) ⇒ Object



96
97
98
99
# File 'lib/walrat/parslet_combining.rb', line 96

def repetition_with_default parslet, min, max, default
  Walrat::ParsletRepetitionDefault.new parslet.to_parseable, min,
    max, default
end

#sequence(first, second, *others) ⇒ Object

Defines a sequence of Parslets (or ParsletCombinations). Returns a ParsletSequence instance.



45
46
47
48
# File 'lib/walrat/parslet_combining.rb', line 45

def sequence first, second, *others
  Walrat::ParsletSequence.new first.to_parseable,
    second.to_parseable, *others
end

#skipObject

Shorthand for ParsletCombining.omission



186
187
188
# File 'lib/walrat/parslet_combining.rb', line 186

def skip
  self.omission self
end

#zero_or_more(default_return_value = NoParameterMarker.instance) ⇒ Object

possible synonym “star”



128
129
130
131
132
133
134
# File 'lib/walrat/parslet_combining.rb', line 128

def zero_or_more default_return_value = NoParameterMarker.instance
  if default_return_value == NoParameterMarker.instance
    self.repeat 0 # default behaviour
  else
    self.repeat_with_default 0, nil, default_return_value
  end
end

#zero_or_oneObject

Alternative to optional.



123
124
125
# File 'lib/walrat/parslet_combining.rb', line 123

def zero_or_one
  self.optional
end

#|(alternative_parslet) ⇒ Object

Shorthand for ParsletCombining.choice(left, right)



81
82
83
# File 'lib/walrat/parslet_combining.rb', line 81

def |(alternative_parslet)
  self.choice self, alternative_parslet
end