Class: ABNF::Compiler::Grammar::Element

Inherits:
Object
  • Object
show all
Includes:
Compiler
Defined in:
lib/abnf/compiler/grammar/element.rb

Constant Summary collapse

GROUP_START =
%r{\(}
GROUP_STOP =
%r{\)}
OPTION_START =
%r{\[}
OPTION_STOP =
%r{\]}

Instance Attribute Summary

Attributes included from Compiler

#match_data, #stream

Instance Method Summary collapse

Methods included from Compiler

included, #initialize

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
# File 'lib/abnf/compiler/grammar/element.rb', line 12

def call
  if stream.next_character == '['.freeze
    option
  elsif stream.next_character == '('.freeze
    group
  else
    terminal
  end
end

#groupObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/abnf/compiler/grammar/element.rb', line 22

def group
  stream.match GROUP_START
  stream.match C_WSP

  alternative = Alternative.compile stream

  stream.match C_WSP
  stream.match GROUP_STOP

  alternative
end

#optionObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abnf/compiler/grammar/element.rb', line 34

def option
  stream.match OPTION_START
  stream.match C_WSP

  alternative = Alternative.compile stream
  option = ABNF::Compiler::Rule::Optional.new alternative

  stream.match C_WSP
  stream.match OPTION_STOP

  option
end

#terminalObject



47
48
49
50
51
52
53
# File 'lib/abnf/compiler/grammar/element.rb', line 47

def terminal
  [Rulename, CharVal, NumVal, ProseVal].each do |type|
    output = type.compile stream
    return output if output
  end
  nil
end