Class: TRuby::ParserCombinator::ChainLeft
- Defined in:
- lib/t_ruby/parser_combinator/combinators/chain_left.rb
Overview
Chainl: left-associative chain
Instance Method Summary collapse
-
#initialize(term, op) ⇒ ChainLeft
constructor
A new instance of ChainLeft.
- #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(term, op) ⇒ ChainLeft
Returns a new instance of ChainLeft.
7 8 9 10 |
# File 'lib/t_ruby/parser_combinator/combinators/chain_left.rb', line 7 def initialize(term, op) @term = term @op = op end |
Instance Method Details
#parse(input, position = 0) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/t_ruby/parser_combinator/combinators/chain_left.rb', line 12 def parse(input, position = 0) first = @term.parse(input, position) return first if first.failure? result = first.value current_pos = first.position loop do op_result = @op.parse(input, current_pos) break if op_result.failure? term_result = @term.parse(input, op_result.position) break if term_result.failure? result = op_result.value.call(result, term_result.value) current_pos = term_result.position end ParseResult.success(result, input, current_pos) end |