Class: Calyx::Grammar::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx/grammar/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



6
7
8
# File 'lib/calyx/grammar/registry.rb', line 6

def initialize
  @rules = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments) ⇒ Object



10
11
12
# File 'lib/calyx/grammar/registry.rb', line 10

def method_missing(name, *arguments)
  rule(name, *arguments)
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



4
5
6
# File 'lib/calyx/grammar/registry.rb', line 4

def rules
  @rules
end

Instance Method Details

#combine(registry) ⇒ Object



22
23
24
# File 'lib/calyx/grammar/registry.rb', line 22

def combine(registry)
  @rules = rules.merge(registry.rules)
end

#evaluate(start_symbol = :start, context = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/calyx/grammar/registry.rb', line 26

def evaluate(start_symbol=:start, context={})
  duplicate_registry = Marshal.load(Marshal.dump(self))
  duplicate_rules = duplicate_registry.rules
  context.each do |key, value|
    if duplicate_rules.key?(key.to_sym)
      raise "Rule already declared in grammar: #{key}"
    end

    duplicate_rules[key.to_sym] = if value.is_a?(Array)
      Production::Choices.parse(value, duplicate_registry)
    else
      Production::Concat.parse(value.to_s, duplicate_registry)
    end
  end

  duplicate_rules[start_symbol].evaluate
end

#expand(symbol) ⇒ Object



18
19
20
# File 'lib/calyx/grammar/registry.rb', line 18

def expand(symbol)
  @rules[symbol]
end

#rule(name, *productions, &production) ⇒ Object



14
15
16
# File 'lib/calyx/grammar/registry.rb', line 14

def rule(name, *productions, &production)
  @rules[name.to_sym] = construct_rule(productions)
end