Class: ABNF::Compiler::Rule::Alternative

Inherits:
Object
  • Object
show all
Includes:
ABNF::Compiler::Rule
Defined in:
lib/abnf/compiler/rule/alternative.rb

Defined Under Namespace

Classes: Match

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ABNF::Compiler::Rule

#parse?

Constructor Details

#initialize(*rules) ⇒ Alternative

Returns a new instance of Alternative.



9
10
11
# File 'lib/abnf/compiler/rule/alternative.rb', line 9

def initialize *rules
  @rules = rules
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/abnf/compiler/rule/alternative.rb', line 7

def rules
  @rules
end

Instance Method Details

#find_match(stream) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/abnf/compiler/rule/alternative.rb', line 13

def find_match stream
  matches = []

  rules.each do |rule|
    stream.branch do
      node = rule.parse stream
      matches.<< Match.new node, stream.position if node
      false
    end
  end

  matches.sort!
  matches.pop
end

#parse(stream) ⇒ Object



28
29
30
31
32
33
# File 'lib/abnf/compiler/rule/alternative.rb', line 28

def parse stream
  match = find_match stream
  return unless match
  stream.seek match.stream_position
  match.node
end