class ABNF::Parser::Rules::Alternation

Attributes

abnf[R]
alternatives[R]

Public Class Methods

new(alternatives, abnf) click to toggle source
# File lib/abnf/parser/rules/alternation.rb, line 8
def initialize alternatives, abnf
  @abnf = abnf
  @alternatives = alternatives
end

Public Instance Methods

==(other_rule) click to toggle source
# 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) click to toggle source
# 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