123456789_123456789_123456789_123456789_123456789_

Class: Prism::Token

Relationships & Source Files
Inherits: Object
Defined in: lib/prism/parse_result.rb,
prism/extension.c

Overview

This represents a token from the Ruby source.

Class Method Summary

Instance Attribute Summary

  • #location readonly

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

  • #type readonly

    The type of token that this token is.

  • #value readonly

    A byteslice of the source that this token represents.

Instance Method Summary

Constructor Details

.new(type, value, location) ⇒ Token

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

[ GitHub ]

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

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

Instance Attribute Details

#location (readonly)

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

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 424

attr_reader :location

#type (readonly)

The type of token that this token is.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 418

attr_reader :type

#value (readonly)

A byteslice of the source that this token represents.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 421

attr_reader :value

Instance Method Details

#==(other)

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

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 454

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

#deconstruct_keys(keys)

Implement the hash pattern matching interface for Token.

[ GitHub ]

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

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

#pretty_print(q)

Implement the pretty print interface for Token.

[ GitHub ]

  
# File 'lib/prism/parse_result.rb', line 439

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