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 14 def initialize(source_file = "unknown-file", importer=nil) 15 @@transform_visitor ||= Puppet::Pops::Visitor.new(nil,"transform",0,0) 16 @@query_transform_visitor ||= Puppet::Pops::Visitor.new(nil,"query",0,0) 17 @@hostname_transform_visitor ||= Puppet::Pops::Visitor.new(nil,"hostname",0,0) 18 @importer = importer 19 @source_file = source_file 20 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 32 def ast(o, klass, hash={}) 33 # create and pass hash with file and line information 34 # PUP-3274 - still needed since hostname transformation requires AST::HostName, and AST::Regexp 35 klass.new(**merge_location(hash, o)) 36 end
Transforms pops expressions into AST 3.1 hostnames
# File lib/puppet/pops/model/ast_transformer.rb 78 def hostname(o) 79 @@hostname_transform_visitor.visit_this_0(self, o) 80 end
Transforms Array of host matching expressions into a (Ruby) array of AST::HostName
# File lib/puppet/pops/model/ast_transformer.rb 90 def hostname_Array(o) 91 o.collect {|x| ast x, AST::HostName, :value => hostname(x) } 92 end
# File lib/puppet/pops/model/ast_transformer.rb 106 def hostname_LiteralDefault(o) 107 return 'default' 108 end
# File lib/puppet/pops/model/ast_transformer.rb 102 def hostname_LiteralNumber(o) 103 transform(o) # Number to string with correct radix 104 end
# File lib/puppet/pops/model/ast_transformer.rb 110 def hostname_LiteralRegularExpression(o) 111 ast o, AST::Regex, :value => o.value 112 end
# File lib/puppet/pops/model/ast_transformer.rb 94 def hostname_LiteralValue(o) 95 return o.value 96 end
# File lib/puppet/pops/model/ast_transformer.rb 114 def hostname_Object(o) 115 raise _("Illegal expression - unacceptable as a node name") 116 end
# File lib/puppet/pops/model/ast_transformer.rb 98 def hostname_QualifiedName(o) 99 return o.value 100 end
Nil, nop Bee bopp a luh-lah, a bop bop boom.
# File lib/puppet/pops/model/ast_transformer.rb 125 def is_nop?(o) 126 o.nil? || o.is_a?(Model::Nop) 127 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 44 def merge_location(hash, o) 45 if o 46 pos = {} 47 locator = o.locator 48 offset = o.is_a?(Model::Program) ? 0 : o.offset 49 pos[:line] = locator.line_for_offset(offset) 50 pos[:pos] = locator.pos_on_line(offset) 51 pos[:file] = locator.file 52 if nil_or_empty?(pos[:file]) && !nil_or_empty?(@source_file) 53 pos[:file] = @source_file 54 end 55 hash = hash.merge(pos) 56 end 57 hash 58 end
# File lib/puppet/pops/model/ast_transformer.rb 129 def nil_or_empty?(x) 130 x.nil? || x == '' 131 end
Transforms pops expressions into AST 3.1 query expressions
# File lib/puppet/pops/model/ast_transformer.rb 73 def query(o) 74 @@query_transform_visitor.visit_this_0(self, o) 75 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 85 def query_Object(o) 86 raise _("Not a valid expression in a collection query: %{class_name}") % { class_name: o.class.name } 87 end
Transforms pops expressions into AST 3.1 statements/expressions
# File lib/puppet/pops/model/ast_transformer.rb 61 def transform(o) 62 begin 63 @@transform_visitor.visit_this_0(self,o) 64 rescue StandardError => e 65 loc_data = {} 66 merge_location(loc_data, o) 67 raise Puppet::ParseError.new(_("Error while transforming to Puppet 3 AST: %{message}") % { message: e.message }, 68 loc_data[:file], loc_data[:line], loc_data[:pos], e) 69 end 70 end
# File lib/puppet/pops/model/ast_transformer.rb 118 def transform_Object(o) 119 raise _("Unacceptable transform - found an Object without a rule: %{klass}") % { klass: o.class } 120 end