Class: Prism::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/parse_result.rb,
ext/prism/extension.c

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.



674
675
676
677
678
679
# File 'lib/prism/parse_result.rb', line 674

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.



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

def type
  @type
end

#valueObject (readonly)

A byteslice of the source that this token represents.



671
672
673
# File 'lib/prism/parse_result.rb', line 671

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object

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



709
710
711
712
713
# File 'lib/prism/parse_result.rb', line 709

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

#deconstruct_keys(keys) ⇒ Object

Implement the hash pattern matching interface for Token.



682
683
684
# File 'lib/prism/parse_result.rb', line 682

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

#inspectObject

Returns a string representation of this token.



716
717
718
719
# File 'lib/prism/parse_result.rb', line 716

def inspect
  location
  super
end

#locationObject

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



687
688
689
690
691
# File 'lib/prism/parse_result.rb', line 687

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.



694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/prism/parse_result.rb', line 694

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