class DParse::Parsers::Alt

Public Class Methods

new(*parsers) click to toggle source
# File lib/d-parse/parsers/combinators/alt.rb, line 4
def initialize(*parsers)
  # FIXME: ensure >0 parsers are provided
  @parsers = parsers
end

Public Instance Methods

inspect() click to toggle source
# File lib/d-parse/parsers/combinators/alt.rb, line 27
def inspect
  "alt(#{@parsers.map(&:inspect).join(',')})"
end
read(input, pos) click to toggle source
# 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