Class: Prism::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/parse_result.rb,
sig/generated/prism/parse_result.rbs,
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, state) ⇒ Token

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

: (Source source, Symbol type, String value, Location | Integer location, Integer state) -> void

Parameters:

  • source (Source)
  • type (Symbol)
  • value (String)
  • location (Location, Integer)
  • state (Integer)


1116
1117
1118
1119
1120
1121
1122
# File 'lib/prism/parse_result.rb', line 1116

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

Instance Attribute Details

#source ⇒ Source (readonly)

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

Returns:



1102
1103
1104
# File 'lib/prism/parse_result.rb', line 1102

def source
  @source
end

#type ⇒ Symbol (readonly)

The type of token that this token is.

Returns:

  • (Symbol)


1106
1107
1108
# File 'lib/prism/parse_result.rb', line 1106

def type
  @type
end

#value ⇒ String (readonly)

A byteslice of the source that this token represents.

Returns:

  • (String)


1109
1110
1111
# File 'lib/prism/parse_result.rb', line 1109

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

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

: (untyped other) -> bool

Parameters:

  • other (Object)

Returns:

  • (Boolean)


1160
1161
1162
1163
1164
# File 'lib/prism/parse_result.rb', line 1160

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

#[](index) ⇒ Token

: (Integer index) -> Token | Integer

Parameters:

  • index (Integer)

Returns:



1193
1194
1195
1196
1197
# File 'lib/prism/parse_result.rb', line 1193

def [](index) # :nodoc:
  return self if index == 0
  return @state if index == 1
  raise ArgumentError, "Invalid index #{index}"
end

#_ripper_state ⇒ Integer

For internal use only. : () -> Integer

Returns:

  • (Integer)


1185
1186
1187
# File 'lib/prism/parse_result.rb', line 1185

def _ripper_state # :nodoc:
  @state
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

Implement the hash pattern matching interface for Token.

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


1127
1128
1129
# File 'lib/prism/parse_result.rb', line 1127

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

#deep_freeze ⇒ void

This method returns an undefined value.

Freeze this object and the objects it contains.

: () -> void



1177
1178
1179
1180
1181
# File 'lib/prism/parse_result.rb', line 1177

def deep_freeze
  value.freeze
  location.freeze
  freeze
end

#first ⇒ Token

: () -> Token

Returns:



1200
1201
1202
# File 'lib/prism/parse_result.rb', line 1200

def first # :nodoc:
  self
end

#inspect ⇒ String

Returns a string representation of this token.

: () -> String

Returns:

  • (String)


1169
1170
1171
1172
# File 'lib/prism/parse_result.rb', line 1169

def inspect # :nodoc:
  location
  super
end

#location ⇒ Location

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

: () -> Location

Returns:



1134
1135
1136
1137
1138
# File 'lib/prism/parse_result.rb', line 1134

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

#pretty_print(q) ⇒ void

This method returns an undefined value.

Implement the pretty print interface for Token.

: (PP q) -> void

Parameters:

  • q (PP)


1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# File 'lib/prism/parse_result.rb', line 1143

def pretty_print(q) # :nodoc:
  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