Class: Antelope::Ace::Token Abstract

Inherits:
Object
  • Object
show all
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 is abstract.

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

Epsilon, Nonterminal, Terminal

Defined Under Namespace

Classes: Epsilon, Error, Nonterminal, Terminal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = nil) ⇒ Token

Initialize.

Parameters:

  • name (Symbol)

    the name of the token.

  • value (String?) (defaults to: nil)

    the value of the token. This is only used in output representation to the developer.



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

#fromRecognizer::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.

Returns:

  • (Recognizer::State)


27
28
29
# File 'lib/antelope/ace/token.rb', line 27

def from
  @from
end

#nameSymbol (readonly)

The name of the token.

Returns:

  • (Symbol)


20
21
22
# File 'lib/antelope/ace/token.rb', line 20

def name
  @name
end

#toRecognizer::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.

Returns:

  • (Recognizer::State)


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.

Parameters:

  • other (Object)

    the other object to compare.

Returns:

  • (Numeric)


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.

Parameters:

  • other (Object)

    the other object to compare.

Returns:

  • (Boolean)

    if they are equal.



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

This method is abstract.

Whether or not the token is an epsilon token.

Returns:

  • (Boolean)


70
71
72
# File 'lib/antelope/ace/token.rb', line 70

def epsilon?
  false
end

#error?Boolean

This method is abstract.

Whether or not the token is an error token.

Returns:

  • (Boolean)


78
79
80
# File 'lib/antelope/ace/token.rb', line 78

def error?
  false
end

#hashObject

Note:

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.

Returns:

  • (Object)


154
155
156
# File 'lib/antelope/ace/token.rb', line 154

def hash
  to_a.hash
end

#nonterminal?Boolean

This method is abstract.

Whether or not the token is a nonterminal.

Returns:

  • (Boolean)


62
63
64
# File 'lib/antelope/ace/token.rb', line 62

def nonterminal?
  false
end

#terminal?Boolean

This method is abstract.

Whether or not the token is a terminal.

Returns:

  • (Boolean)


54
55
56
# File 'lib/antelope/ace/token.rb', line 54

def terminal?
  false
end

#to_aArray<(Recognizer::State, Recognizer::State, Class, Symbol, String?)>

Note:

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.

Returns:

  • (Array<(Recognizer::State, Recognizer::State, Class, Symbol, String?)>)


166
167
168
# File 'lib/antelope/ace/token.rb', line 166

def to_a
  [to, from, self.class, name, @value]
end

#to_sString

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.

Returns:

  • (String)

    the string representation.

See Also:



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_transitionsToken

Creates a new token without to or from states.

Returns:



144
145
146
# File 'lib/antelope/ace/token.rb', line 144

def without_transitions
  self.class.new(name, @value)
end