Class: Sequitur::ProductionRef
- Inherits:
-
Object
- Object
- Sequitur::ProductionRef
- Defined in:
- lib/sequitur/production_ref.rb
Overview
A production reference is a grammar symbol that may appear in the right-hand side of a production P1 and that refers to a production P2. Every time a production P2 appears in the left-hand side of production P1, this is implemented by inserting a production reference to P2 in the appropriate position in the RHS of P1. In the literature, production references are also called non terminal symbols
Instance Attribute Summary collapse
-
#production ⇒ Sequitur::Production
readonly
Link to the production to reference.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Equality testing.
-
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in the Visitor design pattern.
-
#bind_to(aProduction) ⇒ Object
Make this reference point to the given production.
-
#hash ⇒ Integer
Produce a hash value.
-
#initialize(target) ⇒ ProductionRef
constructor
Constructor.
-
#initialize_copy(orig) ⇒ Object
Copy constructor invoked by dup or clone methods.
-
#to_s ⇒ String
(also: #to_string)
Emit the text representation of a production reference.
-
#unbind ⇒ Object
Clear the reference to the target production.
-
#unbound? ⇒ TrueClass, FalseClass
Check that the this object doesn't refer to any production.
Constructor Details
#initialize(target) ⇒ ProductionRef
Constructor
26 27 28 |
# File 'lib/sequitur/production_ref.rb', line 26 def initialize(target) bind_to(target) end |
Instance Attribute Details
#production ⇒ Sequitur::Production (readonly)
Returns Link to the production to reference.
21 22 23 |
# File 'lib/sequitur/production_ref.rb', line 21 def production @production end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
Equality testing. A production ref is equal to another one when its refers to the same production or when it is compared to the production it refers to.
56 57 58 59 60 61 62 63 64 |
# File 'lib/sequitur/production_ref.rb', line 56 def ==(other) return true if object_id == other.object_id if other.is_a?(ProductionRef) production == other.production else production == other end end |
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in the Visitor design pattern.
108 109 110 |
# File 'lib/sequitur/production_ref.rb', line 108 def accept(aVisitor) aVisitor.visit_prod_ref(self) end |
#bind_to(aProduction) ⇒ Object
Make this reference point to the given production.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sequitur/production_ref.rb', line 80 def bind_to(aProduction) return if aProduction == @production production&.decr_refcount unless aProduction.is_a?(Production) raise StandardError, "Illegal production type #{aProduction.class}" end @production = aProduction production.incr_refcount end |
#hash ⇒ Integer
Produce a hash value. A reference has no identity on its own, the method returns the hash value of the referenced production
71 72 73 74 75 |
# File 'lib/sequitur/production_ref.rb', line 71 def hash raise StandardError, 'Nil production' if production.nil? production.hash end |
#initialize_copy(orig) ⇒ Object
Copy constructor invoked by dup or clone methods.
37 38 39 40 |
# File 'lib/sequitur/production_ref.rb', line 37 def initialize_copy(orig) @production = nil bind_to(orig.production) end |
#to_s ⇒ String Also known as: to_string
Emit the text representation of a production reference.
44 45 46 |
# File 'lib/sequitur/production_ref.rb', line 44 def to_s production.object_id.to_s end |
#unbind ⇒ Object
Clear the reference to the target production. return [NilClass]
94 95 96 97 |
# File 'lib/sequitur/production_ref.rb', line 94 def unbind production.decr_refcount @production = nil end |
#unbound? ⇒ TrueClass, FalseClass
Check that the this object doesn't refer to any production.
102 103 104 |
# File 'lib/sequitur/production_ref.rb', line 102 def unbound? production.nil? end |