123456789_123456789_123456789_123456789_123456789_

Class: Prism::Token

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

Overview

This represents a token from the Ruby source.

Class Method Summary

Instance Attribute Summary

  • #type readonly

    The type of token that this token is.

  • #value readonly

    A byteslice of the source that this token represents.

  • #source readonly private

    The Source object that represents the source this token came from.

Instance Method Summary

Constructor Details

.new(source, type, value, location) ⇒ Token

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

[ GitHub ]

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

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

Instance Attribute Details

#source (readonly, private)

The Source object that represents the source this token came from.

[ GitHub ]

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

attr_reader :source

#type (readonly)

The type of token that this token is.

[ GitHub ]

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

attr_reader :type

#value (readonly)

A byteslice of the source that this token represents.

[ GitHub ]

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

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 692

def ==(other)
  Token === other &&
    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 665

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

#location

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

[ GitHub ]

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

def location
  location = @location
  return location if location.is_a?(Location)
  @location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#pretty_print(q)

Implement the pretty print interface for Token.

[ GitHub ]

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

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