Module: Walrus::Grammar::ParsletCombining

Included in:
Proc, Regexp, String, Symbol, Parslet, ParsletCombination, Predicate
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

Instance Method Details

#&(next_parslet) ⇒ Object

Shorthand for ParsletCombining.sequence(first, second).



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

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

#>>(next_parslet) ⇒ Object

Shorthand for ParsletCombining.sequence(first, second)



48
49
50
# File 'lib/walrus/grammar/parslet_combining.rb', line 48

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)


119
120
121
# File 'lib/walrus/grammar/parslet_combining.rb', line 119

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).



113
114
115
# File 'lib/walrus/grammar/parslet_combining.rb', line 113

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.



54
55
56
# File 'lib/walrus/grammar/parslet_combining.rb', line 54

def choice(left, right, *others)
  Walrus::Grammar::ParsletChoice.new(left.to_parseable, right.to_parseable, *others)
end

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

Convenience method.



18
19
20
# File 'lib/walrus/grammar/parslet_combining.rb', line 18

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']


43
44
45
# File 'lib/walrus/grammar/parslet_combining.rb', line 43

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.



136
137
138
# File 'lib/walrus/grammar/parslet_combining.rb', line 136

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!


130
131
132
# File 'lib/walrus/grammar/parslet_combining.rb', line 130

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.



142
143
144
# File 'lib/walrus/grammar/parslet_combining.rb', line 142

def omission(parslet)
  Walrus::Grammar::ParsletOmission.new(parslet.to_parseable)
end

#one_or_moreObject

possible synonym “plus”



107
108
109
# File 'lib/walrus/grammar/parslet_combining.rb', line 107

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”)



84
85
86
87
88
89
90
# File 'lib/walrus/grammar/parslet_combining.rb', line 84

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.



23
24
25
# File 'lib/walrus/grammar/parslet_combining.rb', line 23

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

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

Shorthand for ParsletCombining.repetition.



70
71
72
# File 'lib/walrus/grammar/parslet_combining.rb', line 70

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

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



78
79
80
# File 'lib/walrus/grammar/parslet_combining.rb', line 78

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.



65
66
67
# File 'lib/walrus/grammar/parslet_combining.rb', line 65

def repetition(parslet, min, max)
  Walrus::Grammar::ParsletRepetition.new(parslet.to_parseable, min, max)
end

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



74
75
76
# File 'lib/walrus/grammar/parslet_combining.rb', line 74

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.



29
30
31
# File 'lib/walrus/grammar/parslet_combining.rb', line 29

def sequence(first, second, *others)
  Walrus::Grammar::ParsletSequence.new(first.to_parseable, second.to_parseable, *others)
end

#skipObject

Shorthand for ParsletCombining.omission



147
148
149
# File 'lib/walrus/grammar/parslet_combining.rb', line 147

def skip
  self.omission(self)
end

#zero_or_more(default_return_value = NoParameterMarker.instance) ⇒ Object

possible synonym “star”



98
99
100
101
102
103
104
# File 'lib/walrus/grammar/parslet_combining.rb', line 98

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.



93
94
95
# File 'lib/walrus/grammar/parslet_combining.rb', line 93

def zero_or_one
  self.optional
end

#|(alternative_parslet) ⇒ Object

Shorthand for ParsletCombining.choice(left, right)



59
60
61
# File 'lib/walrus/grammar/parslet_combining.rb', line 59

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