Class: ABNF::Parser::Rules::Terminal

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

Direct Known Subclasses

CharacterRange, String

Defined Under Namespace

Classes: CharacterRange, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, abnf) ⇒ Terminal

Returns a new instance of Terminal.



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

def initialize pattern, abnf
  @abnf = abnf
  @pattern = pattern
  fail if self.class == Terminal
end

Instance Attribute Details

#abnfObject (readonly)

Returns the value of attribute abnf.



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

def abnf
  @abnf
end

#patternObject (readonly)

Returns the value of attribute pattern.



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

def pattern
  @pattern
end

Class Method Details

.character_range(*arguments) ⇒ Object



57
58
59
# File 'lib/abnf/parser/rules/terminal.rb', line 57

def self.character_range *arguments
  CharacterRange.new *arguments
end

.string(*arguments) ⇒ Object



43
44
45
# File 'lib/abnf/parser/rules/terminal.rb', line 43

def self.string *arguments
  String.new *arguments
end

Instance Method Details

#==(other_rule) ⇒ Object



14
15
16
17
# File 'lib/abnf/parser/rules/terminal.rb', line 14

def == other_rule
  return false unless other_rule.is_a? self.class
  pattern == other_rule.pattern
end

#call(io, _ = nil) ⇒ Object



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

def call io, _=nil
  potential_match = io.read octets
  return unless potential_match

  matches = match? potential_match

  if matches
    Node.terminal potential_match, abnf
  else
    io.seek -potential_match.bytesize, IO::SEEK_CUR
    nil
  end
end