Class: Puppet::Pops::Model::PNTransformer

Inherits:
Object
  • Object
show all
Extended by:
Concurrent::ThreadLocalSingleton
Defined in:
lib/puppet/pops/model/pn_transformer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concurrent::ThreadLocalSingleton

singleton

Constructor Details

#initializePNTransformer

Returns a new instance of PNTransformer.



14
15
16
# File 'lib/puppet/pops/model/pn_transformer.rb', line 14

def initialize
  @visitor = Visitor.new(nil, 'transform', 0, 0)
end

Class Method Details

.transform(ast) ⇒ Object



10
11
12
# File 'lib/puppet/pops/model/pn_transformer.rb', line 10

def self.transform(ast)
  singleton.transform(ast)
end

Instance Method Details

#binary_op(e, op) ⇒ Object



335
336
337
# File 'lib/puppet/pops/model/pn_transformer.rb', line 335

def binary_op(e, op)
  PN::Call.new(op, transform(e.left_expr), transform(e.right_expr))
end

#block_as_entry(name, expr) ⇒ Object



358
359
360
361
362
363
364
# File 'lib/puppet/pops/model/pn_transformer.rb', line 358

def block_as_entry(name, expr)
  if expr.is_a?(BlockExpression)
    transform(expr.statements).with_name(name)
  else
    transform([expr]).with_name(name)
  end
end

#call_to_pn(e, r, nr) ⇒ Object



370
371
372
373
374
# File 'lib/puppet/pops/model/pn_transformer.rb', line 370

def call_to_pn(e, r, nr)
  entries = [transform(e.functor_expr).with_name('functor'), transform(e.arguments).with_name('args')]
  entries << transform(e.lambda).with_name('block') unless e.lambda.nil?
  PN::Map.new(entries).as_call(e.rval_required ? r : nr)
end

#definition_to_pn(e, type_name, parent = nil, return_type = nil) ⇒ Object



339
340
341
342
343
344
345
346
# File 'lib/puppet/pops/model/pn_transformer.rb', line 339

def definition_to_pn(e, type_name, parent = nil, return_type = nil)
  entries = [PN::Literal.new(e.name).with_name('name')]
  entries << PN::Literal.new(parent).with_name('parent') unless parent.nil?
  entries << parameters_entry(e.parameters) unless e.parameters.empty?
  entries << block_as_entry('body', e.body) unless e.body.nil?
  entries << transform(return_type).with_name('returns') unless return_type.nil?
  PN::Map.new(entries).as_call(type_name)
end

#if_to_pn(e, name) ⇒ Object



376
377
378
379
380
381
# File 'lib/puppet/pops/model/pn_transformer.rb', line 376

def if_to_pn(e, name)
  entries = [transform(e.test).with_name('test')]
  entries << block_as_entry('then', e.then_expr) unless is_nop?(e.then_expr)
  entries << block_as_entry('else', e.else_expr) unless is_nop?(e.else_expr)
  PN::Map.new(entries).as_call(name)
end

#is_nop?(e) ⇒ Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/puppet/pops/model/pn_transformer.rb', line 331

def is_nop?(e)
  e.nil? || e.is_a?(Nop)
end

#parameters_entry(parameters) ⇒ Object



348
349
350
351
352
353
354
355
356
# File 'lib/puppet/pops/model/pn_transformer.rb', line 348

def parameters_entry(parameters)
  PN::Map.new(parameters.map do |p|
    entries = []
    entries << transform(p.type_expr).with_name('type') unless p.type_expr.nil?
    entries << PN::Literal(true).with_name('splat') if p.captures_rest
    entries << transform(p.value).with_name('value') unless p.value.nil?
    PN::Map.new(entries).with_name(p.name)
  end).with_name('params')
end

#pn_array(a) ⇒ Object



366
367
368
# File 'lib/puppet/pops/model/pn_transformer.rb', line 366

def pn_array(a)
  a.map { |e| transform(e) }
end

#transform(ast) ⇒ Object



18
19
20
# File 'lib/puppet/pops/model/pn_transformer.rb', line 18

def transform(ast)
  @visitor.visit_this_0(self, ast)
end

#transform_AccessExpression(e) ⇒ Object



22
23
24
# File 'lib/puppet/pops/model/pn_transformer.rb', line 22

def transform_AccessExpression(e)
  PN::List.new([transform(e.left_expr)] + pn_array(e.keys)).as_call('access')
end

#transform_AndExpression(e) ⇒ Object



26
27
28
# File 'lib/puppet/pops/model/pn_transformer.rb', line 26

