Class: TRuby::ParserCombinator::Optional

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

Overview

Optional: zero or 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) ⇒ Optional

Returns a new instance of Optional.



341
342
343
# File 'lib/t_ruby/parser_combinator.rb', line 341

def initialize(parser)
  @parser = parser
end

Instance Method Details

#parse(input, position = 0) ⇒ Object



345
346
347
348
349
350
351
352
# File 'lib/t_ruby/parser_combinator.rb', line 345

def parse(input, position = 0)
  result = @parser.parse(input, position)
  if result.success?
    result
  else
    ParseResult.success(nil, input, position)
  end
end