Class: Calyx::Production::Concat

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx/production/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
START_TOKEN =
'{'.freeze
END_TOKEN =
'}'.freeze
DEREF_TOKEN =
'.'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expansion) ⇒ Concat

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

Parameters:

  • expansion (Array)


46
47
48
# File 'lib/calyx/production/concat.rb', line 46

def initialize(expansion)
  @expansion = expansion
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
36
37
38
39
40
# File 'lib/calyx/production/concat.rb', line 18

def self.parse(production, registry)
  expansion = 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_TOKEN)
        if head[0] == Memo::SIGIL
          rule = Memo.new(head, registry)
        else
          rule = NonTerminal.new(head, registry)
        end
        unless tail.empty?
          Expression.new(rule, tail, registry)
        else
          rule
        end
      else
        Terminal.new(atom)
      end
    end
  end

  self.new(expansion)
end

Instance Method Details

#evaluate(rng) ⇒ Array

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

Returns:

  • (Array)


54
55
56
57
58
59
60
# File 'lib/calyx/production/concat.rb', line 54

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

  [:concat, concat]
end