Class: Prism::Token

Inherits:
Object show all
Defined in:
lib/prism/parse_result.rb

Overview

This represents a token from the Ruby source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, location) ⇒ Token

Create a new token object with the given type, value, and location.



435
436
437
438
439
# File 'lib/prism/parse_result.rb', line 435

def initialize(type, value, location)
  @type = type
  @value = value
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

A Location object representing the location of this token in the source.



432
433
434
# File 'lib/prism/parse_result.rb', line 432

def location
  @location
end

#typeObject (readonly)

The type of token that this token is.



426
427
428
# File 'lib/prism/parse_result.rb', line 426

def type
  @type
end

#valueObject (readonly)

A byteslice of the source that this token represents.



429
430
431
# File 'lib/prism/parse_result.rb', line 429

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object

Returns true if the given other token is equal to this token.



462
463
464
465
466
# File 'lib/prism/parse_result.rb', line 462

def ==(other)
  other.is_a?(Token) &&
    other.type == type &&
    other.value == value
end

#deconstruct_keys(keys) ⇒ Object

Implement the hash pattern matching interface for Token.



442
443
444
# File 'lib/prism/parse_result.rb', line 442

def deconstruct_keys(keys)
  { type: type, value: value, location: location }
end

#pretty_print(q) ⇒ Object

Implement the pretty print interface for Token.



447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/prism/parse_result.rb', line 447

def pretty_print(q)
  q.group do
    q.text(type.to_s)
    self.location.pretty_print(q)
    q.text("(")
    q.nest(2) do
      q.breakable("")
      q.pp(value)
    end
    q.breakable("")
    q.text(")")
  end
end