Class: Language::Atom::Or

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

Instance Method Summary collapse

Methods inherited from Language::Atom

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

Constructor Details

#initialize(left:, right:) ⇒ Or

Returns a new instance of Or.



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

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

Instance Method Details

#parse(parser) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/language/atom/or.rb', line 11

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

  right_clone =
    Parser.new(
      root: self,
      input: parser.input,
      cursor: parser.cursor,
      buffer: parser.buffer
    )

  begin
    @left.parse(left_clone)
    parser.cursor = left_clone.cursor
    parser.buffer = left_clone.buffer
    parser.output.merge(left_clone.output)
  rescue Parser::Interuption
    @right.parse(right_clone)
    parser.cursor = right_clone.cursor
    parser.buffer = right_clone.buffer
    parser.output.merge(right_clone.output)
  end
end

#to_sObject



41
42
43
# File 'lib/language/atom/or.rb', line 41

def to_s
  "((#{@left}) | (#{@right}))"
end