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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(visitor) ⇒ PNTransformer

Returns a new instance of PNTransformer


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

def initialize(visitor)
  @visitor = visitor
end

Class Method Details

.singletonObject


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

def self.singleton
  @singleton ||= new(visitor)
end

.transform(ast) ⇒ Object


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

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

.visitorObject


6
7
8
# File 'lib/puppet/pops/model/pn_transformer.rb', line 6

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

Instance Method Details

#binary_op(e, op) ⇒ Object


355
356
357
# File 'lib/puppet/pops/model/pn_transformer.rb', line 355

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

#block_as_entry(name, expr) ⇒ Object


378
379
380
381
382
383
384
# File 'lib/puppet/pops/model/pn_transformer.rb', line 378

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


390
391
392
393
394
# File 'lib/puppet/pops/model/pn_transformer.rb', line 390

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


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

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


396
397
398
399
400
401
# File 'lib/puppet/pops/model/pn_transformer.rb', line 396

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


351
352
353
# File 'lib/puppet/pops/model/pn_transformer.rb', line 351

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

#parameters_entry(parameters) ⇒ Object


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

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


386
387
388
# File 'lib/puppet/pops/model/pn_transformer.rb', line 386

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

#transform(ast) ⇒ Object


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

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

#transform_AccessExpression(e) ⇒ Object


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

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

#transform_AndExpression(e) ⇒ Object


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

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

#transform_Application(e) ⇒ Object


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

def transform_Application(e)
  definition_to_pn(e, 'application')
end

#transform_ArithmeticExpression(e) ⇒ Object


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

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

#transform_Array(a) ⇒ Object


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

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

#transform_AssignmentExpression(e) ⇒ Object


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

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

#transform_AttributeOperation(e) ⇒ Object


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

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

#transform_AttributesOperation(e) ⇒ Object


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

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

#transform_BlockExpression(e) ⇒ Object


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

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

#transform_CallFunctionExpression(e) ⇒ Object


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

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

#transform_CallMethodExpression(e) ⇒ Object


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

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

#transform_CallNamedFunctionExpression(e) ⇒ Object


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

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

#transform_CapabilityMapping(e) ⇒ Object


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

def transform_CapabilityMapping(e)
  PN::Call.new(e.kind, transform(e.component), PN::List.new([PN::Literal.new(e.capability)] + pn_array(e.mappings)))
end

#transform_CaseExpression(e) ⇒ Object


78
79
80
# File 'lib/puppet/pops/model/pn_transformer.rb', line 78

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

#transform_CaseOption(e) ⇒ Object


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

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


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

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


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

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

#transform_ConcatenatedString(e) ⇒ Object


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

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

#transform_EppExpression(e) ⇒ Object


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

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

#transform_ExportedQuery(e) ⇒ Object


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

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


108
109
110
# File 'lib/puppet/pops/model/pn_transformer.rb', line 108

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

#transform_FunctionDefinition(e) ⇒ Object


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

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

#transform_HeredocExpression(e) ⇒ Object


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

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


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

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

#transform_IfExpression(e) ⇒ Object


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

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

#transform_InExpression(e) ⇒ Object


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

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

#transform_KeyedEntry(e) ⇒ Object


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

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

#transform_LambdaExpression(e) ⇒ Object


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

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


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

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

#transform_LiteralDefault(_) ⇒ Object


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

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

#transform_LiteralFloat(e) ⇒ Object


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

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

#transform_LiteralHash(e) ⇒ Object


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

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

#transform_LiteralInteger(e) ⇒ Object


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

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


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

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

#transform_LiteralRegularExpression(e) ⇒ Object


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

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

#transform_LiteralString(e) ⇒ Object


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

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

#transform_LiteralUndef(_) ⇒ Object


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

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

#transform_MatchExpression(e) ⇒ Object


184
185
186
# File 'lib/puppet/pops/model/pn_transformer.rb', line 184

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

#transform_NamedAccessExpression(e) ⇒ Object


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

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

#transform_NilClass(e) ⇒ Object

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


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

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

#transform_NodeDefinition(e) ⇒ Object


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

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


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

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

#transform_NotExpression(e) ⇒ Object


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

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

#transform_OrExpression(e) ⇒ Object


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

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

#transform_Parameter(e) ⇒ Object


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

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


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

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

#transform_PlanDefinition(e) ⇒ Object


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

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

#transform_Program(e) ⇒ Object


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

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

#transform_QualifiedName(e) ⇒ Object


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

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

#transform_QualifiedReference(e) ⇒ Object


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

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

#transform_RelationshipExpression(e) ⇒ Object


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

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

#transform_RenderExpression(e) ⇒ Object


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

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

#transform_RenderStringExpression(e) ⇒ Object


252
253
254
# File 'lib/puppet/pops/model/pn_transformer.rb', line 252

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

#transform_ReservedWord(e) ⇒ Object


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

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

#transform_ResourceBody(e) ⇒ Object


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

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


267
268
269
270
271
# File 'lib/puppet/pops/model/pn_transformer.rb', line 267

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


273
274
275
276
277
278
279
280
# File 'lib/puppet/pops/model/pn_transformer.rb', line 273

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


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

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


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

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

#transform_SelectorEntry(e) ⇒ Object


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

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

#transform_SelectorExpression(e) ⇒ Object


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

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

#transform_SiteDefinition(e) ⇒ Object


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

def transform_SiteDefinition(e)
  transform(e.body).as_call('site')
end

#transform_SubLocatedExpression(e) ⇒ Object


304
305
306
# File 'lib/puppet/pops/model/pn_transformer.rb', line 304

def transform_SubLocatedExpression(e)
  transform(e.expr)
end

#transform_TextExpression(e) ⇒ Object


308
309
310
# File 'lib/puppet/pops/model/pn_transformer.rb', line 308

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

#transform_TypeAlias(e) ⇒ Object


312
313
314
# File 'lib/puppet/pops/model/pn_transformer.rb', line 312

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

#transform_TypeDefinition(e) ⇒ Object


316
317
318
# File 'lib/puppet/pops/model/pn_transformer.rb', line 316

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


320
321
322
# File 'lib/puppet/pops/model/pn_transformer.rb', line 320

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

#transform_UnaryMinusExpression(e) ⇒ Object


324
325
326
327
328
329
330
331
332
# File 'lib/puppet/pops/model/pn_transformer.rb', line 324

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


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

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

#transform_UnlessExpression(e) ⇒ Object


338
339
340
# File 'lib/puppet/pops/model/pn_transformer.rb', line 338

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

#transform_VariableExpression(e) ⇒ Object


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

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


347
348
349
# File 'lib/puppet/pops/model/pn_transformer.rb', line 347

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