Class: Calyx::Grammar
- Inherits:
-
Object
show all
- Defined in:
- lib/calyx/grammar.rb,
lib/calyx/grammar/registry.rb,
lib/calyx/grammar/production/concat.rb,
lib/calyx/grammar/production/choices.rb,
lib/calyx/grammar/production/terminal.rb,
lib/calyx/grammar/production/expression.rb,
lib/calyx/grammar/production/non_terminal.rb,
lib/calyx/grammar/production/weighted_choices.rb
Defined Under Namespace
Modules: Production
Classes: Registry
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(seed = nil, &block) ⇒ Grammar
Returns a new instance of Grammar.
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/calyx/grammar.rb', line 25
def initialize(seed=nil, &block)
@seed = seed || Random.new_seed
srand(@seed)
if block_given?
@registry = Registry.new
@registry.instance_eval(&block)
else
@registry = self.class.registry
end
end
|
Class Method Details
.inherit_registry(child_registry) ⇒ Object
16
17
18
|
# File 'lib/calyx/grammar.rb', line 16
def inherit_registry(child_registry)
registry.combine(child_registry) unless child_registry.nil?
end
|
.inherited(subclass) ⇒ Object
20
21
22
|
# File 'lib/calyx/grammar.rb', line 20
def inherited(subclass)
subclass.inherit_registry(registry)
end
|
.method_missing(name, *productions) ⇒ Object
12
13
14
|
# File 'lib/calyx/grammar.rb', line 12
def method_missing(name, *productions)
registry.rule(name, *productions)
end
|
.registry ⇒ Object
4
5
6
|
# File 'lib/calyx/grammar.rb', line 4
def registry
@registry ||= Registry.new
end
|
.rule(name, *productions, &production) ⇒ Object
8
9
10
|
# File 'lib/calyx/grammar.rb', line 8
def rule(name, *productions, &production)
registry.rule(name, *productions)
end
|
Instance Method Details
#generate(*args) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/calyx/grammar.rb', line 37
def generate(*args)
start_symbol = :start
rules_map = {}
args.each do |arg|
start_symbol = arg if arg.class == Symbol
rules_map = arg if arg.class == Hash
end
@registry.evaluate(start_symbol, rules_map)
end
|