Class: Synthra::Parser::Token
- Inherits:
-
Object
- Object
- Synthra::Parser::Token
- Defined in:
- lib/synthra/parser/tokens.rb
Overview
Represents a single token from the lexer
A Token captures the type, value, and source location of a syntax element in the DSL. Tokens are produced by the Lexer and consumed by the Parser.
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality with another token.
-
#hash ⇒ Integer
Hash code for use in Hash keys and Sets.
-
#initialize(type, value, line:, column:) ⇒ Token
constructor
Create a new Token.
-
#inspect ⇒ String
Inspect output (same as to_s).
-
#to_s ⇒ String
String representation for debugging.
-
#type?(expected_type) ⇒ Boolean
Check if token is of a specific type.
Constructor Details
#initialize(type, value, line:, column:) ⇒ Token
Create a new Token
384 385 386 387 388 389 |
# File 'lib/synthra/parser/tokens.rb', line 384 def initialize(type, value, line:, column:) @type = type @value = value @line = line @column = column end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
371 372 373 |
# File 'lib/synthra/parser/tokens.rb', line 371 def column @column end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
365 366 367 |
# File 'lib/synthra/parser/tokens.rb', line 365 def line @line end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
353 354 355 |
# File 'lib/synthra/parser/tokens.rb', line 353 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
359 360 361 |
# File 'lib/synthra/parser/tokens.rb', line 359 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality with another token
Two tokens are equal if they have the same type and value. Line and column are not considered for equality.
426 427 428 429 430 |
# File 'lib/synthra/parser/tokens.rb', line 426 def ==(other) return false unless other.is_a?(Token) type == other.type && value == other.value end |
#hash ⇒ Integer
Hash code for use in Hash keys and Sets
Based on type and value only (not line/column).
442 443 444 |
# File 'lib/synthra/parser/tokens.rb', line 442 def hash [type, value].hash end |
#inspect ⇒ String
Inspect output (same as to_s)
408 409 410 |
# File 'lib/synthra/parser/tokens.rb', line 408 def inspect to_s end |
#to_s ⇒ String
String representation for debugging
399 400 401 |
# File 'lib/synthra/parser/tokens.rb', line 399 def to_s "Token(#{type}, #{value.inspect}, line: #{line}, col: #{column})" end |
#type?(expected_type) ⇒ Boolean
Check if token is of a specific type
456 457 458 |
# File 'lib/synthra/parser/tokens.rb', line 456 def type?(expected_type) type == expected_type end |