Class: RaddDjur::Grammar::Parser
- Inherits:
-
Object
- Object
- RaddDjur::Grammar::Parser
show all
- Includes:
- Immutable, Parsers
- Defined in:
- lib/radd_djur/grammar.rb
Instance Method Summary
collapse
Methods included from Parsers
any_char, char, fail, ret, string
Constructor Details
#initialize(&block) ⇒ Parser
Returns a new instance of Parser.
105
106
107
|
# File 'lib/radd_djur/grammar.rb', line 105
def initialize(&block)
@block = block
end
|
Instance Method Details
#/(p2) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/radd_djur/grammar.rb', line 132
def /(p2)
p1 = self
Parser.new { |d|
Promise.lazy {
result = p1.parse(d).force
if result.succeeded?
Promise.eager(result)
else
p2.to_parser.parse(d)
end
}
}
end
|
#bind(&f2) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/radd_djur/grammar.rb', line 117
def bind(&f2)
p1 = self
Parser.new { |d|
Promise.lazy {
result = p1.parse(d).force
if result.succeeded?
p2 = f2.call(result.value)
p2.to_parser.parse(result.remainder)
else
Promise.eager(NO_PARSE)
end
}
}
end
|
#one_or_more ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/radd_djur/grammar.rb', line 154
def one_or_more
bind { |x|
zero_or_more.bind { |xs|
ret xs.cons(x)
}
}
end
|
#optional ⇒ Object
146
147
148
|
# File 'lib/radd_djur/grammar.rb', line 146
def optional
self / ret(nil)
end
|
#parse(d) ⇒ Object
113
114
115
|
# File 'lib/radd_djur/grammar.rb', line 113
def parse(d)
@block.call(d)
end
|
#to_parser ⇒ Object
109
110
111
|
# File 'lib/radd_djur/grammar.rb', line 109
def to_parser
self
end
|
#zero_or_more ⇒ Object
150
151
152
|
# File 'lib/radd_djur/grammar.rb', line 150
def zero_or_more
one_or_more / ret(List.empty)
end
|