Class: Calyx::Syntax::Concat

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

Overview

A type of production rule representing a string combining both template substitutions and raw content.

Constant Summary collapse

EXPRESSION =
/(\{[A-Za-z0-9_@$<>\.]+\})/.freeze
DEREF_OP =
/([<>\.])/.freeze
START_TOKEN =
'{'.freeze
END_TOKEN =
'}'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expressions) ⇒ Concat

Initialize the concat node with an expansion of terminal and non-terminal fragments.

Parameters:

  • expansion (Array)


41
42
43
# File 'lib/calyx/syntax/concat.rb', line 41

def initialize(expressions)
  @expressions = expressions
end

Class Method Details

.parse(production, registry) ⇒ Object

Parses an interpolated string into fragments combining terminal strings and non-terminal rules.

Returns a concat node which is the head of a tree of child nodes.

Parameters:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/calyx/syntax/concat.rb', line 18

def self.parse(production, registry)
  expressions = production.split(EXPRESSION).map do |atom|
    if atom.is_a?(String)
      if atom.chars.first == START_TOKEN && atom.chars.last == END_TOKEN
        head, *tail = atom.slice(1, atom.length-2).split(DEREF_OP)
        if tail.any?
          ExpressionChain.parse(head, tail, registry)
        else
          Expression.parse(head, registry)
        end
      else
        Terminal.new(atom)
      end
    end
  end

  self.new(expressions)
end

Instance Method Details

#evaluate(options) ⇒ Array

Evaluate all the child nodes of this node and concatenate each expansion together into a single result.

Parameters:

Returns:

  • (Array)


50
51
52
53
54
55
56
57
58
59
# File 'lib/calyx/syntax/concat.rb', line 50

def evaluate(options)
  expansion = @expressions.reduce([]) do |exp, atom|
    exp << atom.evaluate(options)
  end

  #[:expansion, expansion]
  # TODO: fix this along with a git rename
  # Commented out because of a lot of tests depending on :concat symbol
  [:concat, expansion]
end