Class: EBNF::LL1::Lexer::Token

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb

Overview

Represents a lexer token.

Examples:

Creating a new token

token = EBNF::LL1::Lexer::Token.new(:LANGTAG, "en")
token.type   #=> :LANGTAG
token.value  #=> "en"

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, **options) ⇒ Token

Initializes a new token instance.

Parameters:

Options Hash (**options):

  • :lineno (Integer) — default: nil


382
383
384
385
386
387
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 382

def initialize(type, value, **options)
  @type = type.to_s.to_sym if type
  @value = value.to_s
  @options = options.dup
  @lineno  = @options.delete(:lineno)
end

Instance Attribute Details

#linenoInteger (readonly)

The line number where the token was encountered.

Returns:



367
368
369
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 367

def lineno
  @lineno
end

#optionsHash (readonly)

Any additional options for the token.

Returns:



373
374
375
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 373

def options
  @options
end

#typeSymbol (readonly)

The token's symbol type.

Returns:



355
356
357
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 355

def type
  @type
end

#valueString (readonly)

The token's value.

Returns:



361
362
363
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 361

def value
  @value
end

Instance Method Details

#===(value) ⇒ Boolean

Returns true if the given value matches either the type or value of this token.

Examples:

Matching using the symbolic type

EBNF::LL1::Lexer::Token.new(:NIL) === :NIL     #=> true

Matching using the string value

EBNF::LL1::Lexer::Token.new(nil, "{") === "{"  #=> true

Parameters:

Returns:

  • (Boolean)


415
416
417
418
419
420
421
422
423
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 415

def ===(value)
  case value
    when Symbol
      value == @type
    when ::String
      @value == (@options[:case_insensitive] ? value.to_s.downcase : value.to_s)
    else value == @value
  end
end

#[](key) ⇒ Object

Returns the attribute named by key.

Parameters:

Returns:



394
395
396
397
398
399
400
401
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 394

def [](key)
  key = key.to_s.to_sym unless key.is_a?(Integer) || key.is_a?(Symbol)
  case key
    when 0, :type    then @type
    when 1, :value   then @value
    else nil
  end
end

#inspectString

Returns a developer-friendly representation of this token.

Returns:



457
458
459
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 457

def inspect
  "#{@value.inspect}#{'(' + @type.to_s + ')' if @type}"
end

#representationObject

Returns type, if not nil, otherwise value



441
442
443
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 441

def representation
  @type ? @type : @value
end

#to_aArray

Returns an array representation of this token.

Returns:



449
450
451
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 449

def to_a
  [@type, @value]
end

#to_hashHash

Returns a hash table representation of this token.

Returns:



429
430
431
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 429

def to_hash
  {type: @type, value: @value}
end

#to_sObject

Readable version of token



435
436
437
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/ebnf-c8f40958c6c3/lib/ebnf/ll1/lexer.rb', line 435

def to_s
  @type ? @type.inspect : @value
end