Class: TRuby::ParserCombinator::TokenMany1

Inherits:
TokenParser
  • Object
show all
Defined in:
lib/t_ruby/parser_combinator/token/token_many1.rb

Overview

Many1: one or more

Instance Method Summary collapse

Methods inherited from TokenParser

#<<, #>>, #label, #many, #many1, #map, #optional, #sep_by, #sep_by1, #|

Constructor Details

#initialize(parser) ⇒ TokenMany1

Returns a new instance of TokenMany1.



7
8
9
# File 'lib/t_ruby/parser_combinator/token/token_many1.rb', line 7

def initialize(parser)
  @parser = parser
end

Instance Method Details

#parse(tokens, position = 0) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/t_ruby/parser_combinator/token/token_many1.rb', line 11

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

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

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

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

    current_pos = result.position
  end

  TokenParseResult.success(results, tokens, current_pos)
end