Class: DParse::Parsers::Alt
- Inherits:
-
DParse::Parser
- Object
- DParse::Parser
- DParse::Parsers::Alt
- Defined in:
- lib/d-parse/parsers/combinators/alt.rb
Instance Method Summary collapse
-
#initialize(*parsers) ⇒ Alt
constructor
A new instance of Alt.
- #inspect ⇒ Object
- #read(input, pos) ⇒ Object
Methods inherited from DParse::Parser
#apply, #bind, #capture, #compact, #expectation_message, #first, #flatten, #ignore, #map, #match?, #second, #select_even, #select_odd, #to_s
Constructor Details
#initialize(*parsers) ⇒ Alt
Returns a new instance of Alt.
4 5 6 7 |
# File 'lib/d-parse/parsers/combinators/alt.rb', line 4 def initialize(*parsers) # FIXME: ensure >0 parsers are provided @parsers = parsers end |
Instance Method Details
#inspect ⇒ Object
27 28 29 |
# File 'lib/d-parse/parsers/combinators/alt.rb', line 27 def inspect "alt(#{@parsers.map(&:inspect).join(',')})" end |
#read(input, pos) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/d-parse/parsers/combinators/alt.rb', line 9 def read(input, pos) init = DParse::Failure.new(input, DParse::Position::FAR_BEHIND) @parsers.reduce(init) do |old_res, parser| case old_res when DParse::Success old_res when DParse::Failure new_res = parser.read(input, pos) case new_res when DParse::Success new_res.with_best_failure(old_res) when DParse::Failure [old_res, new_res].max_by { |r| r.pos.index } end end end end |