Class: Prism::Token

Inherits:
Object
  • 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(source, type, value, location) ⇒ Token

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



811
812
813
814
815
816
# 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

#typeObject (readonly)

The type of token that this token is.



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

def type
  @type
end

#valueObject (readonly)

A byteslice of the source that this token represents.



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object

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



846
847
848
849
850
# File 'lib/prism/parse_result.rb', line 846

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

#deconstruct_keys(keys) ⇒ Object

Implement the hash pattern matching interface for Token.



819
820
821
# File 'lib/prism/parse_result.rb', line 819

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

#inspectObject

Returns a string representation of this token.



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

def inspect
  location
  super
end

#locationObject

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



824
825
826
827
828
# 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) ⇒ Object

Implement the pretty print interface for Token.



831
832
833
834
835
836
837
838
839
840
841
842
843
# 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