Class: ABNF::Parser::Rules::Alternation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alternatives, abnf) ⇒ Alternation

Returns a new instance of Alternation.



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

def initialize alternatives, abnf
  @abnf = abnf
  @alternatives = alternatives
end

Instance Attribute Details

#abnfObject (readonly)

Returns the value of attribute abnf.



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

def abnf
  @abnf
end

#alternativesObject (readonly)

Returns the value of attribute alternatives.



6
7
8
# File 'lib/abnf/parser/rules/alternation.rb', line 6

def alternatives
  @alternatives
end

Instance Method Details

#==(other_rule) ⇒ Object



13
14
15
16
# File 'lib/abnf/parser/rules/alternation.rb', line 13

def == other_rule
  return unless other_rule.is_a? self.class
  alternatives == other_rule.alternatives
end

#call(io, rule_list = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/abnf/parser/rules/alternation.rb', line 18

def call io, rule_list=nil
  best_match = nil

  alternatives.each do |alternative|
    node = alternative.(io, rule_list) or next

    io.seek -node.octets, IO::SEEK_CUR

    best_match ||= node
    best_match = node if node.octets > best_match.octets
  end

  return unless best_match

  io.seek best_match.octets, IO::SEEK_CUR
  best_match
end