Class: Calyx::Registry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



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

def initialize
  @rules = {}
  @transforms = {}
  @modifier = Modifier.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments) ⇒ Object



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

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

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

#transformsObject (readonly)

Returns the value of attribute transforms.



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

def transforms
  @transforms
end

Instance Method Details

#combine(registry) ⇒ Object



51
52
53
# File 'lib/calyx/registry.rb', line 51

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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/calyx/registry.rb', line 55

def evaluate(start_symbol=:start, rules_map={})
  reset_evaluation_context

  rules_map.each do |key, value|
    if rules.key?(key.to_sym)
      raise "Rule already declared in grammar: #{key}"
    end

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

  expansion = expand(start_symbol)

  if expansion.respond_to?(:evaluate)
    [start_symbol, expansion.evaluate]
  else
    raise RuleNotFound.new(start_symbol)
  end
end

#expand(symbol) ⇒ Object



35
36
37
# File 'lib/calyx/registry.rb', line 35

def expand(symbol)
  rules[symbol] || context[symbol]
end

#filter(name, callable = nil, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/calyx/registry.rb', line 23

def filter(name, callable=nil, &block)
  if block_given?
    transforms[name.to_sym] = block
  else
    transforms[name.to_sym] = callable
  end
end

#mapping(name, pairs) ⇒ Object



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

def mapping(name, pairs)
  transforms[name.to_sym] = construct_mapping(pairs)
end

#memoize_expansion(symbol) ⇒ Object



47
48
49
# File 'lib/calyx/registry.rb', line 47

def memoize_expansion(symbol)
  memos[symbol] ||= expand(symbol).evaluate
end

#modifier(name) ⇒ Object



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

def modifier(name)
  @modifier.extend_with(name)
end

#rule(name, *productions) ⇒ Object



31
32
33
# File 'lib/calyx/registry.rb', line 31

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

#transform(name, value) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/calyx/registry.rb', line 39

def transform(name, value)
  if transforms.key?(name)
    transforms[name].call(value)
  else
    @modifier.transform(name, value)
  end
end