def transform_AndExpression(e)
  binary_op(e, 'and')
end

#transform_ArithmeticExpression(e) ⇒ Object



30
31
32
# File 'lib/puppet/pops/model/pn_transformer.rb', line 30

def transform_ArithmeticExpression(e)
  binary_op(e, e.operator)
end

#transform_Array(a) ⇒ Object



34
35
36
# File 'lib/puppet/pops/model/pn_transformer.rb', line 34

def transform_Array(a)
  PN::List.new(pn_array(a))
end

#transform_AssignmentExpression(e) ⇒ Object



38
39
40
# File 'lib/puppet/pops/model/pn_transformer.rb', line 38

def transform_AssignmentExpression(e)
  binary_op(e, e.operator)
end

#transform_AttributeOperation(e) ⇒ Object



42
43
44
# File 'lib/puppet/pops/model/pn_transformer.rb', line 42

def transform_AttributeOperation(e)
  PN::Call.new(e.operator, PN::Literal.new(e.attribute_name), transform(e.value_expr))
end

#transform_AttributesOperation(e) ⇒ Object



46
47
48
# File 'lib/puppet/pops/model/pn_transformer.rb', line 46

def transform_AttributesOperation(e)
  PN::Call.new('splat-hash', transform(e.expr))
end

#transform_BlockExpression(e) ⇒ Object



50
51
52
# File 'lib/puppet/pops/model/pn_transformer.rb', line 50

def transform_BlockExpression(e)
  transform(e.statements).as_call('block')
end

#transform_CallFunctionExpression(e) ⇒ Object



54
55
56
# File 'lib/puppet/pops/model/pn_transformer.rb', line 54

def transform_CallFunctionExpression(e)
  call_to_pn(e, 'call-lambda', 'invoke-lambda')
end

#transform_CallMethodExpression(e) ⇒ Object



58
59
60
# File 'lib/puppet/pops/model/pn_transformer.rb', line 58

def transform_CallMethodExpression(e)
  call_to_pn(e, 'call-method', 'invoke-method')
end

#transform_CallNamedFunctionExpression(e) ⇒ Object



62
63
64
# File 'lib/puppet/pops/model/pn_transformer.rb', line 62

def transform_CallNamedFunctionExpression(e)
  call_to_pn(e, 'call', 'invoke')
end

#transform_CaseExpression(e) ⇒ Object



66
67
68
# File 'lib/puppet/pops/model/pn_transformer.rb', line 66

def transform_CaseExpression(e)
  PN::Call.new('case', transform(e.test), transform(e.options))
end

#transform_CaseOption(e) ⇒ Object



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

def transform_CaseOption(e)
  PN::Map.new([transform(e.values).with_name('when'), block_as_entry('then', e.then_expr)])
end

#transform_CollectExpression(e) ⇒ Object



74
75
76
77
78
# File 'lib/puppet/pops/model/pn_transformer.rb', line 74

def transform_CollectExpression(e)
  entries = [transform(e.type_expr).with_name('type'), transform(e.query).with_name('query')]
  entries << transform(e.operations).with_name('ops') unless e.operations.empty?
  PN::Map.new(entries).as_call('collect')
end

#transform_ComparisonExpression(e) ⇒ Object



80
81
82
# File 'lib/puppet/pops/model/pn_transformer.rb', line 80

def transform_ComparisonExpression(e)
  binary_op(e, e.operator)
end

#transform_ConcatenatedString(e) ⇒ Object



84
85
86
# File 'lib/puppet/pops/model/pn_transformer.rb', line 84

def transform_ConcatenatedString(e)
  transform(e.segments).as_call('concat')
end

#transform_EppExpression(e) ⇒ Object



88
89
90
# File 'lib/puppet/pops/model/pn_transformer.rb', line 88

def transform_EppExpression(e)
  e.body.nil? ? PN::Call.new('epp') : transform(e.body).as_call('epp')
end

#transform_ExportedQuery(e) ⇒ Object



92
93
94
# File 'lib/puppet/pops/model/pn_transformer.rb', line 92

def transform_ExportedQuery(e)
  is_nop?(e.expr) ? PN::Call.new('exported-query') : PN::Call.new('exported-query', transform(e.expr))
end

#transform_Factory(e) ⇒ Object



96
97
98
# File 'lib/puppet/pops/model/pn_transformer.rb', line 96

def transform_Factory(e)
  transform(e.model)
end

#transform_FunctionDefinition(e) ⇒ Object



100
101
102
# File 'lib/puppet/pops/model/pn_transformer.rb', line 100

