Class: Antelope::Generation::Recognizer::Rule
- Inherits:
-
Object
- Object
- Antelope::Generation::Recognizer::Rule
- Includes:
- Comparable
- Defined in:
- lib/antelope/generation/recognizer/rule.rb
Overview
Defines a rule. A rule has a corresponding production, and a position in that production. It also contains extra information for other reasons.
Instance Attribute Summary collapse
-
#block ⇒ String
readonly
deprecated
Deprecated.
Use Ace::Production#block instead.
-
#id ⇒ String, Numeric
The id for this rule.
-
#left ⇒ Ace::Token::Nonterminal
readonly
The left-hand side of the rule.
-
#lookahead ⇒ Set<Symbol>
The lookahead set for this specific rule.
-
#position ⇒ Numeric
readonly
The current position inside of the rule.
-
#precedence ⇒ Ace::Precedence
The precedence for this rule.
-
#production ⇒ Ace::Production
readonly
The associated production.
-
#right ⇒ Array<Ace::Token>
The right-hand side of the rule.
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric
Compares this rule to another object.
-
#===(other) ⇒ Numeric
Fuzzily compares this object to another object.
-
#active ⇒ Ace::Token
Returns the active token.
-
#clone ⇒ Rule
Produces a clone of the rule; any modifications made to the contents of that rule do not reflect the contents of this rule.
-
#final? ⇒ Boolean
Checks to see if this is the final rule, as in no rule can exist after this one; i.e.
-
#hash ⇒ Object
Generates a hash for this class.
-
#initialize(production, position, inherited = false) ⇒ Rule
constructor
Initialize the rule.
-
#inspect ⇒ String
Give a nice representation of the rule as a string.
-
#start? ⇒ Boolean
The complete opposite of #final? - it checks to see if this is the first rule, as in no rule can exist before this one; i.e.
-
#succ ⇒ Rule
Creates the rule after this one by incrementing the position by one.
-
#succ? ⇒ Boolean
Checks to see if a rule can exist after this one; i.e.
-
#to_a ⇒ Array<(Ace::Token::Nonterminal, Array<Ace::Token>, Numeric)>
Creates an array representation of this class.
-
#to_s(dot = true) ⇒ String
Give a nicer representation of the rule as a string.
Constructor Details
#initialize(production, position, inherited = false) ⇒ Rule
Initialize the rule.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 67 def initialize(production, position, inherited = false) @left = production.label.dup @position = position @lookahead = Set.new @precedence = production.prec @production = production @block = production.block @id = SecureRandom.hex if inherited @left, @right = inherited else @right = production.items.map(&:dup) end end |
Instance Attribute Details
#block ⇒ String (readonly)
Use Ace::Production#block instead.
The block to be executed on production match.
33 34 35 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 33 def block @block end |
#id ⇒ String, Numeric
The id for this rule. Initialy, this is set to a string of hexadecimal characters; after construction of all states, however, it is a number.
46 47 48 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 46 def id @id end |
#left ⇒ Ace::Token::Nonterminal (readonly)
The left-hand side of the rule.
17 18 19 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 17 def left @left end |
#lookahead ⇒ Set<Symbol>
The lookahead set for this specific rule. Contains nothing unless #final? returns true.
39 40 41 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 39 def lookahead @lookahead end |
#position ⇒ Numeric (readonly)
The current position inside of the rule.
27 28 29 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 27 def position @position end |
#precedence ⇒ Ace::Precedence
The precedence for this rule.
51 52 53 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 51 def precedence @precedence end |
#production ⇒ Ace::Production (readonly)
The associated production.
56 57 58 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 56 def production @production end |
#right ⇒ Array<Ace::Token>
The right-hand side of the rule.
22 23 24 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 22 def right @right end |
Instance Method Details
#<=>(other) ⇒ Numeric
Compares this rule to another object. If the other object is not a rule, it delegates the comparison. Otherwise, it converts both this and the other rule into arrays and compares the result.
155 156 157 158 159 160 161 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 155 def <=>(other) if other.is_a? Rule to_a <=> other.to_a else super end end |
#===(other) ⇒ Numeric
Fuzzily compares this object to another object. If the other object is not a rule, it delegates the comparison. Otherwise, it fuzzily compares the left and right sides.
169 170 171 172 173 174 175 176 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 169 def ===(other) if other.is_a? Rule left === other.left and right.each_with_index. all? { |e, i| e === other.right[i] } else super end end |
#active ⇒ Ace::Token
Returns the active token. If there is no active token, it returns a blank Ace::Token.
108 109 110 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 108 def active right[position] or Ace::Token.new(nil) end |
#clone ⇒ Rule
Produces a clone of the rule; any modifications made to the contents of that rule do not reflect the contents of this rule.
183 184 185 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 183 def clone Rule.new(production, position) end |
#final? ⇒ Boolean
Checks to see if this is the final rule, as in no rule can exist after this one; i.e. the position is equal to the size of the right side.
135 136 137 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 135 def final? !succ? 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.
193 194 195 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 193 def hash to_a.hash end |
#inspect ⇒ String
Give a nice representation of the rule as a string.
86 87 88 89 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 86 def inspect "#<#{self.class} id=#{id} left=#{left} " \ "right=[#{right.join(" ")}] position=#{position}>" end |
#start? ⇒ Boolean
The complete opposite of #final? - it checks to see if this is the first rule, as in no rule can exist before this one; i.e. the position is zero.
144 145 146 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 144 def start? position.zero? end |
#succ ⇒ Rule
Creates the rule after this one by incrementing the position by one. #succ? should be called to make sure that this rule exists.
117 118 119 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 117 def succ Rule.new(production, position + 1, [left, right]) end |
#succ? ⇒ Boolean
Checks to see if a rule can exist after this one; i.e. the position is not equal to the size of the right side of the rule.
126 127 128 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 126 def succ? right.size > position end |
#to_a ⇒ Array<(Ace::Token::Nonterminal, Array<Ace::Token>, Numeric)>
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.
205 206 207 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 205 def to_a [left, right, position].flatten end |
#to_s(dot = true) ⇒ String
Give a nicer representation of the rule as a string. Shows the id of the rule, the precedence, and the actual production; if the given argument is true, it will show a dot to show the position of the rule.
98 99 100 101 102 |
# File 'lib/antelope/generation/recognizer/rule.rb', line 98 def to_s(dot = true) "#{id}/#{precedence.type.to_s[0]}#{precedence.level}: " \ "#{left} → #{right[0, position].join(" ")}" \ "#{" • " if dot}#{right[position..-1].join(" ")}" end |