class Puppet::Pops::Parser::Parser::Factory

Factory is a helper class that makes construction of a Pops Model much more convenient. It can be viewed as a small internal DSL for model constructions. For usage see tests using the factory.

@todo All those uppercase methods … they look bad in one way, but stand out nicely in the grammar…

decide if they should change into lower case names (some of the are lower case)...

Constants

Model
STATEMENT_CALLS

Attributes

current[RW]
model[RW]

Public Instance Methods

%(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 405
def % r;      f_arithmetic(:%, r);                                      end
*(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 403
def * r;      f_arithmetic(:*, r);                                      end
+(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 397
def + r;      f_arithmetic(:+, r);                                      end
-(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 399
def - r;      f_arithmetic(:-, r);                                      end
/(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 401
def / r;      f_arithmetic(:/, r);                                      end
<(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 411
def < r;      f_comparison(:<, r);                                      end
<<(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 407
def << r;     f_arithmetic(:<<, r);                                     end
<=(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 413
def <= r;     f_comparison(:<=, r);                                     end
==(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 419
def == r;     f_comparison(:==, r);                                     end
=~(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 423
def =~ r;     f_match(:'=~', r);                                        end
>(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 415
def > r;      f_comparison(:>, r);                                      end
>=(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 417
def >= r;     f_comparison(:>=, r);                                     end
>>(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 409
def >> r;     f_arithmetic(:>>, r);                                     end
[](*r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 393
def [](*r);   f_build_vararg(Model::AccessExpression, current, *r);     end
and(r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 381
def and(r)    f_build_binary(Model::AndExpression, current, r);         end
attributes(*args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 463
def attributes(*args)
  args.each {|a| current.addAttributes(build(a)) }
  self
end
build(o, *args) click to toggle source

Polymorphic build

# File lib/puppet/pops/model/factory.rb, line 35
def build(o, *args)
  begin
    @@build_visitor.visit_this(self, o, *args)
  rescue =>e
    # debug here when in trouble...
    raise e
  end
end
build_AccessExpression(o, left, *keys) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 78
def build_AccessExpression(o, left, *keys)
  o.left_expr = to_ops(left)
  keys.each {|expr| o.addKeys(to_ops(expr)) }
  o
end
build_ArithmeticExpression(o, op, a, b) click to toggle source

Building of Model classes

# File lib/puppet/pops/model/factory.rb, line 56
def build_ArithmeticExpression(o, op, a, b)
  o.operator = op
  build_BinaryExpression(o, a, b)
end
build_Array(o) click to toggle source

Creates a LiteralList instruction from an Array, where the entries are built.

# File lib/puppet/pops/model/factory.rb, line 922
def build_Array(o)
  x = Model::LiteralList.new
  o.each { |v| x.addValues(build(v)) }
  x
end
build_AssignmentExpression(o, op, a, b) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 61
def build_AssignmentExpression(o, op, a, b)
  o.operator = op
  build_BinaryExpression(o, a, b)
end
build_AttributeOperation(o, name, op, value) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 66
def build_AttributeOperation(o, name, op, value)
  o.operator = op
  o.attribute_name = name.to_s # BOOLEAN is allowed in the grammar
  o.value_expr = build(value)
  o
end
build_AttributesOperation(o, value) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 73
def build_AttributesOperation(o, value)
  o.expr = build(value)
  o
end
build_BinaryExpression(o, left, right) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 84
def build_BinaryExpression(o, left, right)
  o.left_expr = to_ops(left)
  o.right_expr = to_ops(right)
  o
end
build_BlockExpression(o, *args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 90
def build_BlockExpression(o, *args)
  args.each {|expr| o.addStatements(to_ops(expr)) }
  o
end
build_CallExpression(o, functor, rval_required, *args) click to toggle source

@param rval_required [Boolean] if the call must produce a value

# File lib/puppet/pops/model/factory.rb, line 938
def build_CallExpression(o, functor, rval_required, *args)
  o.functor_expr = to_ops(functor)
  o.rval_required = rval_required
  args.each {|x| o.addArguments(to_ops(x)) }
  o
end
build_CallMethodExpression(o, functor, rval_required, lambda, *args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 945
def build_CallMethodExpression(o, functor, rval_required, lambda, *args)
  build_CallExpression(o, functor, rval_required, *args)
  o.lambda = lambda
  o
end
build_CaseExpression(o, test, *args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 951
def build_CaseExpression(o, test, *args)
  o.test = build(test)
  args.each {|opt| o.addOptions(build(opt)) }
  o
end
build_CaseOption(o, value_list, then_expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 957
def build_CaseOption(o, value_list, then_expr)
  value_list = [value_list] unless value_list.is_a? Array
  value_list.each { |v| o.addValues(build(v)) }
  b = f_build_body(then_expr)
  o.then_expr = to_ops(b) if b
  o
end
build_Class(o, *args) click to toggle source

Build a Class by creating an instance of it, and then calling build on the created instance with the given arguments

# File lib/puppet/pops/model/factory.rb, line 967
def build_Class(o, *args)
  build(o.new(), *args)
end
build_CollectExpression(o, type_expr, query_expr, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 95
def build_CollectExpression(o, type_expr, query_expr, attribute_operations)
  o.type_expr = to_ops(type_expr)
  o.query = build(query_expr)
  attribute_operations.each {|op| o.addOperations(build(op)) }
  o
end
build_ComparisonExpression(o, op, a, b) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 102
def build_ComparisonExpression(o, op, a, b)
  o.operator = op
  build_BinaryExpression(o, a, b)
end
build_ConcatenatedString(o, *args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 107
def build_ConcatenatedString(o, *args)
  args.each {|expr| o.addSegments(build(expr)) }
  o
end
build_CreateAttributeExpression(o, name, datatype_expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 124
def build_CreateAttributeExpression(o, name, datatype_expr)
  o.name = name
  o.type = to_ops(datatype_expr)
  o
end
build_CreateEnumExpression(o, *args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 118
def build_CreateEnumExpression(o, *args)
  o.name = args.slice(0) if args.size == 2
  o.values = build(args.last)
  o
end
build_CreateTypeExpression(o, name, super_name = nil) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 112
def build_CreateTypeExpression(o, name, super_name = nil)
  o.name = name
  o.super_name = super_name
  o
end
build_EppExpression(o, parameters_specified, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 895
def build_EppExpression(o, parameters_specified, body)
  o.parameters_specified = parameters_specified
  b = f_build_body(body)
  o.body = b.current if b
  o
end
build_Factory(o) click to toggle source

If building a factory, simply unwrap the model oject contained in the factory.

# File lib/puppet/pops/model/factory.rb, line 903
def build_Factory(o)
  o.current
end
build_FalseClass(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 871
def build_FalseClass(o)
  x = Model::LiteralBoolean.new
  x.value = o
  x
end
build_Fixnum(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 877
def build_Fixnum(o)
  x = Model::LiteralInteger.new
  x.value = o;
  x
end
build_Float(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 883
def build_Float(o)
  x = Model::LiteralFloat.new
  x.value = o;
  x
end
build_Hash(o) click to toggle source

Create a LiteralHash instruction from a hash, where keys and values are built The hash entries are added in sorted order based on key.to_s

# File lib/puppet/pops/model/factory.rb, line 931
def build_Hash(o)
  x = Model::LiteralHash.new
  (o.sort_by {|k,v| k.to_s}).each {|k,v| x.addEntries(build(Model::KeyedEntry.new, k, v)) }
  x
end
build_HeredocExpression(o, name, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 130
def build_HeredocExpression(o, name, expr)
  o.syntax = name
  o.text_expr = build(expr)
  o
end
build_HostClassDefinition(o, name, parameters, parent_class_name, body) click to toggle source

@param name [String] a valid classname @param parameters [Array<Model::Parameter>] may be empty @param parent_class_name [String, nil] a valid classname referencing a parent class, optional. @param body [Array<Expression>, Expression, nil] expression that constitute the body @return [Model::HostClassDefinition] configured from the parameters

# File lib/puppet/pops/model/factory.rb, line 142
def build_HostClassDefinition(o, name, parameters, parent_class_name, body)
  build_NamedDefinition(o, name, parameters, body)
  o.parent_class = parent_class_name if parent_class_name
  o
end
build_IfExpression(o, t, ift, els) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 187
def build_IfExpression(o, t, ift, els)
  o.test = build(t)
  o.then_expr = build(ift)
  o.else_expr= build(els)
  o
end
build_KeyedEntry(o, k, v) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 160
def build_KeyedEntry(o, k, v)
  o.key = to_ops(k)
  o.value = to_ops(v)
  o
end
build_LambdaExpression(o, parameters, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 219
def build_LambdaExpression(o, parameters, body)
  parameters.each {|p| o.addParameters(build(p)) }
  b = f_build_body(body)
  o.body = to_ops(b) if b
  o
end
build_LiteralFloat(o, val) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 176
def build_LiteralFloat(o, val)
  o.value = val
  o
end
build_LiteralHash(o, *keyed_entries) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 166
def build_LiteralHash(o, *keyed_entries)
  keyed_entries.each {|entry| o.addEntries build(entry) }
  o
end
build_LiteralInteger(o, val, radix) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 181
def build_LiteralInteger(o, val, radix)
  o.value = val
  o.radix = radix
  o
end
build_LiteralList(o, *values) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 171
def build_LiteralList(o, *values)
  values.each {|v| o.addValues build(v) }
  o
end
build_MatchExpression(o, op, a, b) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 194
def build_MatchExpression(o, op, a, b)
  o.operator = op
  build_BinaryExpression(o, a, b)
end
build_NamedDefinition(o, name, parameters, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 226
def build_NamedDefinition(o, name, parameters, body)
  parameters.each {|p| o.addParameters(build(p)) }
  b = f_build_body(body)
  o.body = b.current if b
  o.name = name
  o
end
build_NilClass(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 860
def build_NilClass(o)
  x = Model::Nop.new
  x
end
build_NodeDefinition(o, hosts, parent, body) click to toggle source

@param o [Model::NodeDefinition] @param hosts [Array<Expression>] host matches @param parent [Expression] parent node matcher @param body [Object] see {f_build_body}

# File lib/puppet/pops/model/factory.rb, line 238
def build_NodeDefinition(o, hosts, parent, body)
  hosts.each {|h| o.addHost_matches(build(h)) }
  o.parent = build(parent) if parent # no nop here
  b = f_build_body(body)
  o.body = b.current if b
  o
end
build_Parameter(o, name, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 246
def build_Parameter(o, name, expr)
  o.name = name
  o.value = build(expr) if expr # don't build a nil/nop
  o
end
build_Program(o, body, definitions, locator) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 327
def build_Program(o, body, definitions, locator)
  o.body = to_ops(body)
  # non containment
  definitions.each { |d| o.addDefinitions(d) }
  o.source_ref = locator.file
  o.source_text = locator.string
  o.line_offsets = locator.line_index
  o.locator = locator
  o
end
build_QualifiedName(o, name) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 338
def build_QualifiedName(o, name)
  o.value = name.to_s
  o
end
build_QualifiedReference(o, name) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 252
def build_QualifiedReference(o, name)
  o.value = name.to_s.downcase
  o
end
build_QueryExpression(o, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 315
def build_QueryExpression(o, expr)
  ops = to_ops(expr)
  o.expr = ops unless Puppet::Pops::Model::Factory.nop? ops
  o
end
build_Regexp(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 889
def build_Regexp(o)
  x = Model::LiteralRegularExpression.new
  x.value = o;
  x
end
build_RelationshipExpression(o, op, a, b) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 257
def build_RelationshipExpression(o, op, a, b)
  o.operator = op
  build_BinaryExpression(o, a, b)
end
build_RenderStringExpression(o, string) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 268
def build_RenderStringExpression(o, string)
  o.value = string;
  o
end
build_ReservedWord(o, name, future) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 154
def build_ReservedWord(o, name, future)
  o.word = name
  o.future = future
  o
end
build_ResourceBody(o, title_expression, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 273
def build_ResourceBody(o, title_expression, attribute_operations)
  o.title = build(title_expression)
  attribute_operations.each {|ao| o.addOperations(build(ao)) }
  o
end
build_ResourceDefaultsExpression(o, type_ref, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 279
def build_ResourceDefaultsExpression(o, type_ref, attribute_operations)
  o.type_ref = build(type_ref)
  attribute_operations.each {|ao| o.addOperations(build(ao)) }
  o
end
build_ResourceExpression(o, type_name, bodies) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 262
def build_ResourceExpression(o, type_name, bodies)
  o.type_name = build(type_name)
  bodies.each {|b| o.addBodies(build(b)) }
  o
end
build_ResourceOverrideExpression(o, resources, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 148
def build_ResourceOverrideExpression(o, resources, attribute_operations)
  o.resources = build(resources)
  attribute_operations.each {|ao| o.addOperations(build(ao)) }
  o
end
build_SelectorEntry(o, matching, value) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 309
def build_SelectorEntry(o, matching, value)
  o.matching_expr = build(matching)
  o.value_expr = build(value)
  o
end
build_SelectorExpression(o, left, *selectors) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 285
def build_SelectorExpression(o, left, *selectors)
  o.left_expr = to_ops(left)
  selectors.each {|s| o.addSelectors(build(s)) }
  o
end
build_String(o) click to toggle source

Building model equivalences of Ruby objects Allows passing regular ruby objects to the factory to produce instructions that when evaluated produce the same thing.

# File lib/puppet/pops/model/factory.rb, line 854
def build_String(o)
  x = Model::LiteralString.new
  x.value = o;
  x
end
build_SubLocatedExpression(o, token, expression) click to toggle source

Builds a SubLocatedExpression - this wraps the expression in a sublocation configured from the given token A SubLocated holds its own locator that is used for subexpressions holding positions relative to what it describes.

# File lib/puppet/pops/model/factory.rb, line 296
def build_SubLocatedExpression(o, token, expression)
  o.expr = build(expression)
  o.offset = token.offset
  o.length =  token.length
  locator = token.locator
  o.locator = locator
  o.leading_line_count = locator.leading_line_count
  o.leading_line_offset = locator.leading_line_offset
  # Index is held in sublocator's parent locator - needed to be able to reconstruct
  o.line_offsets = locator.locator.line_index
  o
end
build_Symbol(o) click to toggle source

Creates a String literal, unless the symbol is one of the special :undef, or :default which instead creates a LiterlUndef, or a LiteralDefault. Supports :undef because nil creates a no-op instruction.

# File lib/puppet/pops/model/factory.rb, line 910
def build_Symbol(o)
  case o
  when :undef
    Model::LiteralUndef.new
  when :default
    Model::LiteralDefault.new
  else
    build_String(o.to_s)
  end
end
build_TokenValue(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 343
def build_TokenValue(o)
  raise "Factory can not deal with a Lexer Token. Got token: #{o}. Probably caused by wrong index in grammar val[n]."
end
build_TrueClass(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 865
def build_TrueClass(o)
  x = Model::LiteralBoolean.new
  x.value = o
  x
end
build_UnaryExpression(o, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 321
def build_UnaryExpression(o, expr)
  ops = to_ops(expr)
  o.expr = ops unless Puppet::Pops::Model::Factory.nop? ops
  o
end
captures_rest() click to toggle source

Mark parameter as capturing the rest of arguments

# File lib/puppet/pops/model/factory.rb, line 591
def captures_rest()
  current.captures_rest = true
end
default(r) click to toggle source

For CaseExpression, setting the default for an already build CaseExpression

# File lib/puppet/pops/model/factory.rb, line 438
def default r
  current.addOptions(Puppet::Pops::Model::Factory.WHEN(:default, r).current)
  self
end
dot(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 395
def dot r;    f_build_binary(Model::NamedAccessExpression, current, r); end
f_arithmetic(op, r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 364
def f_arithmetic(op, r)
  f_build_binary_op(Model::ArithmeticExpression, op, current, r)
end
f_build_binary(klazz, left, right) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 356
def f_build_binary(klazz, left, right)
  Puppet::Pops::Model::Factory.new(build(klazz.new, left, right))
end
f_build_binary_op(klazz, op, left, right) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 352
def f_build_binary_op(klazz, op, left, right)
  Puppet::Pops::Model::Factory.new(build(klazz.new, op, left, right))
end
f_build_body(body) click to toggle source

Builds body :) from different kinds of input @overload #f_build_body(nothing)

@param nothing [nil] unchanged, produces nil

@overload #f_build_body(array)

@param array [Array<Expression>] turns into a BlockExpression

@overload #f_build_body(expr)

@param expr [Expression] produces the given expression

@overload #f_build_body(obj)

@param obj [Object] produces the result of calling #build with body as argument
# File lib/puppet/pops/model/factory.rb, line 208
def f_build_body(body)
  case body
  when NilClass
    nil
  when Array
    Puppet::Pops::Model::Factory.new(Model::BlockExpression, *body)
  else
    build(body)
  end
end
f_build_unary(klazz, expr) click to toggle source

Puppet::Pops::Model::Factory helpers

# File lib/puppet/pops/model/factory.rb, line 348
def f_build_unary(klazz, expr)
  Puppet::Pops::Model::Factory.new(build(klazz.new, expr))
end
f_build_vararg(klazz, left, *arg) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 360
def f_build_vararg(klazz, left, *arg)
  Puppet::Pops::Model::Factory.new(build(klazz.new, left, *arg))
end
f_comparison(op, r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 368
def f_comparison(op, r)
  f_build_binary_op(Model::ComparisonExpression, op, current, r)
end
f_match(op, r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 372
def f_match(op, r)
  f_build_binary_op(Model::MatchExpression, op, current, r)
end
in(r) click to toggle source

Operator helpers

# File lib/puppet/pops/model/factory.rb, line 377
def in(r)     f_build_binary(Model::InExpression, current, r);          end
interpolate() click to toggle source

Polymorphic interpolate

# File lib/puppet/pops/model/factory.rb, line 45
def interpolate()
  begin
    @@interpolation_visitor.visit_this_0(self, current)
  rescue =>e
    # debug here when in trouble...
    raise e
  end
end
interpolate_AccessExpression(o) click to toggle source

rewrite left expression to variable if it is name, number, and recurse if it is an access expression this is for interpolation support in new lexer (${NAME}, ${NAME[}}, ${NUMBER}, ${NUMBER[]} - all other expressions requires variables to be preceded with $

# File lib/puppet/pops/model/factory.rb, line 992
def interpolate_AccessExpression(o)
  if is_interop_rewriteable?(o.left_expr)
    o.left_expr = to_ops(self.class.new(o.left_expr).interpolate)
  end
  o
end
interpolate_CallMethodExpression(o) click to toggle source

Rewrite method calls on the form ${x.each …} to ${$x.each}

# File lib/puppet/pops/model/factory.rb, line 1007
def interpolate_CallMethodExpression(o)
  if is_interop_rewriteable?(o.functor_expr)
    o.functor_expr = to_ops(self.class.new(o.functor_expr).interpolate)
  end
  o
end
interpolate_Factory(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 971
def interpolate_Factory(o)
  interpolate(o.current)
end
interpolate_LiteralInteger(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 975
def interpolate_LiteralInteger(o)
  # convert number to a variable
  self.class.new(o).var
end
interpolate_NamedAccessExpression(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 999
def interpolate_NamedAccessExpression(o)
  if is_interop_rewriteable?(o.left_expr)
      o.left_expr = to_ops(self.class.new(o.left_expr).interpolate)
  end
  o
end
interpolate_Object(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 980
def interpolate_Object(o)
  o
end
interpolate_QualifiedName(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 984
def interpolate_QualifiedName(o)
  self.class.new(o).var
end
is_interop_rewriteable?(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 1014
def is_interop_rewriteable?(o)
  case o
  when Model::AccessExpression, Model::QualifiedName,
    Model::NamedAccessExpression, Model::CallMethodExpression
    true
  when Model::LiteralInteger
    # Only decimal integers can represent variables, else it is a number
    o.radix == 10
  else
    false
  end
end
lambda=(lambda) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 443
def lambda=(lambda)
  current.lambda = lambda.current
  self
end
loc() click to toggle source

@return [Puppet::Pops::Adapters::SourcePosAdapter] with location information

# File lib/puppet/pops/model/factory.rb, line 499
def loc()
  Puppet::Pops::Adapters::SourcePosAdapter.adapt(current)
end
method_missing(meth, *args, &block) click to toggle source

Catch all delegation to current

# File lib/puppet/pops/model/factory.rb, line 469
def method_missing(meth, *args, &block)
  if current.respond_to?(meth)
    current.send(meth, *args, &block)
  else
    super
  end
end
minus() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 385
def minus();  f_build_unary(Model::UnaryMinusExpression, self);         end
minus_set(r) click to toggle source

Assignment -=

# File lib/puppet/pops/model/factory.rb, line 459
def minus_set(r)
  f_build_binary_op(Model::AssignmentExpression, :'-=', current, r)
end
mne(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 425
def mne r;    f_match(:'!~', r);                                        end
name_is_statement(name) click to toggle source

Returns true if the given name is a “statement keyword” (require, include, contain, error, notice, info, debug

# File lib/puppet/pops/model/factory.rb, line 785
def name_is_statement(name)
  STATEMENT_CALLS[name]
end
ne(r;) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 421
def ne r;     f_comparison(:'!=', r);                                   end
not() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 383
def not();    f_build_unary(Model::NotExpression, self);                end
or(r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 379
def or(r)     f_build_binary(Model::OrExpression, current, r);          end
paren() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 427
def paren();  f_build_unary(Model::ParenthesizedExpression, current);   end
plus_set(r) click to toggle source

Assignment +=

# File lib/puppet/pops/model/factory.rb, line 454
def plus_set(r)
  f_build_binary_op(Model::AssignmentExpression, :'+=', current, r)
end
record_position(start_locatable, end_locatable) click to toggle source

Records the position (start -> end) and computes the resulting length.

# File lib/puppet/pops/model/factory.rb, line 487
def record_position(start_locatable, end_locatable)
  from = start_locatable.is_a?(Puppet::Pops::Model::Factory) ? start_locatable.current : start_locatable
  to   = end_locatable.is_a?(Puppet::Pops::Model::Factory) ? end_locatable.current  : end_locatable
  to = from if to.nil? || to.offset.nil?
  o = current
  # record information directly in the Model::Positioned object
  o.offset = from.offset
  o.length ||= to.offset - from.offset + to.length
  self
end
relop(op, r) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 429
def relop op, r
  f_build_binary_op(Model::RelationshipExpression, op.to_sym, current, r)
end
respond_to?(meth, include_all=false) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 477
def respond_to?(meth, include_all=false)
  current.respond_to?(meth, include_all) || super
end
select(*args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 433
def select *args
  Puppet::Pops::Model::Factory.new(build(Model::SelectorExpression, current, *args))
end
set(r) click to toggle source

Assignment =

# File lib/puppet/pops/model/factory.rb, line 449
def set(r)
  f_build_binary_op(Model::AssignmentExpression, :'=', current, r)
end
text() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 389
def text();   f_build_unary(Model::TextExpression, self);               end
to_ops(o, *args) click to toggle source

Checks if the object is already a model object, or build it

# File lib/puppet/pops/model/factory.rb, line 1028
def to_ops(o, *args)
  case o
  when Model::PopsObject
    o
  when Puppet::Pops::Model::Factory
    o.current
  else
    build(o, *args)
  end
end
to_s() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 1053
def to_s
  Puppet::Pops::Model::ModelTreeDumper.new.dump(self)
end
type_expr(o) click to toggle source

Set Expression that should evaluate to the parameter’s type

# File lib/puppet/pops/model/factory.rb, line 596
def type_expr(o)
  current.type_expr = to_ops(o)
end
unfold() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 387
def unfold(); f_build_unary(Model::UnfoldExpression, self);             end
var() click to toggle source
# File lib/puppet/pops/model/factory.rb, line 391
def var();    f_build_unary(Model::VariableExpression, self);           end

Public Class Methods

ATTR(name, type_expr=nil) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 569
def self.ATTR(name, type_expr=nil);    new(Model::CreateAttributeExpression, name, type_expr); end
ATTRIBUTES_OP(expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 698
def self.ATTRIBUTES_OP(expr)
  new(Model::AttributesOperation, expr)
end
ATTRIBUTE_OP(name, op, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 694
def self.ATTRIBUTE_OP(name, op, expr)
  new(Model::AttributeOperation, name, op, expr)
end
CALL_METHOD(functor, argument_list) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 709
def self.CALL_METHOD(functor, argument_list)
  new(Model::CallMethodExpression, functor, true, nil, *argument_list)
end
CALL_NAMED(name, rval_required, argument_list) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 702
def self.CALL_NAMED(name, rval_required, argument_list)
  unless name.kind_of?(Model::PopsObject)
    name = Puppet::Pops::Model::Factory.fqn(name) unless name.is_a?(Puppet::Pops::Model::Factory)
  end
  new(Model::CallNamedFunctionExpression, name, rval_required, *argument_list)
end
CASE(test_e,*options) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 561
def self.CASE(test_e,*options);        new(Model::CaseExpression, test_e, *options);           end
COLLECT(type_expr, query_expr, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 713
def self.COLLECT(type_expr, query_expr, attribute_operations)
  new(Model::CollectExpression, type_expr, query_expr, attribute_operations)
end
DEFINITION(name, parameters, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 754
def self.DEFINITION(name, parameters, body)
  new(Model::ResourceTypeDefinition, name, parameters, body)
end
ENUM(*args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 571
def self.ENUM(*args);                  new(Model::CreateEnumExpression, *args);                end
EPP(parameters, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 631
def self.EPP(parameters, body)
  if parameters.nil?
    params = []
    parameters_specified = false
  else
    params = parameters
    parameters_specified = true
  end
  LAMBDA(params, new(Model::EppExpression, parameters_specified, body))
end
EXPORTED_QUERY(query_expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 690
def self.EXPORTED_QUERY(query_expr)
  new(Model::ExportedQuery, query_expr)
end
HASH(entries) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 575
def self.HASH(entries);                new(Model::LiteralHash, *entries);                      end
HEREDOC(name, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 577
def self.HEREDOC(name, expr);          new(Model::HeredocExpression, name, expr);              end
HOSTCLASS(name, parameters, parent, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 750
def self.HOSTCLASS(name, parameters, parent, body)
  new(Model::HostClassDefinition, name, parameters, parent, body)
end
IF(test_e,then_e,else_e) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 557
def self.IF(test_e,then_e,else_e);     new(Model::IfExpression, test_e, then_e, else_e);       end
KEY_ENTRY(key, val) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 573
def self.KEY_ENTRY(key, val);          new(Model::KeyedEntry, key, val);                       end
LAMBDA(parameters, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 758
def self.LAMBDA(parameters, body)
  new(Model::LambdaExpression, parameters, body)
end
LIST(entries) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 581
def self.LIST(entries);                new(Model::LiteralList, *entries);                      end
MAP(match, value) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 565
def self.MAP(match, value);            new(Model::SelectorEntry, match, value);                end
NAMED_ACCESS(type_name, bodies) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 717
def self.NAMED_ACCESS(type_name, bodies)
  new(Model::NamedAccessExpression, type_name, bodies)
end
NODE(hosts, parent, body) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 585
def self.NODE(hosts, parent, body);    new(Model::NodeDefinition, hosts, parent, body);        end
NUMBER(name_or_numeric) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 653
def self.NUMBER(name_or_numeric)
  if n_radix = Puppet::Pops::Utils.to_n_with_radix(name_or_numeric)
    val, radix = n_radix
    if val.is_a?(Float)
      new(Model::LiteralFloat, val)
    else
      new(Model::LiteralInteger, val, radix)
    end
  else
    # Bad number should already have been caught by lexer - this should never happen
    raise ArgumentError, "Internal Error, NUMBER token does not contain a valid number, #{name_or_numeric}"
  end
end
PARAM(name, expr=nil) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 583
def self.PARAM(name, expr=nil);        new(Model::Parameter, name, expr);                      end
PROGRAM(body, definitions, locator) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 737
def self.PROGRAM(body, definitions, locator)
  new(Model::Program, body, definitions, locator)
end
QNAME(name) click to toggle source

TODO: This is the same a fqn factory method, don’t know if callers to fqn and ::QNAME can live with the same result or not yet - refactor into one method when decided.

# File lib/puppet/pops/model/factory.rb, line 649
def self.QNAME(name)
  new(Model::QualifiedName, name)
end
QNAME_OR_NUMBER(name) click to toggle source

Convert input string to either a qualified name, a LiteralInteger with radix, or a LiteralFloat

# File lib/puppet/pops/model/factory.rb, line 669
def self.QNAME_OR_NUMBER(name)
  if n_radix = Puppet::Pops::Utils.to_n_with_radix(name)
    val, radix = n_radix
    if val.is_a?(Float)
      new(Model::LiteralFloat, val)
    else
      new(Model::LiteralInteger, val, radix)
    end
  else
    new(Model::QualifiedName, name)
  end
end
QREF(name) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 682
def self.QREF(name)
  new(Model::QualifiedReference, name)
end
RENDER_EXPR(expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 627
def self.RENDER_EXPR(expr)
  new(Model::RenderExpression, expr)
end
RENDER_STRING(o) click to toggle source

TODO_EPP

# File lib/puppet/pops/model/factory.rb, line 623
def self.RENDER_STRING(o)
  new(Model::RenderStringExpression, o)
end
RESERVED(name, future=false) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 642
def self.RESERVED(name, future=false)
  new(Model::ReservedWord, name, future)
end
RESOURCE(type_name, bodies) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 721
def self.RESOURCE(type_name, bodies)
  new(Model::ResourceExpression, type_name, bodies)
end
RESOURCE_BODY(resource_title, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 733
def self.RESOURCE_BODY(resource_title, attribute_operations)
  new(Model::ResourceBody, resource_title, attribute_operations)
end
RESOURCE_DEFAULTS(type_name, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 725
def self.RESOURCE_DEFAULTS(type_name, attribute_operations)
  new(Model::ResourceDefaultsExpression, type_name, attribute_operations)
end
RESOURCE_OVERRIDE(resource_ref, attribute_operations) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 729
def self.RESOURCE_OVERRIDE(resource_ref, attribute_operations)
  new(Model::ResourceOverrideExpression, resource_ref, attribute_operations)
end
SUBLOCATE(token, expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 579
def self.SUBLOCATE(token, expr)        new(Model::SubLocatedExpression, token, expr);          end
TEXT(expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 618
def self.TEXT(expr)
  new(Model::TextExpression, new(expr).interpolate)
end
TYPE(name, super_name=nil) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 567
def self.TYPE(name, super_name=nil);   new(Model::CreateTypeExpression, name, super_name);     end
UNLESS(test_e,then_e,else_e) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 559
def self.UNLESS(test_e,then_e,else_e); new(Model::UnlessExpression, test_e, then_e, else_e);   end
VIRTUAL_QUERY(query_expr) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 686
def self.VIRTUAL_QUERY(query_expr)
  new(Model::VirtualQuery, query_expr)
end
WHEN(values_list, block) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 563
def self.WHEN(values_list, block);     new(Model::CaseOption, values_list, block);             end
block(*args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 551
def self.block(*args);                 new(Model::BlockExpression, *args);                     end
block_or_expression(*args) click to toggle source

Builds a BlockExpression if args size > 1, else the single expression/value in args

# File lib/puppet/pops/model/factory.rb, line 742
def self.block_or_expression(*args)
  if args.size > 1
    new(Model::BlockExpression, *args)
  else
    new(args[0])
  end
end
concat(*args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 1039
def self.concat(*args)
  new(args.map do |e|
    e = e.current if e.is_a?(self)
    case e
    when Model::LiteralString
      e.value
    when String
      e
    else
      raise ArgumentError, "can only concatenate strings, got #{e.class}"
    end
  end.join(''))
end
fqn(o) click to toggle source

Creates a QualifiedName representation of o, unless o already represents a QualifiedName in which case it is returned.

# File lib/puppet/pops/model/factory.rb, line 603
def self.fqn(o)
  o = o.current if o.is_a?(Puppet::Pops::Model::Factory)
  o = new(Model::QualifiedName, o) unless o.is_a? Model::QualifiedName
  o
end
fqr(o) click to toggle source

Creates a QualifiedName representation of o, unless o already represents a QualifiedName in which case it is returned.

# File lib/puppet/pops/model/factory.rb, line 612
def self.fqr(o)
  o = o.current if o.is_a?(Puppet::Pops::Model::Factory)
  o = new(Model::QualifiedReference, o) unless o.is_a? Model::QualifiedReference
  o
end
literal(o) click to toggle source

Factory starting points

# File lib/puppet/pops/model/factory.rb, line 543
def self.literal(o);                   new(o);                                                 end
minus(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 545
def self.minus(o);                     new(o).minus;                                           end
new(o, *args) click to toggle source

Initialize a factory with a single object, or a class with arguments applied to build of created instance

# File lib/puppet/pops/model/factory.rb, line 23
def initialize o, *args
  @current = case o
  when Model::PopsObject
    o
  when Puppet::Pops::Model::Factory
    o.current
  else
    build(o, *args)
  end
end
nop?(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 762
def self.nop? o
  o.nil? || o.is_a?(Puppet::Pops::Model::Nop)
end
record_position(o, start_locatable, end_locateable) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 481
def self.record_position(o, start_locatable, end_locateable)
  new(o).record_position(start_locatable, end_locateable)
end
resource_shape(expr) click to toggle source

Returns symbolic information about an expected shape of a resource expression given the LHS of a resource expr.

  • `name { }` => `:resource`, create a resource of the given type

  • `Name { }` => ‘:defaults`, set defaults for the referenced type

  • `Name[] { }` => `:override`, overrides instances referenced by LHS

  • _any other_ => ‘:error’, all other are considered illegal

# File lib/puppet/pops/model/factory.rb, line 521
def self.resource_shape(expr)
  expr = expr.current if expr.is_a?(Puppet::Pops::Model::Factory)
  case expr
  when Model::QualifiedName
    :resource
  when Model::QualifiedReference
    :defaults
  when Model::AccessExpression
    # if Resource[e], then it is not resource specific
    if expr.left_expr.is_a?(Model::QualifiedReference) && expr.left_expr.value == 'resource' && expr.keys.size == 1
      :defaults
    else
      :override
    end
  when 'class'
    :class
  else
    :error
  end
end
set_resource_form(expr, form) click to toggle source

Sets the form of the resource expression (:regular (the default), :virtual, or :exported). Produces true if the expression was a resource expression, false otherwise.

# File lib/puppet/pops/model/factory.rb, line 506
def self.set_resource_form(expr, form)
  expr = expr.current if expr.is_a?(Puppet::Pops::Model::Factory)
  # Note: Validation handles illegal combinations
  return false unless expr.is_a?(Puppet::Pops::Model::AbstractResource)
  expr.form = form
  return true
end
string(*args) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 553
def self.string(*args);                new(Model::ConcatenatedString, *args);                  end
text(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 555
def self.text(o);                      new(o).text;                                            end
transform_calls(expressions) click to toggle source

Transforms an array of expressions containing literal name expressions to calls if followed by an expression, or expression list.

# File lib/puppet/pops/model/factory.rb, line 800
def self.transform_calls(expressions)
  expressions.reduce([]) do |memo, expr|
    expr = expr.current if expr.is_a?(Puppet::Pops::Model::Factory)
    name = memo[-1]
    if name.is_a?(Model::QualifiedName) && STATEMENT_CALLS[name.value]
      if expr.is_a?(Array)
        expr = expr.reject {|e| e.is_a?(Puppet::Pops::Parser::LexerSupport::TokenValue) }
      else
        expr = [expr]
      end
      the_call = Puppet::Pops::Model::Factory.CALL_NAMED(name, false, expr)
      # last positioned is last arg if there are several
      record_position(the_call, name, expr.is_a?(Array) ? expr[-1]  : expr)
      memo[-1] = the_call
      if expr.is_a?(Model::CallNamedFunctionExpression)
        # Patch statement function call to expression style
        # This is needed because it is first parsed as a "statement" and the requirement changes as it becomes
        # an argument to the name to call transform above.
        expr.rval_required = true
      end
    elsif expr.is_a?(Array)
      raise ArgsToNonCallError.new(expr, name)
    else
      memo << expr
      if expr.is_a?(Model::CallNamedFunctionExpression)
        # Patch rvalue expression function call to statement style.
        # This is not really required but done to be AST model compliant
        expr.rval_required = false
      end
    end
    memo
  end

end
transform_resource_wo_title(left, attribute_ops) click to toggle source

Transforms a left expression followed by an untitled resource (in the form of attribute_operations) @param left [Factory, Expression] the lhs followed what may be a hash

# File lib/puppet/pops/model/factory.rb, line 837
def self.transform_resource_wo_title(left, attribute_ops)
  # Returning nil means accepting the given as a potential resource expression
  return nil unless attribute_ops.is_a? Array
  return nil unless left.current.is_a?(Puppet::Pops::Model::QualifiedName)
  keyed_entries = attribute_ops.map do |ao|
    return nil if ao.operator == :'+>'
    KEY_ENTRY(ao.attribute_name, ao.value_expr)
  end
  result = block_or_expression(*transform_calls([left, HASH(keyed_entries)]))
  result
end
unfold(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 547
def self.unfold(o);                    new(o).unfold;                                          end
var(o) click to toggle source
# File lib/puppet/pops/model/factory.rb, line 549
def self.var(o);                       new(o).var;                                             end