Class: EBNF::LL1::Lexer::Token
- Inherits:
-
Object
- Object
- EBNF::LL1::Lexer::Token
- Defined in:
- lib/ebnf/ll1/lexer.rb
Overview
Represents a lexer token.
Instance Attribute Summary collapse
-
#lineno ⇒ Integer
readonly
The line number where the token was encountered.
-
#options ⇒ Hash
readonly
Any additional options for the token.
-
#type ⇒ Symbol
readonly
The token’s symbol type.
-
#value ⇒ String
readonly
The token’s value.
Instance Method Summary collapse
-
#===(value) ⇒ Boolean
Returns ‘true` if the given `value` matches either the type or value of this token.
-
#[](key) ⇒ Object
Returns the attribute named by ‘key`.
-
#initialize(type, value, options = {}) ⇒ Token
constructor
Initializes a new token instance.
-
#inspect ⇒ String
Returns a developer-friendly representation of this token.
-
#representation ⇒ Object
Returns type, if not nil, otherwise value.
-
#to_a ⇒ Array
Returns an array representation of this token.
-
#to_hash ⇒ Hash
Returns a hash table representation of this token.
-
#to_s ⇒ Object
Readable version of token.
Constructor Details
#initialize(type, value, options = {}) ⇒ Token
Initializes a new token instance.
387 388 389 390 391 392 |
# File 'lib/ebnf/ll1/lexer.rb', line 387 def initialize(type, value, = {}) @type = type.to_s.to_sym if type @value = value.to_s @options = .dup @lineno = @options.delete(:lineno) end |
Instance Attribute Details
#lineno ⇒ Integer (readonly)
The line number where the token was encountered.
372 373 374 |
# File 'lib/ebnf/ll1/lexer.rb', line 372 def lineno @lineno end |
#options ⇒ Hash (readonly)
Any additional options for the token.
378 379 380 |
# File 'lib/ebnf/ll1/lexer.rb', line 378 def @options end |
#type ⇒ Symbol (readonly)
The token’s symbol type.
360 361 362 |
# File 'lib/ebnf/ll1/lexer.rb', line 360 def type @type end |
#value ⇒ String (readonly)
The token’s value.
366 367 368 |
# File 'lib/ebnf/ll1/lexer.rb', line 366 def value @value end |
Instance Method Details
#===(value) ⇒ Boolean
Returns ‘true` if the given `value` matches either the type or value of this token.
420 421 422 423 424 425 426 427 428 |
# File 'lib/ebnf/ll1/lexer.rb', line 420 def ===(value) case value when Symbol value == @type when ::String @value == (@options[:case_insensitive] ? value.to_s.downcase : value.to_s) else value == @value end end |
#[](key) ⇒ Object
Returns the attribute named by ‘key`.
399 400 401 402 403 404 405 406 |
# File 'lib/ebnf/ll1/lexer.rb', line 399 def [](key) key = key.to_s.to_sym unless key.is_a?(Integer) || key.is_a?(Symbol) case key when 0, :type then @type when 1, :value then @value else nil end end |
#inspect ⇒ String
Returns a developer-friendly representation of this token.
462 463 464 |
# File 'lib/ebnf/ll1/lexer.rb', line 462 def inspect "#{@value.inspect}#{'(' + @type.to_s + ')' if @type}" end |
#representation ⇒ Object
Returns type, if not nil, otherwise value
446 447 448 |
# File 'lib/ebnf/ll1/lexer.rb', line 446 def representation @type ? @type : @value end |
#to_a ⇒ Array
Returns an array representation of this token.
454 455 456 |
# File 'lib/ebnf/ll1/lexer.rb', line 454 def to_a [@type, @value] end |
#to_hash ⇒ Hash
Returns a hash table representation of this token.
434 435 436 |
# File 'lib/ebnf/ll1/lexer.rb', line 434 def to_hash {:type => @type, :value => @value} end |
#to_s ⇒ Object
Readable version of token
440 441 442 |
# File 'lib/ebnf/ll1/lexer.rb', line 440 def to_s @type ? @type.inspect : @value end |