Class: EBNF::LL1::Lexer::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/ebnf/ll1/lexer.rb

Overview

Terminal class, representing the terminal identifier and matching regular expression. Optionally, a Terminal may include a map to turn case-insensitively matched terminals into their canonical form

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, regexp, options = {}) ⇒ Terminal

Returns a new instance of Terminal.

Parameters:

  • type (Symbol, nil)
  • regexp (Regexp)
  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :map (Hash{String => String}) — default: {}

    A mapping from terminals, in lower-case form, to their canonical value

  • :unescape (Boolean)

    Cause strings and codepoints to be unescaped.



288
289
290
291
# File 'lib/ebnf/ll1/lexer.rb', line 288

def initialize(type, regexp, options = {})
  @type, @regexp, @options = type, regexp, options
  @map = options.fetch(:map, {})
end

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



278
279
280
# File 'lib/ebnf/ll1/lexer.rb', line 278

def regexp
  @regexp
end

#typeObject (readonly)

Returns the value of attribute type.



277
278
279
# File 'lib/ebnf/ll1/lexer.rb', line 277

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



304
305
306
307
308
309
310
311
# File 'lib/ebnf/ll1/lexer.rb', line 304

def ==(other)
  case other
  when Array
    @type == other.first && @regexp == other.last
  when Terminal
    @type == other.type && @regexp == other.regexp
  end
end

#canonicalize(value) ⇒ String

Map a terminal to it’s canonical form. If there is no map, ‘value` is returned. `value` is unescaped if there is no canonical mapping, and the `:unescape` option is set.

Parameters:

  • value (String)

    value to canonicalize

Returns:

  • (String)


300
301
302
# File 'lib/ebnf/ll1/lexer.rb', line 300

def canonicalize(value)
  @map.fetch(value.downcase, unescape(value))
end