Class: Antelope::Ace::Token Abstract
- Inherits:
-
Object
- Object
- Antelope::Ace::Token
- Includes:
- Comparable
- Defined in:
- lib/antelope/ace/token.rb,
lib/antelope/ace/token/error.rb,
lib/antelope/ace/token/epsilon.rb,
lib/antelope/ace/token/terminal.rb,
lib/antelope/ace/token/nonterminal.rb
Overview
This class should be inherited to define a real token. A base class does not match any token; however, any token can match the base class.
Defines a token type for productions/rules.
Direct Known Subclasses
Defined Under Namespace
Classes: Epsilon, Error, Nonterminal, Terminal
Instance Attribute Summary collapse
-
#from ⇒ Recognizer::State
The from state that this token is transitioned from.
-
#name ⇒ Symbol
readonly
The name of the token.
-
#to ⇒ Recognizer::State
The to state that this token is transitioned to.
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric
Compares this class to any other object.
-
#===(other) ⇒ Boolean
Compares this class and another object, fuzzily.
-
#epsilon? ⇒ Boolean
abstract
Whether or not the token is an epsilon token.
-
#error? ⇒ Boolean
abstract
Whether or not the token is an error token.
-
#hash ⇒ Object
Generates a hash for this class.
-
#initialize(name, value = nil) ⇒ Token
constructor
Initialize.
-
#nonterminal? ⇒ Boolean
abstract
Whether or not the token is a nonterminal.
-
#terminal? ⇒ Boolean
abstract
Whether or not the token is a terminal.
-
#to_a ⇒ Array<(Recognizer::State, Recognizer::State, Class, Symbol, String?)>
Creates an array representation of this class.
-
#to_s ⇒ String
Gives a string representation of the token.
-
#without_transitions ⇒ Token
Creates a new token without to or from states.
Constructor Details
#initialize(name, value = nil) ⇒ Token
Initialize.
41 42 43 44 45 46 |
# File 'lib/antelope/ace/token.rb', line 41 def initialize(name, value = nil) @name = name @value = value @from = nil @to = nil end |
Instance Attribute Details
#from ⇒ Recognizer::State
The from state that this token is transitioned from. This is the source. This is used in the constructor in order to handle lookahead sets.
27 28 29 |
# File 'lib/antelope/ace/token.rb', line 27 def from @from end |
#name ⇒ Symbol (readonly)
The name of the token.
20 21 22 |
# File 'lib/antelope/ace/token.rb', line 20 def name @name end |
#to ⇒ Recognizer::State
The to state that this token is transitioned to. This is the destination. This is used in the constructor in order to handle lookahead sets.
34 35 36 |
# File 'lib/antelope/ace/token.rb', line 34 def to @to end |
Instance Method Details
#<=>(other) ⇒ Numeric
Compares this class to any other object. If the other object is a token, it converts both this class and the other object to an array and compares the array. Otherwise, it delegates the comparison.
118 119 120 121 122 123 124 |
# File 'lib/antelope/ace/token.rb', line 118 def <=>(other) if other.is_a? Token to_a <=> other.to_a else super end end |
#===(other) ⇒ Boolean
Compares this class and another object, fuzzily. If the other object is a token, it removes the transitions (to and from) on both objects and compares them like that. Otherwise, it delegates the comparison.
133 134 135 136 137 138 139 |
# File 'lib/antelope/ace/token.rb', line 133 def ===(other) if other.is_a? Token without_transitions == other.without_transitions else super end end |
#epsilon? ⇒ Boolean
Whether or not the token is an epsilon token.
70 71 72 |
# File 'lib/antelope/ace/token.rb', line 70 def epsilon? false end |
#error? ⇒ Boolean
Whether or not the token is an error token.
78 79 80 |
# File 'lib/antelope/ace/token.rb', line 78 def error? false end |
#hash ⇒ Object
This is not intended for use. It is only defined to be compatible with Hashs (and by extension, Sets).
Generates a hash for this class.
154 155 156 |
# File 'lib/antelope/ace/token.rb', line 154 def hash to_a.hash end |
#nonterminal? ⇒ Boolean
Whether or not the token is a nonterminal.
62 63 64 |
# File 'lib/antelope/ace/token.rb', line 62 def nonterminal? false end |
#terminal? ⇒ Boolean
Whether or not the token is a terminal.
54 55 56 |
# File 'lib/antelope/ace/token.rb', line 54 def terminal? false end |
#to_a ⇒ Array<(Recognizer::State, Recognizer::State, Class, Symbol, String?)>
This is not intended for use. It is only defined to make equality checking easier, and to create a hash.
Creates an array representation of this class.
166 167 168 |
# File 'lib/antelope/ace/token.rb', line 166 def to_a [to, from, self.class, name, @value] end |
#to_s ⇒ String
Gives a string representation of the token. The output is
formatted like so: <data>["(" [<from_id>][:<to_id>] ")"],
where <data> is either the value (if it's non-nil) or the
name, <from_id> is the from state id, and <to_id> is the
to state id. The last part of the format is optional; if
neither the from state or to state is non-nil, it's non-
existant.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/antelope/ace/token.rb', line 94 def to_s buf = if @value @value.inspect else @name.to_s end if from or to buf << "(" buf << "#{from.id}" if from buf << ":#{to.id}" if to buf << ")" end buf end |
#without_transitions ⇒ Token
Creates a new token without to or from states.
144 145 146 |
# File 'lib/antelope/ace/token.rb', line 144 def without_transitions self.class.new(name, @value) end |