Class: Language::Atom::Maybe

Inherits:
Language::Atom show all
Defined in:
lib/language/atom/maybe.rb

Instance Method Summary collapse

Methods inherited from Language::Atom

#<<, #>>, #absent, #aka, #any, #ignore, #inspect, #maybe, #repeat, #str, #then, #|

Constructor Details

#initialize(parent:) ⇒ Maybe

Returns a new instance of Maybe.



6
7
8
# File 'lib/language/atom/maybe.rb', line 6

def initialize(parent:)
  @parent = parent
end

Instance Method Details

#parse(parser) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/language/atom/maybe.rb', line 10

def parse(parser)
  clone =
    Parser.new(
      root: self,
      input: parser.input,
      cursor: parser.cursor,
      buffer: parser.buffer
    )

  @parent.parse(clone)
rescue Parser::Interuption
else
  parser.buffer = clone.buffer
  parser.cursor = clone.cursor
  parser.output = clone.output
end

#to_sObject



27
28
29
# File 'lib/language/atom/maybe.rb', line 27

def to_s
  @parent ? "#{@parent}.maybe" : "maybe"
end