Class: ABNF::Compiler::Grammar::NumVal

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

Constant Summary collapse

PATTERN =
%r{\A%
  (?:
    (?<base_code>x)(?<first_character>[[:xdigit:]]+)
    (?:(?:-(?<range_end>[[:xdigit:]]+))|(?<sequence>(?:\.[[:xdigit:]]+)+))?
  |
    (?<base_code>d)(?<first_character>[[:digit:]]+)
    (?:(?:-(?<range_end>[[:digit:]]+))|(?<sequence>(?:\.[[:digit:]]+)+))?
  |
    (?<base_code>b)(?<first_character>[01]+)
    (?:(?:-(?<range_end>[01]+))|(?<sequence>(?:\.[01]+)+))?
  )
}nx

Instance Attribute Summary

Attributes included from Compiler

#match_data, #stream

Instance Method Summary collapse

Methods included from Compiler

included, #initialize

Instance Method Details

#baseObject



20
21
22
23
24
25
26
27
28
# File 'lib/abnf/compiler/grammar/num_val.rb', line 20

def base
  base_code = match_data['base_code'.freeze]

  {
    'b'.freeze => 2,
    'd'.freeze => 10,
    'x'.freeze => 16,
  }[base_code]
end

#build_stringObject



30
31
32
33
34
35
36
37
38
# File 'lib/abnf/compiler/grammar/num_val.rb', line 30

def build_string
  *characters = sequence.to_s.split '.'

  [first_character, *characters].reduce String.new do |string, character|
    octet = convert_hex_character character
    string << octet
    string
  end
end

#callObject



40
41
42
43
44
45
46
47
48
# File 'lib/abnf/compiler/grammar/num_val.rb', line 40

def call
  if range_end
    first_octet = convert_hex_character first_character
    last_octet = convert_hex_character range_end
    return ABNF::Compiler::Rule::ValueRange.new first_octet, last_octet
  end

  ABNF::Compiler::Rule::TerminalValue.new build_string, true
end

#convert_hex_character(hex) ⇒ Object



50
51
52
53
54
# File 'lib/abnf/compiler/grammar/num_val.rb', line 50

def convert_hex_character hex
  character = hex.to_i base
  octet = character.chr
  octet
end

#first_characterObject



56
57
58
# File 'lib/abnf/compiler/grammar/num_val.rb', line 56

def first_character
  match_data['first_character'.freeze]
end

#range_endObject



60
61
62
# File 'lib/abnf/compiler/grammar/num_val.rb', line 60

def range_end
  match_data['range_end'.freeze]
end

#sequenceObject



64
65
66
67
68
# File 'lib/abnf/compiler/grammar/num_val.rb', line 64

def sequence
  sequence = match_data['sequence'.freeze]
  sequence.slice! 0, 1 if sequence
  sequence
end