Class: TRuby::ParserCombinator::Choice
- Defined in:
- lib/t_ruby/parser_combinator/combinators/choice.rb
Overview
Choice: try multiple parsers in order
Instance Method Summary collapse
-
#initialize(*parsers) ⇒ Choice
constructor
A new instance of Choice.
- #parse(input, position = 0) ⇒ Object
Methods inherited from Parser
#<<, #>>, #between, #flat_map, #label, #lookahead, #many, #many1, #map, #not_followed_by, #optional, #sep_by, #sep_by1, #|
Constructor Details
#initialize(*parsers) ⇒ Choice
Returns a new instance of Choice.
7 8 9 |
# File 'lib/t_ruby/parser_combinator/combinators/choice.rb', line 7 def initialize(*parsers) @parsers = parsers end |
Instance Method Details
#parse(input, position = 0) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/t_ruby/parser_combinator/combinators/choice.rb', line 11 def parse(input, position = 0) best_error = nil best_position = position @parsers.each do |parser| result = parser.parse(input, position) return result if result.success? if result.position >= best_position best_error = result.error best_position = result.position end end ParseResult.failure(best_error || "No alternative matched", input, best_position) end |