Class: Puppet::Pops::Model::AstTransformer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/model/ast_transformer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

AST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Puppet::Parser::AST
Model =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Puppet::Pops::Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file = "unknown-file", importer = nil) ⇒ AstTransformer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AstTransformer.



13
14
15
16
17
18
19
# File 'lib/puppet/pops/model/ast_transformer.rb', line 13

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

#importerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
# File 'lib/puppet/pops/model/ast_transformer.rb', line 12

def importer
  @importer
end

Instance Method Details

#ast(o, klass, hash = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • o (Object, nil)

    object from which source position / location is obtained, may be nil

  • klass (Class<Puppet::Parser::AST>)

    the ast class to create an instance of

  • hash (Hash) (defaults to: {})

    hash with options for the class to create



31
32
33
34
35
# File 'lib/puppet/pops/model/ast_transformer.rb', line 31

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transforms pops expressions into AST 3.1 hostnames



75
76
77
# File 'lib/puppet/pops/model/ast_transformer.rb', line 75

def hostname(o)
  @@hostname_transform_visitor.visit_this_0(self, o)
end

#hostname_Array(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transforms Array of host matching expressions into a (Ruby) array of AST::HostName



87
88
89
# File 'lib/puppet/pops/model/ast_transformer.rb', line 87

def hostname_Array(o)
  o.collect {|x| ast x, AST::HostName, :value => hostname(x) }
end

#hostname_LiteralDefault(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/puppet/pops/model/ast_transformer.rb', line 103

def hostname_LiteralDefault(o)
  return 'default'
end

#hostname_LiteralNumber(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
# File 'lib/puppet/pops/model/ast_transformer.rb', line 99

def hostname_LiteralNumber(o)
  transform(o) # Number to string with correct radix
end

#hostname_LiteralRegularExpression(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
# File 'lib/puppet/pops/model/ast_transformer.rb', line 107

def hostname_LiteralRegularExpression(o)
  ast o, AST::Regex, :value => o.value
end

#hostname_LiteralValue(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/puppet/pops/model/ast_transformer.rb', line 91

def hostname_LiteralValue(o)
  return o.value
end

#hostname_Object(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



111
112
113
# File 'lib/puppet/pops/model/ast_transformer.rb', line 111

def hostname_Object(o)
  raise "Illegal expression - unacceptable as a node name"
end

#hostname_QualifiedName(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/puppet/pops/model/ast_transformer.rb', line 95

def hostname_QualifiedName(o)
  return o.value
end

#is_nop?(o) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Nil, nop Bee bopp a luh-lah, a bop bop boom.

Returns:

  • (Boolean)


122
123
124
# File 'lib/puppet/pops/model/ast_transformer.rb', line 122

def is_nop?(o)
  o.nil? || o.is_a?(Model::Nop)
end

#merge_location(hash, o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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).



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/pops/model/ast_transformer.rb', line 43

def merge_location(hash, o)
  if o
    pos = {}
    source_pos = Puppet::Pops::Utils.find_closest_positioned(o)
    if source_pos
      pos[:line] = source_pos.line
      pos[:pos]  = source_pos.pos
    end
    pos[:file] = @source_file if @source_file
    hash = hash.merge(pos)
  end
  hash
end

#query(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transforms pops expressions into AST 3.1 query expressions



70
71
72
# File 'lib/puppet/pops/model/ast_transformer.rb', line 70

def query(o)
  @@query_transform_visitor.visit_this_0(self, o)
end

#query_Object(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ensures transformation fails if a 3.1 non supported object is encountered in a query expression



82
83
84
# File 'lib/puppet/pops/model/ast_transformer.rb', line 82

def query_Object(o)
  raise "Not a valid expression in a collection query: "+o.class.name
end

#transform(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Transforms pops expressions into AST 3.1 statements/expressions



58
59
60
61
62
63
64
65
66
67
# File 'lib/puppet/pops/model/ast_transformer.rb', line 58

def transform(o)
  begin
  @@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: #{e.message}", 
      loc_data[:file], loc_data[:line], loc_data[:pos], e)
  end
end

#transform_Object(o) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
# File 'lib/puppet/pops/model/ast_transformer.rb', line 115

def transform_Object(o)
  raise "Unacceptable transform - found an Object without a rule: #{o.class}"
end