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

  • #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

  • #==(other)

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

  • #deconstruct_keys(keys)

    Implement the hash pattern matching interface for Token.

  • #inspect

    Returns a string representation of this token.

  • #location

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

  • #pretty_print(q)

    Implement the pretty print interface for Token.

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 811

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 801

attr_reader :source

#type (readonly)

The type of token that this token is.

[ GitHub ]

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

attr_reader :type

#value (readonly)

A byteslice of the source that this token represents.

[ GitHub ]

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

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 846

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 819

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

#inspect

Returns a string representation of this token.

[ GitHub ]

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

def inspect
  location
  super
end

#location

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

[ GitHub ]

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

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 831

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