Class: Puppet::Pops::Model::AstTransformer
- Defined in:
- lib/puppet/pops/model/ast_transformer.rb
Overview
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)
Constant Summary collapse
Instance Attribute Summary collapse
- #importer ⇒ Object readonly
Instance Method Summary collapse
-
#ast(o, klass, hash = {}) ⇒ Object
Initialize klass from o (location) and hash (options to created instance).
-
#hostname(o) ⇒ Object
Transforms pops expressions into AST 3.1 hostnames.
-
#hostname_Array(o) ⇒ Object
Transforms Array of host matching expressions into a (Ruby) array of AST::HostName.
- #hostname_LiteralDefault(o) ⇒ Object
- #hostname_LiteralNumber(o) ⇒ Object
- #hostname_LiteralRegularExpression(o) ⇒ Object
- #hostname_LiteralValue(o) ⇒ Object
- #hostname_Object(o) ⇒ Object
- #hostname_QualifiedName(o) ⇒ Object
-
#initialize(source_file = "unknown-file", importer = nil) ⇒ AstTransformer
constructor
A new instance of AstTransformer.
-
#is_nop?(o) ⇒ Boolean
Nil, nop Bee bopp a luh-lah, a bop bop boom.
-
#merge_location(hash, o) ⇒ Object
THIS IS AN EXPENSIVE OPERATION The 3x AST requires line, pos etc.
- #nil_or_empty?(x) ⇒ Boolean
-
#query(o) ⇒ Object
Transforms pops expressions into AST 3.1 query expressions.
-
#query_Object(o) ⇒ Object
Ensures transformation fails if a 3.1 non supported object is encountered in a query expression.
-
#transform(o) ⇒ Object
Transforms pops expressions into AST 3.1 statements/expressions.
- #transform_Object(o) ⇒ Object
Constructor Details
#initialize(source_file = "unknown-file", importer = nil) ⇒ AstTransformer
Returns a new instance of AstTransformer.
16 17 18 19 20 21 22 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 16 def initialize(source_file = "unknown-file", importer = nil) @@transform_visitor ||= Puppet::Pops::Visitor.new(nil, "transform", 0, 0) @@query_transform_visitor ||= Puppet::Pops::Visitor.new(nil, "query", 0, 0) @@hostname_transform_visitor ||= Puppet::Pops::Visitor.new(nil, "hostname", 0, 0) @importer = importer @source_file = source_file end |
Instance Attribute Details
#importer ⇒ Object (readonly)
14 15 16 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 14 def importer @importer end |
Instance Method Details
#ast(o, klass, hash = {}) ⇒ Object
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.
34 35 36 37 38 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 34 def ast(o, klass, hash = {}) # create and pass hash with file and line information # PUP-3274 - still needed since hostname transformation requires AST::HostName, and AST::Regexp klass.new(**merge_location(hash, o)) end |
#hostname(o) ⇒ Object
Transforms pops expressions into AST 3.1 hostnames
78 79 80 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 78 def hostname(o) @@hostname_transform_visitor.visit_this_0(self, o) end |
#hostname_Array(o) ⇒ Object
Transforms Array of host matching expressions into a (Ruby) array of AST::HostName
89 90 91 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 89 def hostname_Array(o) o.collect { |x| ast x, AST::HostName, :value => hostname(x) } end |
#hostname_LiteralDefault(o) ⇒ Object
105 106 107 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 105 def hostname_LiteralDefault(o) 'default' end |
#hostname_LiteralNumber(o) ⇒ Object
101 102 103 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 101 def hostname_LiteralNumber(o) transform(o) # Number to string with correct radix end |
#hostname_LiteralRegularExpression(o) ⇒ Object
109 110 111 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 109 def hostname_LiteralRegularExpression(o) ast o, AST::Regex, :value => o.value end |
#hostname_LiteralValue(o) ⇒ Object
93 94 95 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 93 def hostname_LiteralValue(o) o.value end |
#hostname_Object(o) ⇒ Object
113 114 115 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 113 def hostname_Object(o) raise _("Illegal expression - unacceptable as a node name") end |
#hostname_QualifiedName(o) ⇒ Object
97 98 99 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 97 def hostname_QualifiedName(o) o.value end |
#is_nop?(o) ⇒ Boolean
Nil, nop Bee bopp a luh-lah, a bop bop boom.
124 125 126 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 124 def is_nop?(o) o.nil? || o.is_a?(Model::Nop) end |
#merge_location(hash, o) ⇒ Object
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).
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 46 def merge_location(hash, o) if o pos = {} locator = o.locator offset = o.is_a?(Model::Program) ? 0 : o.offset pos[:line] = locator.line_for_offset(offset) pos[:pos] = locator.pos_on_line(offset) pos[:file] = locator.file if nil_or_empty?(pos[:file]) && !nil_or_empty?(@source_file) pos[:file] = @source_file end hash = hash.merge(pos) end hash end |
#nil_or_empty?(x) ⇒ Boolean
128 129 130 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 128 def nil_or_empty?(x) x.nil? || x == '' end |
#query(o) ⇒ Object
Transforms pops expressions into AST 3.1 query expressions
73 74 75 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 73 def query(o) @@query_transform_visitor.visit_this_0(self, o) end |
#query_Object(o) ⇒ Object
Ensures transformation fails if a 3.1 non supported object is encountered in a query expression
84 85 86 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 84 def query_Object(o) raise _("Not a valid expression in a collection query: %{class_name}") % { class_name: o.class.name } end |
#transform(o) ⇒ Object
Transforms pops expressions into AST 3.1 statements/expressions
63 64 65 66 67 68 69 70 |
# File 'lib/puppet/pops/model/ast_transformer.rb', line 63 def transform(o) @@transform_visitor.visit_this_0(self, o) rescue StandardError => e loc_data = {} merge_location(loc_data, o) raise Puppet::ParseError.new(_("Error while transforming to Puppet 3 AST: %{message}") % { message: e. }, loc_data[:file], loc_data[:line], loc_data[:pos], e) end |