Class: Yoga::Token
- Inherits:
-
Object
- Object
- Yoga::Token
- Defined in:
- lib/yoga/token.rb
Overview
A token that is used in scanning. This is emitted when a lexical
structure is encountered; e.g., for a {
.
Instance Attribute Summary collapse
-
#hash ⇒ ::Numeric
readonly
private
A "hash" of the token.
-
#kind ⇒ ::Symbol
readonly
The kind of the token.
-
#location ⇒ Location
readonly
The exact location the token was taken from.
-
#value ⇒ ::String
readonly
The lexeme that this token is associated.
Class Method Summary collapse
-
.eof(location = Location.default) ⇒ Token
Creates an
EOF
-kind token.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Determines if this object is equal to another object.
-
#initialize(kind, value, location) ⇒ Token
constructor
Initializes the token with the given kind, value, and location.
Constructor Details
#initialize(kind, value, location) ⇒ Token
Initializes the token with the given kind, value, and location.
49 50 51 52 53 54 55 |
# File 'lib/yoga/token.rb', line 49 def initialize(kind, value, location) @kind = kind @value = value.freeze @location = location @hash = [@kind, @value, @location].hash freeze end |
Instance Attribute Details
#hash ⇒ ::Numeric (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A "hash" of the token. This is a number that is meant to roughly represent the value of this token. Used primarily for the Hash class.
33 34 35 |
# File 'lib/yoga/token.rb', line 33 def hash @hash end |
#kind ⇒ ::Symbol (readonly)
The kind of the token. For any explicit matches (e.g. "{"
),
this is the exact match (e.g. :"{"
). For any regular matches
(e.g. an identifier), this is in all caps (e.g. :IDENTIFIER
).
13 14 15 |
# File 'lib/yoga/token.rb', line 13 def kind @kind end |
#location ⇒ Location (readonly)
The exact location the token was taken from. This only spans one line.
25 26 27 |
# File 'lib/yoga/token.rb', line 25 def location @location end |
#value ⇒ ::String (readonly)
The lexeme that this token is associated. This is what the token matched directly from the source.
19 20 21 |
# File 'lib/yoga/token.rb', line 19 def value @value end |
Class Method Details
Instance Method Details
#==(other) ⇒ Boolean
Determines if this object is equal to another object. It first checks
for equality using equal?
, then checks if the other is a token. If
the other object is a token, it checks that the attributes equate.
63 64 65 66 |
# File 'lib/yoga/token.rb', line 63 def ==(other) equal?(other) || other.is_a?(Token) && @kind == other.kind && @value == other.value && @location == other.location end |