Class: Carbon::Compiler::Scanner::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/carbon/compiler/scanner/token.rb

Overview

A token from the scanner.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, location) ⇒ Token

Initialize the token. Freezes it after initializing.

Parameters:



31
32
33
34
35
36
37
38
# File 'lib/carbon/compiler/scanner/token.rb', line 31

def initialize(type, value, location)
  @type = type
  @value = value
  @location = location
  @children = []
  to_a
  freeze
end

Instance Attribute Details

#locationLocation (readonly)

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

Returns:



24
25
26
# File 'lib/carbon/compiler/scanner/token.rb', line 24

def location
  @location
end

#typeSymbol (readonly)

The type of the token. For any explicit matches (e.g. ‘“module”`), this is the exact match (e.g. `:module`). For any regular matches (e.g. an identifier), this is in all caps (e.g. `:IDENTIFIER`).

Returns:

  • (Symbol)


14
15
16
# File 'lib/carbon/compiler/scanner/token.rb', line 14

def type
  @type
end

#valueString (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/carbon/compiler/scanner/token.rb', line 19

def value
  @value
end

Instance Method Details

#hashNumeric

Creates a numeric representation of this class for hashing.

Returns:

  • (Numeric)

See Also:



52
53
54
# File 'lib/carbon/compiler/scanner/token.rb', line 52

def hash
  to_a.hash
end

#to_a(Class, Symbol, String, Location)

Creates an array representation of this class. This is cached and frozen, since nothing about this class changes.

Returns:



44
45
46
# File 'lib/carbon/compiler/scanner/token.rb', line 44

def to_a
  @array ||= [self.class, @type, @value, @location].freeze
end