Class: Puppet::Parser::AST::PopsBridge::Program

Inherits:
TopLevelConstruct show all
Defined in:
lib/puppet/parser/ast/pops_bridge.rb

Overview

Bridges the top level “Program” produced by the pops parser. Its main purpose is to give one point where all definitions are instantiated (actually defined since the Puppet 3x terminology is somewhat misleading - the definitions are instantiated, but instances of the created types are not created, that happens when classes are included / required, nodes are matched and when resources are instantiated by a resource expression (which is also used to instantiate a host class).

Constant Summary

Constants inherited from Puppet::Parser::AST

AST

Instance Attribute Summary collapse

Attributes inherited from Puppet::Parser::AST

#file, #line, #parent, #pos, #scope

Instance Method Summary collapse

Methods inherited from Puppet::Parser::AST

#inspect, #safeevaluate

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail

Constructor Details

#initialize(program_model, context = {}) ⇒ Program


86
87
88
89
90
91
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 86

def initialize(program_model, context = {})
  @program_model = program_model
  @context = context
  @ast_transformer ||= Puppet::Pops::Model::AstTransformer.new(@context[:file])
  @@evaluator ||= Puppet::Pops::Parser::EvaluatingParser.new()
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context


84
85
86
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 84

def context
  @context
end

#program_modelObject (readonly)

Returns the value of attribute program_model


84
85
86
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 84

def program_model
  @program_model
end

Instance Method Details

#each {|_self| ... } ⇒ Object

Adapts to 3x where top level constructs needs to have each to iterate over children. Short circuit this by yielding self. This means that the HostClass container will call this bridge instance with `instantiate`.

Yields:

  • (_self)

Yield Parameters:


131
132
133
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 131

def each
  yield self
end

#evaluate(scope) ⇒ Object


124
125
126
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 124

def evaluate(scope)
  @@evaluator.evaluate(scope, program_model)
end

#instantiate(modname) ⇒ Object

This is the 3x API, the 3x AST searches through all code to find the instructions that can be instantiated. This Pops-model based instantiation relies on the parser to build this list while parsing (which is more efficient as it avoids one full scan of all logic via recursive enumeration/yield)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 97

def instantiate(modname)

  @program_model.definitions.map do |d|
    case d
    when Puppet::Pops::Model::HostClassDefinition
      instantiate_HostClassDefinition(d, modname)
    when Puppet::Pops::Model::ResourceTypeDefinition
      instantiate_ResourceTypeDefinition(d, modname)
    when Puppet::Pops::Model::CapabilityMapping
      instantiate_CapabilityMapping(d, modname)
    when Puppet::Pops::Model::NodeDefinition
      instantiate_NodeDefinition(d, modname)
    when Puppet::Pops::Model::SiteDefinition
        instantiate_SiteDefinition(d, modname)
    when Puppet::Pops::Model::Application
      instantiate_ApplicationDefinition(d, modname)
    else
      loaders = Puppet::Pops::Loaders.loaders
      loaders.instantiate_definition(d, loaders.find_loader(modname))

      # The 3x logic calling this will not know what to do with the result, it is compacted away at the end
      nil
    end
  end.flatten().compact() # flatten since node definition may have returned an array
                          # Compact since 4x definitions are not understood by compiler
end

#is_definitions_only?Boolean

Returns true if this Program only contains definitions


136
137
138
# File 'lib/puppet/parser/ast/pops_bridge.rb', line 136

def is_definitions_only?
  is_definition?(program_model)
end