Class: Carbon::Compiler::Scanner::Token
- Inherits:
-
Object
- Object
- Carbon::Compiler::Scanner::Token
- Defined in:
- lib/carbon/compiler/scanner/token.rb
Overview
A token from the scanner.
Instance Attribute Summary collapse
-
#location ⇒ Location
readonly
The exact location the token was taken from.
-
#type ⇒ Symbol
readonly
The type of the token.
-
#value ⇒ String
readonly
The lexeme that this token is associated.
Instance Method Summary collapse
-
#hash ⇒ Numeric
Creates a numeric representation of this class for hashing.
-
#initialize(type, value, location) ⇒ Token
constructor
Initialize the token.
-
#to_a ⇒ (Class, Symbol, String, Location)
Creates an array representation of this class.
Constructor Details
#initialize(type, value, location) ⇒ Token
Initialize the token. Freezes it after initializing.
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
#location ⇒ Location (readonly)
The exact location the token was taken from. This only spans one line.
24 25 26 |
# File 'lib/carbon/compiler/scanner/token.rb', line 24 def location @location end |
#type ⇒ Symbol (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`).
14 15 16 |
# File 'lib/carbon/compiler/scanner/token.rb', line 14 def type @type 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/carbon/compiler/scanner/token.rb', line 19 def value @value end |
Instance Method Details
#hash ⇒ Numeric
Creates a numeric representation of this class for hashing.
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.
44 45 46 |
# File 'lib/carbon/compiler/scanner/token.rb', line 44 def to_a @array ||= [self.class, @type, @value, @location].freeze end |