Class: BracketNotation::Token
- Inherits:
-
Object
- Object
- BracketNotation::Token
- Defined in:
- lib/bracket_notation/token.rb
Overview
This class represents a token in a stream of characters. All tokens have a type, and some of them (e.g. NAME
tokens) have corresponding values.
Constant Summary collapse
- LBRACKET =
Constants that identify the different types of tokens
"LBRACKET"
- RBRACKET =
"RBRACKET"
- NAME =
"NAME"
- EOL =
"EOL"
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.EOL ⇒ Object
Initializes and returns a new token of type
EOL
. -
.LBRACKET ⇒ Object
Initializes and returns a new token of type
LBRACKET
. -
.NAME(value) ⇒ Object
Initializes and returns a new token of type
NAME
with the given value. -
.RBRACKET ⇒ Object
Initializes and returns a new token of type
RBRACKET
.
Instance Method Summary collapse
-
#==(rvalue) ⇒ Object
Compares the receiver with another object, returning true only if the other object is also an instance of Token, and only if the two tokens share the same
type
andvalue
. -
#initialize(type, value = nil) ⇒ Token
constructor
Saves the token type, as well as an optional value.
-
#inspect ⇒ Object
Provides a human-friendly string representation of a token instance.
Constructor Details
#initialize(type, value = nil) ⇒ Token
Saves the token type, as well as an optional value.
55 56 57 58 |
# File 'lib/bracket_notation/token.rb', line 55 def initialize(type, value = nil) @type = type @value = value end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
33 34 35 |
# File 'lib/bracket_notation/token.rb', line 33 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
34 35 36 |
# File 'lib/bracket_notation/token.rb', line 34 def value @value end |
Class Method Details
.EOL ⇒ Object
Initializes and returns a new token of type EOL
.
52 |
# File 'lib/bracket_notation/token.rb', line 52 def self.EOL; return self.new(EOL); end |
.LBRACKET ⇒ Object
Initializes and returns a new token of type LBRACKET
.
43 |
# File 'lib/bracket_notation/token.rb', line 43 def self.LBRACKET; return self.new(LBRACKET); end |
.NAME(value) ⇒ Object
Initializes and returns a new token of type NAME
with the given value.
49 |
# File 'lib/bracket_notation/token.rb', line 49 def self.NAME(value); return self.new(NAME, value); end |
.RBRACKET ⇒ Object
Initializes and returns a new token of type RBRACKET
.
46 |
# File 'lib/bracket_notation/token.rb', line 46 def self.RBRACKET; return self.new(RBRACKET); end |
Instance Method Details
#==(rvalue) ⇒ Object
Compares the receiver with another object, returning true only if the other object is also an instance of Token, and only if the two tokens share the same type
and value
.
71 72 73 74 75 76 77 |
# File 'lib/bracket_notation/token.rb', line 71 def ==(rvalue) if self.class != rvalue.class return super end return @type == rvalue.type && @value == rvalue.value end |
#inspect ⇒ Object
Provides a human-friendly string representation of a token instance.
61 62 63 64 65 66 |
# File 'lib/bracket_notation/token.rb', line 61 def inspect # :nodoc: output = "#{@type}" output << " \"#{@value}\"" unless @value.nil? return output end |