Class: Yoga::Token

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, value, location) ⇒ Token

Initializes the token with the given kind, value, and location.

Parameters:

  • kind (::Symbol)

    The kind of token. See #kind.

  • value (::String)

    The value of the token. See #value.

  • location (Location)

    The location of the token. See #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.

Returns:

  • (::Numeric)


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).

Returns:

  • (::Symbol)


13
14
15
# File 'lib/yoga/token.rb', line 13

def kind
  @kind
end

#locationLocation (readonly)

The exact location the token was taken from. This only spans one line.

Returns:



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.

Returns:

  • (::String)


19
20
21
# File 'lib/yoga/token.rb', line 19

def value
  @value
end

Class Method Details

.eof(location = Location.default) ⇒ Token

Creates an EOF-kind token. This creates the token at the given location.

Parameters:

  • location (Location) (defaults to: Location.default)

Returns:



40
41
42
# File 'lib/yoga/token.rb', line 40

def self.eof(location = Location.default)
  new(:EOF, "", location)
end

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.

Parameters:

  • other (::Object)

Returns:

  • (Boolean)


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