class Puppet::Pops::Parser::CodeMerger

Public Instance Methods

concatenate(parse_results) click to toggle source

Concatenates the logic in the array of parse results into one parse result. @return Puppet::Parser::AST::BlockExpression

   # File lib/puppet/pops/parser/code_merger.rb
 7 def concatenate(parse_results)
 8   # this is a bit brute force as the result is already 3x ast with wrapped 4x content
 9   # this could be combined in a more elegant way, but it is only used to process a handful of files
10   # at the beginning of a puppet run. TODO: Revisit for Puppet 4x when there is no 3x ast at the top.
11   # PUP-5299, some sites have thousands of entries, and run out of stack when evaluating - the logic
12   # below maps the logic as flatly as possible.
13   #
14   children = parse_results.select {|x| !x.nil? && x.code}.reduce([]) do |memo, parsed_class|
15     case parsed_class.code
16     when Puppet::Parser::AST::BlockExpression
17       # the BlockExpression wraps a single 4x instruction that is most likely wrapped in a Factory
18       memo + parsed_class.code.children.map {|c| c.is_a?(Puppet::Pops::Model::Factory) ? c.model : c }
19     when Puppet::Pops::Model::Factory
20       # If it is a 4x instruction wrapped in a Factory
21       memo + parsed_class.code.model
22     else
23       # It is the instruction directly
24       memo << parsed_class.code
25     end
26   end
27   Puppet::Parser::AST::BlockExpression.new(:children => children)
28 end