Class: Calyx::Syntax::PairedMapping
- Inherits:
-
Object
- Object
- Calyx::Syntax::PairedMapping
- Defined in:
- lib/calyx/syntax/paired_mapping.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
-
#initialize(mapping) ⇒ PairedMapping
constructor
%y prefix: nil, suffix: ‘ies’.
- #key_for(value) ⇒ Object
- #value_for(key) ⇒ Object
Constructor Details
#initialize(mapping) ⇒ PairedMapping
%y prefix: nil, suffix: ‘ies’
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/calyx/syntax/paired_mapping.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/syntax/paired_mapping.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/syntax/paired_mapping.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/syntax/paired_mapping.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 |