Module: Walrus::Grammar::ParsletCombining
- Defined in:
- lib/walrus/grammar/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
-
#&(next_parslet) ⇒ Object
Shorthand for ParsletCombining.sequence(first, second).
-
#>>(next_parslet) ⇒ Object
Shorthand for ParsletCombining.sequence(first, second).
-
#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.
-
#and_predicate(parslet) ⇒ Object
Parsing Expression Grammar support.
-
#choice(left, right, *others) ⇒ Object
Defines a choice of Parslets (or ParsletCombinations).
-
#memoizing_parse(string, options = {}) ⇒ Object
Convenience method.
-
#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.
-
#not! ⇒ Object
Shorthand for not_predicate.
-
#not_predicate(parslet) ⇒ Object
Parsing Expression Grammar support.
-
#omission(parslet) ⇒ Object
Succeeds if parsing succeeds, consuming the output, but doesn’t actually return anything.
-
#one_or_more ⇒ Object
possible synonym “plus”.
-
#optional(default_return_value = NoParameterMarker.instance) ⇒ Object
Shorthand for ParsletCombining.repetition(0, 1).
-
#parse(string, options = {}) ⇒ Object
Convenience method.
-
#repeat(min = nil, max = nil) ⇒ Object
Shorthand for ParsletCombining.repetition.
- #repeat_with_default(min = nil, max = nil, default = nil) ⇒ Object
-
#repetition(parslet, min, max) ⇒ Object
Defines a repetition of the supplied Parslet (or ParsletCombination).
- #repetition_with_default(parslet, min, max, default) ⇒ Object
-
#sequence(first, second, *others) ⇒ Object
Defines a sequence of Parslets (or ParsletCombinations).
-
#skip ⇒ Object
Shorthand for ParsletCombining.omission.
-
#zero_or_more(default_return_value = NoParameterMarker.instance) ⇒ Object
possible synonym “star”.
-
#zero_or_one ⇒ Object
Alternative to optional.
-
#|(alternative_parslet) ⇒ Object
Shorthand for ParsletCombining.choice(left, right).
Instance Method Details
#&(next_parslet) ⇒ Object
Shorthand for ParsletCombining.sequence(first, second).
40 41 42 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 40 def &(next_parslet) self.sequence(self, next_parslet) end |
#>>(next_parslet) ⇒ Object
Shorthand for ParsletCombining.sequence(first, second)
54 55 56 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 54 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.
125 126 127 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 125 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).
119 120 121 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 119 def and_predicate(parslet) Walrus::Grammar::AndPredicate.new(parslet.to_parseable) end |
#choice(left, right, *others) ⇒ Object
Defines a choice of Parslets (or ParsletCombinations). Returns a ParsletChoice instance.
60 61 62 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 60 def choice(left, right, *others) Walrus::Grammar::ParsletChoice.new(left.to_parseable, right.to_parseable, *others) end |
#memoizing_parse(string, options = {}) ⇒ Object
Convenience method.
24 25 26 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 24 def memoizing_parse(string, = {}) self.to_parseable.memoizing_parse(string, ) 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']
49 50 51 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 49 def merge(first, second, *others) Walrus::Grammar::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.
142 143 144 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 142 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!
136 137 138 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 136 def not_predicate(parslet) Walrus::Grammar::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.
148 149 150 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 148 def omission(parslet) Walrus::Grammar::ParsletOmission.new(parslet.to_parseable) end |
#one_or_more ⇒ Object
possible synonym “plus”
113 114 115 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 113 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”)
90 91 92 93 94 95 96 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 90 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.
29 30 31 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 29 def parse(string, = {}) self.to_parseable.parse(string, ) end |
#repeat(min = nil, max = nil) ⇒ Object
Shorthand for ParsletCombining.repetition.
76 77 78 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 76 def repeat(min = nil, max = nil) self.repetition(self, min, max) end |
#repeat_with_default(min = nil, max = nil, default = nil) ⇒ Object
84 85 86 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 84 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.
71 72 73 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 71 def repetition(parslet, min, max) Walrus::Grammar::ParsletRepetition.new(parslet.to_parseable, min, max) end |
#repetition_with_default(parslet, min, max, default) ⇒ Object
80 81 82 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 80 def repetition_with_default(parslet, min, max, default) Walrus::Grammar::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.
35 36 37 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 35 def sequence(first, second, *others) Walrus::Grammar::ParsletSequence.new(first.to_parseable, second.to_parseable, *others) end |
#skip ⇒ Object
Shorthand for ParsletCombining.omission
153 154 155 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 153 def skip self.omission(self) end |
#zero_or_more(default_return_value = NoParameterMarker.instance) ⇒ Object
possible synonym “star”
104 105 106 107 108 109 110 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 104 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_one ⇒ Object
Alternative to optional.
99 100 101 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 99 def zero_or_one self.optional end |
#|(alternative_parslet) ⇒ Object
Shorthand for ParsletCombining.choice(left, right)
65 66 67 |
# File 'lib/walrus/grammar/parslet_combining.rb', line 65 def |(alternative_parslet) self.choice(self, alternative_parslet) end |