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})

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.

  • :partial_regexp (Regexp)

    A regular expression matching the beginning of this terminal; useful for terminals that match things longer than the scanner low water mark.



284
285
286
287
288
# File 'lib/ebnf/ll1/lexer.rb', line 284

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

Instance Attribute Details

#partial_regexpObject (readonly)

Returns the value of attribute partial_regexp.



272
273
274
# File 'lib/ebnf/ll1/lexer.rb', line 272

def partial_regexp
  @partial_regexp
end

#regexpObject (readonly)

Returns the value of attribute regexp.



271
272
273
# File 'lib/ebnf/ll1/lexer.rb', line 271

def regexp
  @regexp
end

#typeObject (readonly)

Returns the value of attribute type.



270
271
272
# File 'lib/ebnf/ll1/lexer.rb', line 270

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



301
302
303
304
305
306
307
308
# File 'lib/ebnf/ll1/lexer.rb', line 301

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)


297
298
299
# File 'lib/ebnf/ll1/lexer.rb', line 297

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

#unescape(string) ⇒ String (protected)

Perform string and codepoint unescaping if defined for this terminal

Parameters:

  • string (String)

Returns:

  • (String)


315
316
317
318
319
320
321
# File 'lib/ebnf/ll1/lexer.rb', line 315

def unescape(string)
  if @options[:unescape]
    EBNF::Unescape.unescape(string)
  else
    string
  end
end