Class: Sequitur::ProductionRef

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

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ ProductionRef

Constructor [target] The production that is being referenced.



19
20
21
# File 'lib/sequitur/production_ref.rb', line 19

def initialize(target)
  bind_to(target)
end

Instance Attribute Details

#productionObject (readonly)

Link to the production to reference



15
16
17
# File 'lib/sequitur/production_ref.rb', line 15

def production
  @production
end

Instance Method Details

#==(other) ⇒ Object

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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sequitur/production_ref.rb', line 41

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

#bind_to(aProduction) ⇒ Object

Make this reference points to the given production



63
64
65
66
67
68
69
70
71
72
# File 'lib/sequitur/production_ref.rb', line 63

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

#hashObject

Generates a Fixnum value as hash value. As a reference has no identity on its own, the method returns the hash value of the referenced production



57
58
59
60
# File 'lib/sequitur/production_ref.rb', line 57

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



24
25
26
27
# File 'lib/sequitur/production_ref.rb', line 24

def initialize_copy(orig)
  @production = nil
  bind_to(orig.production)
end

#to_sObject Also known as: to_string

Return the text representation of a production reference.



30
31
32
# File 'lib/sequitur/production_ref.rb', line 30

def to_s()
  return "#{production.object_id}"
end

#unbindObject

Clear the reference to the target production



75
76
77
78
# File 'lib/sequitur/production_ref.rb', line 75

def unbind()
  production.decr_refcount
  @production = nil
end

#unbound?Boolean

Check that the this object doesn't refer to any production.

Returns:

  • (Boolean)


81
82
83
# File 'lib/sequitur/production_ref.rb', line 81

def unbound?()
  return production.nil?
end