Class: TRuby::ParserCombinator::SepBy1

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

Overview

Separated by 1 (at least one)

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, delimiter) ⇒ SepBy1

Returns a new instance of SepBy1.



386
387
388
389
# File 'lib/t_ruby/parser_combinator.rb', line 386

def initialize(parser, delimiter)
  @parser = parser
  @delimiter = delimiter
end

Instance Method Details

#parse(input, position = 0) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/t_ruby/parser_combinator.rb', line 391

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

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

  loop do
    delim_result = @delimiter.parse(input, current_pos)
    break if delim_result.failure?

    item_result = @parser.parse(input, delim_result.position)
    break if item_result.failure?

    results << item_result.value
    current_pos = item_result.position
  end

  ParseResult.success(results, input, current_pos)
end