Class: TRuby::ParserCombinator::Alternative

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

Overview

Alternative: try first, if fails try second

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(left, right) ⇒ Alternative

Returns a new instance of Alternative.



249
250
251
252
# File 'lib/t_ruby/parser_combinator.rb', line 249

def initialize(left, right)
  @left = left
  @right = right
end

Instance Method Details

#parse(input, position = 0) ⇒ Object



254
255
256
257
258
259
# File 'lib/t_ruby/parser_combinator.rb', line 254

def parse(input, position = 0)
  result = @left.parse(input, position)
  return result if result.success?

  @right.parse(input, position)
end