Class: SPARQL::Grammar::Lexer::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/sparql/grammar/lexer.rb

Overview

Represents a lexer token.

Examples:

Creating a new token

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

See Also:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Token) initialize(type, value = nil, options = {})

Initializes a new token instance.

Parameters:

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

Options Hash (options):

  • (Integer) :lineno — default: nil


468
469
470
471
472
# File 'lib/sparql/grammar/lexer.rb', line 468

def initialize(type, value = nil, options = {})
  @type, @value = (type ? type.to_s.to_sym : nil), value
  @options = options.dup
  @lineno  = @options.delete(:lineno)
end

Instance Attribute Details

- (Integer) lineno (readonly)

The line number where the token was encountered.

Returns:

  • (Integer)


490
491
492
# File 'lib/sparql/grammar/lexer.rb', line 490

def lineno
  @lineno
end

- (Hash) options (readonly)

Any additional options for the token.

Returns:

  • (Hash)


496
497
498
# File 'lib/sparql/grammar/lexer.rb', line 496

def options
  @options
end

- (Symbol) type (readonly)

The token’s symbol type.

Returns:

  • (Symbol)


478
479
480
# File 'lib/sparql/grammar/lexer.rb', line 478

def type
  @type
end

- (Object) value (readonly)

The token’s value.

Returns:

  • (Object)


484
485
486
# File 'lib/sparql/grammar/lexer.rb', line 484

def value
  @value
end

Instance Method Details

- (Boolean) ===(value)

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

Examples:

Matching using the symbolic type

SPARQL::Grammar::Lexer::Token.new(:NIL) === :NIL     #=> true

Matching using the string value

SPARQL::Grammar::Lexer::Token.new(nil, "{") === "{"  #=> true

Parameters:

Returns:

  • (Boolean)


524
525
526
527
528
529
530
# File 'lib/sparql/grammar/lexer.rb', line 524

def ===(value)
  case value
    when Symbol   then value == @type
    when ::String then value.to_s == @value.to_s
    else value == @value
  end
end

- (Object) [](key)

Returns the attribute named by `key`.

Parameters:

  • (Symbol) key

Returns:

  • (Object)


503
504
505
506
507
508
509
510
# File 'lib/sparql/grammar/lexer.rb', line 503

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

- (String) inspect

Returns a developer-friendly representation of this token.

Returns:



558
559
560
# File 'lib/sparql/grammar/lexer.rb', line 558

def inspect
  to_hash.inspect
end

- (Object) representation

Returns type, if not nil, otherwise value



542
543
544
# File 'lib/sparql/grammar/lexer.rb', line 542

def representation
  @type ? @type : @value
end

- (Array) to_a

Returns an array representation of this token.

Returns:

  • (Array)


550
551
552
# File 'lib/sparql/grammar/lexer.rb', line 550

def to_a
  [@type, @value]
end

- (Hash) to_hash

Returns a hash table representation of this token.

Returns:

  • (Hash)


536
537
538
# File 'lib/sparql/grammar/lexer.rb', line 536

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