Class: TRuby::ParserCombinator::Many1

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

Overview

Many1: one 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) ⇒ Many1

Returns a new instance of Many1.



315
316
317
# File 'lib/t_ruby/parser_combinator.rb', line 315

def initialize(parser)
  @parser = parser
end

Instance Method Details

#parse(input, position = 0) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/t_ruby/parser_combinator.rb', line 319

def parse(input, position = 0)
  first = @parser.parse(input, position)
  return first if first.failure?

  results = [first.value]
  current_pos = first.position

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

    results << result.value
    break if result.position == current_pos
    current_pos = result.position
  end

  ParseResult.success(results, input, current_pos)
end