Class: ABNF::Parser::Compiler::Alternation

Inherits:
RuleBuilder
  • Object
show all
Defined in:
lib/abnf/parser/compiler/alternation.rb

Instance Attribute Summary

Attributes inherited from RuleBuilder

#compiler

Instance Method Summary collapse

Methods inherited from RuleBuilder

#call, #initialize

Constructor Details

This class inherits a constructor from ABNF::Parser::Compiler::RuleBuilder

Instance Method Details

#alternation_slash(_) ⇒ Object



5
6
# File 'lib/abnf/parser/compiler/alternation.rb', line 5

def alternation_slash _
end

#alternative(rule) ⇒ Object



8
9
10
# File 'lib/abnf/parser/compiler/alternation.rb', line 8

def alternative rule
  alternatives << rule
end

#alternativesObject



12
13
14
# File 'lib/abnf/parser/compiler/alternation.rb', line 12

def alternatives
  @alternatives ||= []
end

#c_nl(token) ⇒ Object



16
17
18
19
# File 'lib/abnf/parser/compiler/alternation.rb', line 16

def c_nl token
  finished
  compiler.handle token
end

#c_wsp(_) ⇒ Object



21
22
# File 'lib/abnf/parser/compiler/alternation.rb', line 21

def c_wsp _
end

#finishedObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/abnf/parser/compiler/alternation.rb', line 29

def finished
  if alternatives.size == 1
    rule = alternatives[0]
  else
    abnf = alternatives.map &:abnf
    abnf *= ' / '
    rule = Rules::Alternation.new alternatives, abnf
  end

  compiler.pop rule
end

#group_stop(token) ⇒ Object



24
25
26
27
# File 'lib/abnf/parser/compiler/alternation.rb', line 24

def group_stop token
  finished
  compiler.handle token
end

#option_stop(token) ⇒ Object



41
42
43
44
# File 'lib/abnf/parser/compiler/alternation.rb', line 41

def option_stop token
  finished
  compiler.handle token
end

#start_rule(token) ⇒ Object



46
47
48
49
50
# File 'lib/abnf/parser/compiler/alternation.rb', line 46

def start_rule token
  compiler.push Concatenation do |rule|
    alternative rule
  end
end