def transform_FunctionDefinition(e)
  definition_to_pn(e, 'function', nil, e.return_type)
end

#transform_HeredocExpression(e) ⇒ Object



104
105
106
107
108
109
# File 'lib/puppet/pops/model/pn_transformer.rb', line 104

def transform_HeredocExpression(e)
  entries = []
  entries << PN::Literal.new(e.syntax).with_name('syntax') unless e.syntax == ''
  entries << transform(e.text_expr).with_name('text')
  PN::Map.new(entries).as_call('heredoc')
end

#transform_HostClassDefinition(e) ⇒ Object



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

def transform_HostClassDefinition(e)
  definition_to_pn(e, 'class', e.parent_class)
end

#transform_IfExpression(e) ⇒ Object



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

def transform_IfExpression(e)
  if_to_pn(e, 'if')
end

#transform_InExpression(e) ⇒ Object



119
120
121
# File 'lib/puppet/pops/model/pn_transformer.rb', line 119

def transform_InExpression(e)
  binary_op(e, 'in')
end

#transform_KeyedEntry(e) ⇒ Object



123
124
125
# File 'lib/puppet/pops/model/pn_transformer.rb', line 123

def transform_KeyedEntry(e)
  PN::Call.new('=>', transform(e.key), transform(e.value))
end

#transform_LambdaExpression(e) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/puppet/pops/model/pn_transformer.rb', line 127

def transform_LambdaExpression(e)
  entries = []
  entries << parameters_entry(e.parameters) unless e.parameters.empty?
  entries << transform(e.return_type).with_name('returns') unless e.return_type.nil?
  entries << block_as_entry('body', e.body) unless e.body.nil?
  PN::Map.new(entries).as_call('lambda')
end

#transform_LiteralBoolean(e) ⇒ Object



135
136
137
# File 'lib/puppet/pops/model/pn_transformer.rb', line 135

def transform_LiteralBoolean(e)
  PN::Literal.new(e.value)
end

#transform_LiteralDefault(_) ⇒ Object



139
140
141
# File 'lib/puppet/pops/model/pn_transformer.rb', line 139

def transform_LiteralDefault(_)
  PN::Call.new('default')
end

#transform_LiteralFloat(e) ⇒ Object



143
144
145
# File 'lib/puppet/pops/model/pn_transformer.rb', line 143

def transform_LiteralFloat(e)
  PN::Literal.new(e.value)
end

#transform_LiteralHash(e) ⇒ Object



147
148
149
# File 'lib/puppet/pops/model/pn_transformer.rb', line 147

def transform_LiteralHash(e)
  transform(e.entries).as_call('hash')
end

#transform_LiteralInteger(e) ⇒ Object



151
152
153
154
# File 'lib/puppet/pops/model/pn_transformer.rb', line 151

def transform_LiteralInteger(e)
  vl = PN::Literal.new(e.value)
  e.radix == 10 ? vl : PN::Map.new([PN::Literal.new(e.radix).with_name('radix'), vl.with_name('value')]).as_call('int')
end

#transform_LiteralList(e) ⇒ Object



156
157
158
# File 'lib/puppet/pops/model/pn_transformer.rb', line 156

def transform_LiteralList(e)
  transform(e.values).as_call('array')
end

#transform_LiteralRegularExpression(e) ⇒ Object



160
161
162
# File 'lib/puppet/pops/model/pn_transformer.rb', line 160

def transform_LiteralRegularExpression(e)
  PN::Literal.new(Types::PRegexpType.regexp_to_s(e.value)).as_call('regexp')
end

#transform_LiteralString(e) ⇒ Object



164
165
166
# File 'lib/puppet/pops/model/pn_transformer.rb', line 164

def transform_LiteralString(e)
  PN::Literal.new(e.value)
end

#transform_LiteralUndef(_) ⇒ Object



168
169
170
# File 'lib/puppet/pops/model/pn_transformer.rb', line 168

def transform_LiteralUndef(_)
  PN::Literal.new(nil)
end

#transform_MatchExpression(e) ⇒ Object



172
173
174
# File 'lib/puppet/pops/model/pn_transformer.rb', line 172

def transform_MatchExpression(e)
  binary_op(e, e.operator)
end

#transform_NamedAccessExpression(e) ⇒ Object



176
177
178
# File 'lib/puppet/pops/model/pn_transformer.rb', line 176

def transform_NamedAccessExpression(e)
  binary_op(e, '.')
end

#transform_NilClass(e) ⇒ Object

Some elements may have a nil element instead of a Nop Expression



