Class: Calyx::Grammar
- Inherits:
-
Object
- Object
- Calyx::Grammar
- Defined in:
- lib/calyx.rb
Defined Under Namespace
Modules: Production
Class Attribute Summary collapse
-
.registry ⇒ Object
Returns the value of attribute registry.
Class Method Summary collapse
- .construct_rule(productions) ⇒ Object
- .inherit_registry(rules) ⇒ Object
- .inherited(subclass) ⇒ Object
- .rule(name, *productions, &production) ⇒ Object
- .start(*productions, &production) ⇒ Object
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(seed = nil) ⇒ Grammar
constructor
A new instance of Grammar.
- #registry ⇒ Object
Constructor Details
#initialize(seed = nil) ⇒ Grammar
Returns a new instance of Grammar.
137 138 139 140 141 |
# File 'lib/calyx.rb', line 137 def initialize(seed=nil) @seed = seed @seed = Time.new.to_i unless @seed srand(@seed) end |
Class Attribute Details
.registry ⇒ Object
Returns the value of attribute registry.
4 5 6 |
# File 'lib/calyx.rb', line 4 def registry @registry end |
Class Method Details
.construct_rule(productions) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/calyx.rb', line 23 def construct_rule(productions) if productions.first.is_a?(Enumerable) Production::WeightedChoices.parse(productions) else Production::Choices.parse(productions) end end |
.inherit_registry(rules) ⇒ Object
14 15 16 17 |
# File 'lib/calyx.rb', line 14 def inherit_registry(rules) @registry ||= {} @registry.merge!(rules || {}) end |
.inherited(subclass) ⇒ Object
19 20 21 |
# File 'lib/calyx.rb', line 19 def inherited(subclass) subclass.inherit_registry(@registry) end |
.rule(name, *productions, &production) ⇒ Object
10 11 12 |
# File 'lib/calyx.rb', line 10 def rule(name, *productions, &production) registry[name.to_sym] = construct_rule(productions) end |
.start(*productions, &production) ⇒ Object
6 7 8 |
# File 'lib/calyx.rb', line 6 def start(*productions, &production) registry[:start] = construct_rule(productions) end |
Instance Method Details
#generate ⇒ Object
147 148 149 |
# File 'lib/calyx.rb', line 147 def generate registry[:start].evaluate(registry) end |
#registry ⇒ Object
143 144 145 |
# File 'lib/calyx.rb', line 143 def registry self.class.registry end |