class Puppet::Pops::Model::AstTransformer
The receiver of `import(file)` calls; once per imported file, or nil if imports are ignored
Transforms a Pops::Model to classic Puppet
AST
. TODO: Documentation is currently skipped completely (it is only used for Rdoc)
Constants
- AST
The base class for the 3x “parse tree”, now only used by the top level constructs and the compiler. Handles things like file name, line #, and also does the initialization for all of the parameters of all of the child objects.
- Model
Attributes
Public Class Methods
# File lib/puppet/pops/model/ast_transformer.rb 13 def initialize(source_file = "unknown-file", importer=nil) 14 @@transform_visitor ||= Puppet::Pops::Visitor.new(nil,"transform",0,0) 15 @@query_transform_visitor ||= Puppet::Pops::Visitor.new(nil,"query",0,0) 16 @@hostname_transform_visitor ||= Puppet::Pops::Visitor.new(nil,"hostname",0,0) 17 @importer = importer 18 @source_file = source_file 19 end
Public Instance Methods
Initialize klass from o (location) and hash (options to created instance). The object o is used to compute a source location. It may be nil. Source position is merged into the given options (non surgically). If o is non-nil, the first found source position going up the containment hierarchy is set. I.e. callers should pass nil if a source position is not wanted or known to be unobtainable for the object.
@param o [Object, nil] object from which source position / location is obtained, may be nil @param klass [Class<Puppet::Parser::AST>] the ast class to create an instance of @param hash [Hash] hash with options for the class to create
# File lib/puppet/pops/model/ast_transformer.rb 31 def ast(o, klass, hash={}) 32 # create and pass hash with file and line information 33 # PUP-3274 - still needed since hostname transformation requires AST::HostName, and AST::Regexp 34 klass.new(**merge_location(hash, o)) 35 end
Transforms pops expressions into AST
3.1 hostnames
# File lib/puppet/pops/model/ast_transformer.rb 77 def hostname(o) 78 @@hostname_transform_visitor.visit_this_0(self, o) 79 end
Transforms Array of host matching expressions into a (Ruby) array of AST::HostName
# File lib/puppet/pops/model/ast_transformer.rb 89 def hostname_Array(o) 90 o.collect {|x| ast x, AST::HostName, :value => hostname(x) } 91 end
# File lib/puppet/pops/model/ast_transformer.rb 105 def hostname_LiteralDefault(o) 106 return 'default' 107 end
# File lib/puppet/pops/model/ast_transformer.rb 101 def hostname_LiteralNumber(o) 102 transform(o) # Number to string with correct radix 103 end
# File lib/puppet/pops/model/ast_transformer.rb 109 def hostname_LiteralRegularExpression(o) 110 ast o, AST::Regex, :value => o.value 111 end
# File lib/puppet/pops/model/ast_transformer.rb 93 def hostname_LiteralValue(o) 94 return o.value 95 end
# File lib/puppet/pops/model/ast_transformer.rb 113 def hostname_Object(o) 114 raise _("Illegal expression - unacceptable as a node name") 115 end
# File lib/puppet/pops/model/ast_transformer.rb 97 def hostname_QualifiedName(o) 98 return o.value 99 end
Nil, nop Bee bopp a luh-lah, a bop bop boom.
# File lib/puppet/pops/model/ast_transformer.rb 124 def is_nop?(o) 125 o.nil? || o.is_a?(Model::Nop) 126 end
THIS IS AN EXPENSIVE OPERATION The 3x AST
requires line, pos etc. to be recorded directly in the AST
nodes and this information must be computed. (Newer implementation only computes the information that is actually needed; typically when raising an exception).
# File lib/puppet/pops/model/ast_transformer.rb 43 def merge_location(hash, o) 44 if o 45 pos = {} 46 locator = o.locator 47 offset = o.is_a?(Model::Program) ? 0 : o.offset 48 pos[:line] = locator.line_for_offset(offset) 49 pos[:pos] = locator.pos_on_line(offset) 50 pos[:file] = locator.file 51 if nil_or_empty?(pos[:file]) && !nil_or_empty?(@source_file) 52 pos[:file] = @source_file 53 end 54 hash = hash.merge(pos) 55 end 56 hash 57 end
# File lib/puppet/pops/model/ast_transformer.rb 128 def nil_or_empty?(x) 129 x.nil? || x == '' 130 end
Transforms pops expressions into AST
3.1 query expressions
# File lib/puppet/pops/model/ast_transformer.rb 72 def query(o) 73 @@query_transform_visitor.visit_this_0(self, o) 74 end
Ensures transformation fails if a 3.1 non supported object is encountered in a query expression
# File lib/puppet/pops/model/ast_transformer.rb 84 def query_Object(o) 85 raise _("Not a valid expression in a collection query: %{class_name}") % { class_name: o.class.name } 86 end
Transforms pops expressions into AST
3.1 statements/expressions
# File lib/puppet/pops/model/ast_transformer.rb 60 def transform(o) 61 begin 62 @@transform_visitor.visit_this_0(self,o) 63 rescue StandardError => e 64 loc_data = {} 65 merge_location(loc_data, o) 66 raise Puppet::ParseError.new(_("Error while transforming to Puppet 3 AST: %{message}") % { message: e.message }, 67 loc_data[:file], loc_data[:line], loc_data[:pos], e) 68 end 69 end
# File lib/puppet/pops/model/ast_transformer.rb 117 def transform_Object(o) 118 raise _("Unacceptable transform - found an Object without a rule: %{klass}") % { klass: o.class } 119 end