192
193
194
# File 'lib/puppet/pops/model/pn_transformer.rb', line 192

def transform_NilClass(e)
  PN::Call.new('nop')
end

#transform_NodeDefinition(e) ⇒ Object



180
181
182
183
184
185
# File 'lib/puppet/pops/model/pn_transformer.rb', line 180

def transform_NodeDefinition(e)
  entries = [transform(e.host_matches).with_name('matches')]
  entries << transform(e.parent).with_name('parent') unless e.parent.nil?
  entries << block_as_entry('body', e.body) unless e.body.nil?
  PN::Map.new(entries).as_call('node')
end

#transform_Nop(_) ⇒ Object



187
188
189
# File 'lib/puppet/pops/model/pn_transformer.rb', line 187

def transform_Nop(_)
  PN::Call.new('nop')
end

#transform_NotExpression(e) ⇒ Object



196
197
198
# File 'lib/puppet/pops/model/pn_transformer.rb', line 196

def transform_NotExpression(e)
  PN::Call.new('!', transform(e.expr))
end

#transform_OrExpression(e) ⇒ Object



200
201
202
# File 'lib/puppet/pops/model/pn_transformer.rb', line 200

def transform_OrExpression(e)
  binary_op(e, 'or')
end

#transform_Parameter(e) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/puppet/pops/model/pn_transformer.rb', line 204

def transform_Parameter(e)
  entries = [PN::Literal.new(e.name).with_name('name')]
  entries << transform(e.type_expr).with_name('type') unless e.type_expr.nil?
  entries << PN::Literal.new(true).with_name('splat') if e.captures_rest
  entries << transform(e.value).with_name('value') unless e.value.nil?
  PN::Map.new(entries).with_name('param')
end

#transform_ParenthesizedExpression(e) ⇒ Object



212
213
214
# File 'lib/puppet/pops/model/pn_transformer.rb', line 212

def transform_ParenthesizedExpression(e)
  PN::Call.new('paren', transform(e.expr))
end

#transform_PlanDefinition(e) ⇒ Object



216
217
218
# File 'lib/puppet/pops/model/pn_transformer.rb', line 216

def transform_PlanDefinition(e)
  definition_to_pn(e, 'plan', nil, e.return_type)
end

#transform_Program(e) ⇒ Object



220
221
222
# File 'lib/puppet/pops/model/pn_transformer.rb', line 220

def transform_Program(e)
  transform(e.body)
end

#transform_QualifiedName(e) ⇒ Object



224
225
226
# File 'lib/puppet/pops/model/pn_transformer.rb', line 224

def transform_QualifiedName(e)
  PN::Call.new('qn', PN::Literal.new(e.value))
end

#transform_QualifiedReference(e) ⇒ Object



228
229
230
# File 'lib/puppet/pops/model/pn_transformer.rb', line 228

def transform_QualifiedReference(e)
  PN::Call.new('qr', PN::Literal.new(e.cased_value))
end

#transform_RelationshipExpression(e) ⇒ Object



232
233
234
# File 'lib/puppet/pops/model/pn_transformer.rb', line 232

def transform_RelationshipExpression(e)
  binary_op(e, e.operator)
end

#transform_RenderExpression(e) ⇒ Object



236
237
238
# File 'lib/puppet/pops/model/pn_transformer.rb', line 236

def transform_RenderExpression(e)
  PN::Call.new('render', transform(e.expr))
end

#transform_RenderStringExpression(e) ⇒ Object



240
241
242
# File 'lib/puppet/pops/model/pn_transformer.rb', line 240

def transform_RenderStringExpression(e)
  PN::Literal.new(e.value).as_call('render-s')
end

#transform_ReservedWord(e) ⇒ Object



244
245
246
# File 'lib/puppet/pops/model/pn_transformer.rb', line 244

def transform_ReservedWord(e)
  PN::Literal.new(e.word).as_call('reserved')
end

#transform_ResourceBody(e) ⇒ Object



248
249
250
251
252
253
# File 'lib/puppet/pops/model/pn_transformer.rb', line 248

def transform_ResourceBody(e)
  PN::Map.new([
                transform(e.title).with_name('title'),
                transform(e.operations).with_name('ops')
              ]).as_call('resource_body')
end

#transform_ResourceDefaultsExpression(e) ⇒ Object



255
256
257
258
259
# File 'lib/puppet/pops/model/pn_transformer.rb', line 255

