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 ⇒ Object
readonly
Link to the production to reference.
Instance Method Summary collapse
-
#==(other) ⇒ true / false
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 ⇒ Fixnum
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? ⇒ true / false
Check that the this object doesn't refer to any production.
Constructor Details
#initialize(target) ⇒ ProductionRef
Constructor
28 29 30 |
# File 'lib/sequitur/production_ref.rb', line 28 def initialize(target) bind_to(target) end |
Instance Attribute Details
#production ⇒ Object (readonly)
Link to the production to reference.
23 24 25 |
# File 'lib/sequitur/production_ref.rb', line 23 def production @production end |
Instance Method Details
#==(other) ⇒ true / false
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.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sequitur/production_ref.rb', line 59 def ==(other) return true if object_id == other.object_id if other.is_a?(ProductionRef) result = (production == other.production) else result = (production == other) end return result end |
#accept(aVisitor) ⇒ Object
Part of the 'visitee' role in the Visitor design pattern.
111 112 113 |
# File 'lib/sequitur/production_ref.rb', line 111 def accept(aVisitor) aVisitor.visit_prod_ref(self) end |
#bind_to(aProduction) ⇒ Object
Make this reference point to the given production.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sequitur/production_ref.rb', line 84 def bind_to(aProduction) return if aProduction == @production production.decr_refcount if production unless aProduction.kind_of?(Production) fail StandardError, "Illegal production type #{aProduction.class}" end @production = aProduction production.incr_refcount end |
#hash ⇒ Fixnum
Produce a hash value. A reference has no identity on its own, the method returns the hash value of the referenced production
76 77 78 79 |
# File 'lib/sequitur/production_ref.rb', line 76 def hash() fail StandardError, 'Nil production' if production.nil? return production.hash end |
#initialize_copy(orig) ⇒ Object
Copy constructor invoked by dup or clone methods.
39 40 41 42 |
# File 'lib/sequitur/production_ref.rb', line 39 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.
46 47 48 |
# File 'lib/sequitur/production_ref.rb', line 46 def to_s() return "#{production.object_id}" end |
#unbind ⇒ Object
Clear the reference to the target production.
97 98 99 100 |
# File 'lib/sequitur/production_ref.rb', line 97 def unbind() production.decr_refcount @production = nil end |
#unbound? ⇒ true / false
Check that the this object doesn't refer to any production.
105 106 107 |
# File 'lib/sequitur/production_ref.rb', line 105 def unbound?() return production.nil? end |