Class: ABNF::Compiler::Rule::TerminalValue

Inherits:
Object
  • Object
show all
Includes:
ABNF::Compiler::Rule
Defined in:
lib/abnf/compiler/rule/terminal_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ABNF::Compiler::Rule

#parse?

Constructor Details

#initialize(string, case_sensitive = false) ⇒ TerminalValue

Returns a new instance of TerminalValue.



10
11
12
13
# File 'lib/abnf/compiler/rule/terminal_value.rb', line 10

def initialize string, case_sensitive = false
  @string = string
  @case_sensitive = case_sensitive
end

Instance Attribute Details

#case_sensitiveObject (readonly)

Returns the value of attribute case_sensitive.



7
8
9
# File 'lib/abnf/compiler/rule/terminal_value.rb', line 7

def case_sensitive
  @case_sensitive
end

#stringObject (readonly)

Returns the value of attribute string.



8
9
10
# File 'lib/abnf/compiler/rule/terminal_value.rb', line 8

def string
  @string
end

Instance Method Details

#parse(stream) ⇒ Object



15
16
17
18
19
# File 'lib/abnf/compiler/rule/terminal_value.rb', line 15

def parse stream
  match_data = stream.match regexp
  return unless match_data
  AST.leaf match_data.to_s
end

#regexpObject



21
22
23
24
25
26
27
28
# File 'lib/abnf/compiler/rule/terminal_value.rb', line 21

def regexp
  @regexp ||=
    begin
      escaped_string = Regexp.escape string
      case_insensitive = !case_sensitive
      Regexp.new escaped_string, case_insensitive, 'n'.freeze
    end
end