Class: TRuby::ParserCombinator::Many

Inherits:
Parser
  • Object
show all
Defined in:
lib/t_ruby/parser_combinator/combinators/many.rb

Overview

Many: zero or more

Instance Method Summary collapse

Methods inherited from Parser

#<<, #>>, #between, #flat_map, #label, #lookahead, #many, #many1, #map, #not_followed_by, #optional, #sep_by, #sep_by1, #|

Constructor Details

#initialize(parser) ⇒ Many

Returns a new instance of Many.



7
8
9
# File 'lib/t_ruby/parser_combinator/combinators/many.rb', line 7

def initialize(parser)
  @parser = parser
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/many.rb', line 11

def parse(input, position = 0)
  results = []
  current_pos = position

  loop do
    result = @parser.parse(input, current_pos)
    break if result.failure?

    results << result.value
    break if result.position == current_pos # Prevent infinite loop

    current_pos = result.position
  end

  ParseResult.success(results, input, current_pos)
end