class Calyx::Syntax::ExpressionChain

Handles filter chains that symbolic expressions can pass through to generate a custom substitution.

Public Class Methods

new(production, modifiers, registry) click to toggle source

@param [#evaluate] production @param [Array] modifiers @param [Calyx::Registry] registry

# File lib/calyx/syntax/expression.rb, line 58
def initialize(production, modifiers, registry)
  @production = production
  @modifiers = modifiers
  @registry = registry
end
parse(production, production_chain, registry) click to toggle source
# File lib/calyx/syntax/expression.rb, line 33
def self.parse(production, production_chain, registry)
  modifier_chain = production_chain.each_slice(2).map do |op_token, target|
    rule = target.to_sym
    case op_token
    when Token::EXPR_FILTER then Modifier.filter(rule)
    when Token::EXPR_MAP_LEFT then Modifier.map_left(rule)
    when Token::EXPR_MAP_RIGHT then Modifier.map_right(rule)
    else
      # Should not end up here because the regex excludes it but this
      # could be a place to add a helpful parse error on any weird
      # chars used by the expression—current behaviour is to pass
      # the broken expression through to the result as part of the
      # text, as if that is what the author meant.
      raise("unreachable")
    end
  end

  expression = Expression.parse(production, registry)

  self.new(expression, modifier_chain, registry)
end

Public Instance Methods

evaluate(options) click to toggle source

Evaluate the expression by expanding the non-terminal to produce a terminal string, then passing it through the given modifier chain and returning the transformed result.

@param [Calyx::Options] options @return [Array]

# File lib/calyx/syntax/expression.rb, line 70
def evaluate(options)
  expanded = @production.evaluate(options).flatten.reject { |o| o.is_a?(Symbol) }.join
  chain = []

  expression = @modifiers.reduce(expanded) do |value, modifier|
    case modifier.type
    when :filter
      @registry.expand_filter(modifier.name, value)
    when :map
      @registry.expand_map(modifier.name, value, modifier.map_dir)
    end
  end

  [:expression, expression]
end