Class: Calyx::Production::AffixTable

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

Overview

A type of production rule representing a bidirectional dictionary of mapping pairs that can be used as a substitution table in template expressions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ AffixTable

%y prefix: nil, suffix: ‘ies’



19
20
21
22
23
24
25
26
27
28
# File 'lib/calyx/production/affix_table.rb', line 19

def initialize(mapping)
  @lhs_index = PrefixTree.new
  @rhs_index = PrefixTree.new

  @lhs_list = mapping.keys
  @rhs_list = mapping.values

  @lhs_index.add_all(@lhs_list)
  @rhs_index.add_all(@rhs_list)
end

Class Method Details

.parse(productions, registry) ⇒ Object



7
8
9
10
# File 'lib/calyx/production/affix_table.rb', line 7

def self.parse(productions, registry)
  # TODO: handle wildcard expressions
  self.new(productions)
end

Instance Method Details

#key_for(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/calyx/production/affix_table.rb', line 41

def key_for(value)
  match = @rhs_index.lookup(value)
  result = @lhs_list[match.index]

  if match.captured
    result.sub("%", match.captured)
  else
    result
  end
end

#value_for(key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/calyx/production/affix_table.rb', line 30

def value_for(key)
  match = @lhs_index.lookup(key)
  result = @rhs_list[match.index]

  if match.captured
    result.sub("%", match.captured)
  else
    result
  end
end