class Sequitur::ProductionRef
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 @example
# Given a production rule... prod = Sequitur::Production.new puts prod.refcount # outputs 0 # ... Build a reference to it ref = Sequitur::ProductionRef.new(prod) # ... Production reference count is updated... puts prod.refcount # outputs 1
Attributes
@return [Sequitur::Production] Link to the production to reference.
Public Class Methods
Source
# File lib/sequitur/production_ref.rb, line 26 def initialize(target) bind_to(target) end
Constructor @param target [Production, ProductionRef]
The production that is being referenced.
Public Instance Methods
Source
# 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
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.
@param other [Production, ProductionRef] @return [TrueClass, FalseClass]
Source
# File lib/sequitur/production_ref.rb, line 108 def accept(aVisitor) aVisitor.visit_prod_ref(self) end
Part of the ‘visitee’ role in the Visitor design pattern. @param aVisitor [Sequitur::GrammarVisitor] the visitor
Source
# 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
Make this reference point to the given production. @param aProduction [Production, ProductionRef] the production
to refer to
Source
# File lib/sequitur/production_ref.rb, line 71 def hash raise StandardError, 'Nil production' if production.nil? production.hash end
Produce a hash value.
A reference has no identity on its own, the method returns the hash value of the referenced production
@return [Integer] the hash value
Source
# File lib/sequitur/production_ref.rb, line 37 def initialize_copy(orig) @production = nil bind_to(orig.production) end
Copy constructor invoked by dup or clone methods. @param orig [ProductionRef] @example
prod = Sequitur::Production.new ref = Sequitur::ProductionRef.new(prod) copy_ref = ref.dup puts prod.refcount # outputs 2
Source
# File lib/sequitur/production_ref.rb, line 44 def to_s production.object_id.to_s end
Emit the text representation of a production reference. @return [String]
Source
# File lib/sequitur/production_ref.rb, line 94 def unbind production.decr_refcount @production = nil end
Clear the reference to the target production. return [NilClass]
Source
# File lib/sequitur/production_ref.rb, line 102 def unbound? production.nil? end
Check that the this object doesn’t refer to any production. @return [TrueClass, FalseClass] true when this object doesn’t
point to a production.