class Calyx::Syntax::PairedMapping

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

Public Class Methods

new(mapping) click to toggle source

%y prefix: nil, suffix: 'ies'

# 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
parse(productions, registry) click to toggle source
# File lib/calyx/syntax/paired_mapping.rb, line 7
def self.parse(productions, registry)
  # TODO: handle wildcard expressions
  self.new(productions)
end

Public Instance Methods

key_for(value) click to toggle source
# 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) click to toggle source
# 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