Class: ABNF::Compiler::Grammar::NumVal
- Inherits:
-
Object
- Object
- ABNF::Compiler::Grammar::NumVal
- 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
Instance Method Summary collapse
- #base ⇒ Object
- #build_string ⇒ Object
- #call ⇒ Object
- #convert_hex_character(hex) ⇒ Object
- #first_character ⇒ Object
- #range_end ⇒ Object
- #sequence ⇒ Object
Methods included from Compiler
Instance Method Details
#base ⇒ Object
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_string ⇒ Object
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 |
#call ⇒ Object
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_character ⇒ Object
56 57 58 |
# File 'lib/abnf/compiler/grammar/num_val.rb', line 56 def first_character match_data['first_character'.freeze] end |
#range_end ⇒ Object
60 61 62 |
# File 'lib/abnf/compiler/grammar/num_val.rb', line 60 def range_end match_data['range_end'.freeze] end |
#sequence ⇒ Object
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 |