def transform_ResourceDefaultsExpression(e)
  entries = [transform(e.type_ref).with_name('type'), transform(e.operations).with_name('ops')]
  entries << PN::Literal.new(e.form).with_name('form') unless e.form == 'regular'
  PN::Map.new(entries).as_call('resource-defaults')
end

#transform_ResourceExpression(e) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/puppet/pops/model/pn_transformer.rb', line 261

def transform_ResourceExpression(e)
  entries = [
    transform(e.type_name).with_name('type'),
    PN::List.new(pn_array(e.bodies).map { |body| body[0] }).with_name('bodies')
  ]
  entries << PN::Literal.new(e.form).with_name('form') unless e.form == 'regular'
  PN::Map.new(entries).as_call('resource')
end

#transform_ResourceOverrideExpression(e) ⇒ Object



270
271
272
273
274
# File 'lib/puppet/pops/model/pn_transformer.rb', line 270

def transform_ResourceOverrideExpression(e)
  entries = [transform(e.resources).with_name('resources'), transform(e.operations).with_name('ops')]
  entries << PN::Literal.new(e.form).with_name('form') unless e.form == 'regular'
  PN::Map.new(entries).as_call('resource-override')
end

#transform_ResourceTypeDefinition(e) ⇒ Object



276
277
278
# File 'lib/puppet/pops/model/pn_transformer.rb', line 276

def transform_ResourceTypeDefinition(e)
  definition_to_pn(e, 'define')
end

#transform_SelectorEntry(e) ⇒ Object



280
281
282
# File 'lib/puppet/pops/model/pn_transformer.rb', line 280

def transform_SelectorEntry(e)
  PN::Call.new('=>', transform(e.matching_expr), transform(e.value_expr))
end

#transform_SelectorExpression(e) ⇒ Object



284
285
286
# File 'lib/puppet/pops/model/pn_transformer.rb', line 284

def transform_SelectorExpression(e)
  PN::Call.new('?', transform(e.left_expr), transform(e.selectors))
end

#transform_TextExpression(e) ⇒ Object



288
289
290
# File 'lib/puppet/pops/model/pn_transformer.rb', line 288

def transform_TextExpression(e)
  PN::Call.new('str', transform(e.expr))
end

#transform_TypeAlias(e) ⇒ Object



292
293
294
# File 'lib/puppet/pops/model/pn_transformer.rb', line 292

def transform_TypeAlias(e)
  PN::Call.new('type-alias', PN::Literal.new(e.name), transform(e.type_expr))
end

#transform_TypeDefinition(e) ⇒ Object



296
297
298
# File 'lib/puppet/pops/model/pn_transformer.rb', line 296

def transform_TypeDefinition(e)
  PN::Call.new('type-definition', PN::Literal.new(e.name), PN::Literal.new(e.parent), transform(e.body))
end

#transform_TypeMapping(e) ⇒ Object



300
301
302
# File 'lib/puppet/pops/model/pn_transformer.rb', line 300

def transform_TypeMapping(e)
  PN::Call.new('type-mapping', transform(e.type_expr), transform(e.mapping_expr))
end

#transform_UnaryMinusExpression(e) ⇒ Object



304
305
306
307
308
309
310
311
312
# File 'lib/puppet/pops/model/pn_transformer.rb', line 304

def transform_UnaryMinusExpression(e)
  if e.expr.is_a?(LiteralValue)
    v = e.expr.value
    if v.is_a?(Numeric)
      return PN::Literal.new(-v)
    end
  end
  PN::Call.new('-', transform(e.expr))
end

#transform_UnfoldExpression(e) ⇒ Object



314
315
316
# File 'lib/puppet/pops/model/pn_transformer.rb', line 314

def transform_UnfoldExpression(e)
  PN::Call.new('unfold', transform(e.expr))
end

#transform_UnlessExpression(e) ⇒ Object



318
319
320
# File 'lib/puppet/pops/model/pn_transformer.rb', line 318

def transform_UnlessExpression(e)
  if_to_pn(e, 'unless')
end

#transform_VariableExpression(e) ⇒ Object



322
323
324
325
# File 'lib/puppet/pops/model/pn_transformer.rb', line 322

def transform_VariableExpression(e)
  ne = e.expr
  PN::Call.new('var', ne.is_a?(Model::QualifiedName) ? PN::Literal.new(ne.value) : transform(ne))
end

#transform_VirtualQuery(e) ⇒ Object



327
328
329
# File 'lib/puppet/pops/model/pn_transformer.rb', line 327

def transform_VirtualQuery(e)
  is_nop?(e.expr) ? PN::Call.new('virtual-query') : PN::Call.new('virtual-query', transform(e.expr))
end