Class: Calyx::Rule
- Inherits:
-
Object
- Object
- Calyx::Rule
- Defined in:
- lib/calyx/rule.rb
Overview
Represents a named rule connected to a tree of productions that can be evaluated and a trace which represents where the rule was declared.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#trace ⇒ Object
readonly
Returns the value of attribute trace.
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
- #evaluate(options) ⇒ Object
-
#initialize(name, productions, trace) ⇒ Rule
constructor
A new instance of Rule.
- #size ⇒ Object
Constructor Details
#initialize(name, productions, trace) ⇒ Rule
Returns a new instance of Rule.
28 29 30 31 32 |
# File 'lib/calyx/rule.rb', line 28 def initialize(name, productions, trace) @name = name.to_sym @tree = productions @trace = trace end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/calyx/rule.rb', line 26 def name @name end |
#trace ⇒ Object (readonly)
Returns the value of attribute trace.
26 27 28 |
# File 'lib/calyx/rule.rb', line 26 def trace @trace end |
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
26 27 28 |
# File 'lib/calyx/rule.rb', line 26 def tree @tree end |
Class Method Details
.build_ast(productions, registry) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/calyx/rule.rb', line 5 def self.build_ast(productions, registry) if productions.first.is_a?(Hash) # TODO: test that key is a string if productions.first.first.last.is_a?(String) # If value of the production is a strings then this is a # paired mapping production. Production::AffixTable.parse(productions.first, registry) else # Otherwise, we assume this is a weighted choice declaration and # convert the hash to an array Syntax::WeightedChoices.parse(productions.first.to_a, registry) end elsif productions.first.is_a?(Enumerable) # TODO: this needs to change to support attributed/tagged grammars Syntax::WeightedChoices.parse(productions, registry) else Syntax::Choices.parse(productions, registry) end end |
Instance Method Details
#evaluate(options) ⇒ Object
38 39 40 |
# File 'lib/calyx/rule.rb', line 38 def evaluate() tree.evaluate() end |
#size ⇒ Object
34 35 36 |
# File 'lib/calyx/rule.rb', line 34 def size tree.size end |