Class: Puppet::Pops::Types::TypeParser Private
- Defined in:
- lib/puppet/pops/types/type_parser.rb
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.
Class Method Summary collapse
- .singleton ⇒ Object private
- .type_map ⇒ Object private
Instance Method Summary collapse
-
#initialize ⇒ TypeParser
constructor
A new instance of TypeParser.
-
#interpret(ast, context = nil) ⇒ PAnyType
A specialization of the PAnyType representing the type.
- #interpret_AccessExpression(ast, context) ⇒ Object private
- #interpret_any(ast, context) ⇒ Object private
- #interpret_HeredocExpression(o, context) ⇒ Object private
- #interpret_LambdaExpression(o, context) ⇒ Object private
- #interpret_LiteralBoolean(o, context) ⇒ Object private
- #interpret_LiteralDefault(o, context) ⇒ Object private
- #interpret_LiteralFloat(o, context) ⇒ Object private
- #interpret_LiteralHash(o, context) ⇒ Object private
- #interpret_LiteralInteger(o, context) ⇒ Object private
- #interpret_LiteralList(o, context) ⇒ Object private
- #interpret_LiteralRegularExpression(o, context) ⇒ Object private
- #interpret_LiteralString(o, context) ⇒ Object private
- #interpret_LiteralUndef(o, context) ⇒ Object private
- #interpret_Object(o, context) ⇒ Object private
- #interpret_Program(o, context) ⇒ Object private
- #interpret_QualifiedName(o, context) ⇒ Object private
- #interpret_QualifiedReference(name_ast, context) ⇒ Object private
- #interpret_String(o, context) ⇒ Object private
- #interpret_SubLocatedExpression(o, context) ⇒ Object private
- #interpret_TypeAlias(o, context) ⇒ Object private
- #interpret_TypeDefinition(o, context) ⇒ Object private
- #interpret_UnaryMinusExpression(o, context) ⇒ Object private
- #loader_from_context(ast, context) ⇒ Object private
-
#parse(string, context = nil) ⇒ PAnyType
Produces a *puppet type* based on the given string.
- #parse_literal(string, context = nil) ⇒ Object private
Constructor Details
Class Method Details
.singleton ⇒ 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.
11 12 13 |
# File 'lib/puppet/pops/types/type_parser.rb', line 11 def self.singleton @singleton ||= TypeParser.new end |
.type_map ⇒ 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.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/puppet/pops/types/type_parser.rb', line 161 def self.type_map @type_map ||= { 'integer' => TypeFactory.integer, 'float' => TypeFactory.float, 'numeric' => TypeFactory.numeric, 'init' => TypeFactory.init, 'iterable' => TypeFactory.iterable, 'iterator' => TypeFactory.iterator, 'string' => TypeFactory.string, 'binary' => TypeFactory.binary, 'sensitive' => TypeFactory.sensitive, 'enum' => TypeFactory.enum, 'boolean' => TypeFactory.boolean, 'pattern' => TypeFactory.pattern, 'regexp' => TypeFactory.regexp, 'array' => TypeFactory.array_of_any, 'hash' => TypeFactory.hash_of_any, 'class' => TypeFactory.host_class, 'resource' => TypeFactory.resource, 'collection' => TypeFactory.collection, 'scalar' => TypeFactory.scalar, 'scalardata' => TypeFactory.scalar_data, 'catalogentry' => TypeFactory.catalog_entry, 'undef' => TypeFactory.undef, 'notundef' => TypeFactory.not_undef, 'default' => TypeFactory.default, 'any' => TypeFactory.any, 'variant' => TypeFactory.variant, 'optional' => TypeFactory.optional, 'runtime' => TypeFactory.runtime, 'type' => TypeFactory.type_type, 'tuple' => TypeFactory.tuple, 'struct' => TypeFactory.struct, 'object' => TypeFactory.object, 'typealias' => TypeFactory.type_alias, 'typereference' => TypeFactory.type_reference, 'typeset' => TypeFactory.type_set, # A generic callable as opposed to one that does not accept arguments 'callable' => TypeFactory.all_callables, 'semver' => TypeFactory.sem_ver, 'semverrange' => TypeFactory.sem_ver_range, 'timestamp' => TypeFactory., 'timespan' => TypeFactory.timespan }.freeze end |
Instance Method Details
#interpret(ast, context = nil) ⇒ PAnyType
Returns a specialization of the PAnyType representing the type.
51 52 53 54 55 |
# File 'lib/puppet/pops/types/type_parser.rb', line 51 def interpret(ast, context = nil) result = @type_transformer.visit_this_1(self, ast, context) raise_invalid_type_specification_error(ast) unless result.is_a?(PAnyType) result end |
#interpret_AccessExpression(ast, context) ⇒ 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.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/puppet/pops/types/type_parser.rb', line 228 def interpret_AccessExpression(ast, context) parameters = ast.keys.collect { |param| interpret_any(param, context) } qref = ast.left_expr raise_invalid_type_specification_error(ast) unless qref.is_a?(Model::QualifiedReference) type_name = qref.value case type_name when 'array' case parameters.size when 1 type = assert_type(ast, parameters[0]) when 2 if parameters[0].is_a?(PAnyType) type = parameters[0] size_type = if parameters[1].is_a?(PIntegerType) size_type = parameters[1] else assert_range_parameter(ast, parameters[1]) TypeFactory.range(parameters[1], :default) end else type = :default assert_range_parameter(ast, parameters[0]) assert_range_parameter(ast, parameters[1]) size_type = TypeFactory.range(parameters[0], parameters[1]) end when 3 type = assert_type(ast, parameters[0]) assert_range_parameter(ast, parameters[1]) assert_range_parameter(ast, parameters[2]) size_type = TypeFactory.range(parameters[1], parameters[2]) else raise_invalid_parameters_error('Array', '1 to 3', parameters.size) end TypeFactory.array_of(type, size_type) when 'hash' case parameters.size when 2 if parameters[0].is_a?(PAnyType) && parameters[1].is_a?(PAnyType) TypeFactory.hash_of(parameters[1], parameters[0]) else assert_range_parameter(ast, parameters[0]) assert_range_parameter(ast, parameters[1]) TypeFactory.hash_of(:default, :default, TypeFactory.range(parameters[0], parameters[1])) end when 3 size_type = if parameters[2].is_a?(PIntegerType) parameters[2] else assert_range_parameter(ast, parameters[2]) TypeFactory.range(parameters[2], :default) end assert_type(ast, parameters[0]) assert_type(ast, parameters[1]) TypeFactory.hash_of(parameters[1], parameters[0], size_type) when 4 assert_range_parameter(ast, parameters[2]) assert_range_parameter(ast, parameters[3]) assert_type(ast, parameters[0]) assert_type(ast, parameters[1]) TypeFactory.hash_of(parameters[1], parameters[0], TypeFactory.range(parameters[2], parameters[3])) else raise_invalid_parameters_error('Hash', '2 to 4', parameters.size) end when 'collection' size_type = case parameters.size when 1 if parameters[0].is_a?(PIntegerType) parameters[0] else assert_range_parameter(ast, parameters[0]) TypeFactory.range(parameters[0], :default) end when 2 assert_range_parameter(ast, parameters[0]) assert_range_parameter(ast, parameters[1]) TypeFactory.range(parameters[0], parameters[1]) else raise_invalid_parameters_error('Collection', '1 to 2', parameters.size) end TypeFactory.collection(size_type) when 'class' if parameters.size != 1 raise_invalid_parameters_error('Class', 1, parameters.size) end TypeFactory.host_class(parameters[0]) when 'resource' type = parameters[0] if type.is_a?(PTypeReferenceType) type_str = type.type_string param_start = type_str.index('[') if param_start.nil? type = type_str else tps = interpret_any(@parser.parse_string(type_str[param_start..-1]).model, context) raise_invalid_parameters_error(type.to_s, '1', tps.size) unless tps.size == 1 type = type_str[0..param_start-1] parameters = [type] + tps end end create_resource(type, parameters) when 'regexp' # 1 parameter being a string, or regular expression raise_invalid_parameters_error('Regexp', '1', parameters.size) unless parameters.size == 1 TypeFactory.regexp(parameters[0]) when 'enum' # 1..m parameters being strings raise_invalid_parameters_error('Enum', '1 or more', parameters.size) unless parameters.size >= 1 parameters.each { |p| raise Puppet::ParseError, 'Enum parameters must be identifiers or strings' unless p.is_a?(String) } TypeFactory.enum(*parameters) when 'pattern' # 1..m parameters being strings or regular expressions raise_invalid_parameters_error('Pattern', '1 or more', parameters.size) unless parameters.size >= 1 TypeFactory.pattern(*parameters) when 'variant' # 1..m parameters being strings or regular expressions raise_invalid_parameters_error('Variant', '1 or more', parameters.size) unless parameters.size >= 1 parameters.each { |p| assert_type(ast, p) } TypeFactory.variant(*parameters) when 'tuple' # 1..m parameters being types (last two optionally integer or literal default raise_invalid_parameters_error('Tuple', '1 or more', parameters.size) unless parameters.size >= 1 length = parameters.size size_type = nil if TypeFactory.is_range_parameter?(parameters[-2]) # min, max specification min = parameters[-2] min = (min == :default || min == 'default') ? 0 : min assert_range_parameter(ast, parameters[-1]) max = parameters[-1] max = max == :default ? nil : max parameters = parameters[0, length-2] size_type = TypeFactory.range(min, max) elsif TypeFactory.is_range_parameter?(parameters[-1]) min = parameters[-1] min = (min == :default || min == 'default') ? 0 : min max = nil parameters = parameters[0, length-1] size_type = TypeFactory.range(min, max) end TypeFactory.tuple(parameters, size_type) when 'callable' # 1..m parameters being types (last three optionally integer or literal default, and a callable) if parameters.size > 1 && parameters[0].is_a?(Array) raise_invalid_parameters_error('callable', '2 when first parameter is an array', parameters.size) unless parameters.size == 2 end TypeFactory.callable(*parameters) when 'struct' # 1..m parameters being types (last two optionally integer or literal default raise_invalid_parameters_error('Struct', '1', parameters.size) unless parameters.size == 1 h = parameters[0] raise_invalid_type_specification_error(ast) unless h.is_a?(Hash) TypeFactory.struct(h) when 'integer' if parameters.size == 1 case parameters[0] when Integer TypeFactory.range(parameters[0], :default) when :default TypeFactory.integer # unbound end elsif parameters.size != 2 raise_invalid_parameters_error('Integer', '1 or 2', parameters.size) else TypeFactory.range(parameters[0] == :default ? nil : parameters[0], parameters[1] == :default ? nil : parameters[1]) end when 'object' raise_invalid_parameters_error('Object', 1, parameters.size) unless parameters.size == 1 TypeFactory.object(parameters[0]) when 'typeset' raise_invalid_parameters_error('Object', 1, parameters.size) unless parameters.size == 1 TypeFactory.type_set(parameters[0]) when 'init' assert_type(ast, parameters[0]) TypeFactory.init(*parameters) when 'iterable' if parameters.size != 1 raise_invalid_parameters_error('Iterable', 1, parameters.size) end assert_type(ast, parameters[0]) TypeFactory.iterable(parameters[0]) when 'iterator' if parameters.size != 1 raise_invalid_parameters_error('Iterator', 1, parameters.size) end assert_type(ast, parameters[0]) TypeFactory.iterator(parameters[0]) when 'float' if parameters.size == 1 case parameters[0] when Integer, Float TypeFactory.float_range(parameters[0], :default) when :default TypeFactory.float # unbound end elsif parameters.size != 2 raise_invalid_parameters_error('Float', '1 or 2', parameters.size) else TypeFactory.float_range(parameters[0] == :default ? nil : parameters[0], parameters[1] == :default ? nil : parameters[1]) end when 'string' size_type = case parameters.size when 1 if parameters[0].is_a?(PIntegerType) parameters[0] else assert_range_parameter(ast, parameters[0]) TypeFactory.range(parameters[0], :default) end when 2 assert_range_parameter(ast, parameters[0]) assert_range_parameter(ast, parameters[1]) TypeFactory.range(parameters[0], parameters[1]) else raise_invalid_parameters_error('String', '1 to 2', parameters.size) end TypeFactory.string(size_type) when 'sensitive' if parameters.size == 0 TypeFactory.sensitive elsif parameters.size == 1 param = parameters[0] assert_type(ast, param) TypeFactory.sensitive(param) else raise_invalid_parameters_error('Sensitive', '0 to 1', parameters.size) end when 'optional' if parameters.size != 1 raise_invalid_parameters_error('Optional', 1, parameters.size) end param = parameters[0] assert_type(ast, param) unless param.is_a?(String) TypeFactory.optional(param) when 'any', 'data', 'catalogentry', 'boolean', 'scalar', 'undef', 'numeric', 'default', 'semverrange' raise_unparameterized_type_error(qref) when 'notundef' case parameters.size when 0 TypeFactory.not_undef when 1 param = parameters[0] assert_type(ast, param) unless param.is_a?(String) TypeFactory.not_undef(param) else raise_invalid_parameters_error("NotUndef", "0 to 1", parameters.size) end when 'type' if parameters.size != 1 raise_invalid_parameters_error('Type', 1, parameters.size) end assert_type(ast, parameters[0]) TypeFactory.type_type(parameters[0]) when 'runtime' raise_invalid_parameters_error('Runtime', '2', parameters.size) unless parameters.size == 2 TypeFactory.runtime(*parameters) when 'timespan' raise_invalid_parameters_error('Timespan', '0 to 2', parameters.size) unless parameters.size <= 2 TypeFactory.timespan(*parameters) when 'timestamp' raise_invalid_parameters_error('Timestamp', '0 to 2', parameters.size) unless parameters.size <= 2 TypeFactory.(*parameters) when 'semver' raise_invalid_parameters_error('SemVer', '1 or more', parameters.size) unless parameters.size >= 1 TypeFactory.sem_ver(*parameters) else loader = loader_from_context(qref, context) type = nil unless loader.nil? type = loader.load(:type, type_name) type = type.resolve(loader) unless type.nil? end if type.nil? TypeFactory.type_reference(original_text_of(ast)) elsif type.is_a?(PResourceType) raise_invalid_parameters_error(qref.cased_value, 1, parameters.size) unless parameters.size == 1 TypeFactory.resource(type.type_name, parameters[0]) else # Must be a type alias. They can't use parameters (yet) raise_unparameterized_type_error(qref) end end end |
#interpret_any(ast, context) ⇒ 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.
58 59 60 |
# File 'lib/puppet/pops/types/type_parser.rb', line 58 def interpret_any(ast, context) @type_transformer.visit_this_1(self, ast, context) end |
#interpret_HeredocExpression(o, context) ⇒ 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.
88 89 90 |
# File 'lib/puppet/pops/types/type_parser.rb', line 88 def interpret_HeredocExpression(o, context) interpret_any(o.text_expr, context) end |
#interpret_LambdaExpression(o, context) ⇒ 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.
83 84 85 |
# File 'lib/puppet/pops/types/type_parser.rb', line 83 def interpret_LambdaExpression(o, context) o end |
#interpret_LiteralBoolean(o, context) ⇒ 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.
102 103 104 |
# File 'lib/puppet/pops/types/type_parser.rb', line 102 def interpret_LiteralBoolean(o, context) o.value end |
#interpret_LiteralDefault(o, context) ⇒ 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/types/type_parser.rb', line 107 def interpret_LiteralDefault(o, context) :default end |
#interpret_LiteralFloat(o, context) ⇒ 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.
112 113 114 |
# File 'lib/puppet/pops/types/type_parser.rb', line 112 def interpret_LiteralFloat(o, context) o.value end |
#interpret_LiteralHash(o, context) ⇒ 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.
117 118 119 120 121 122 123 |
# File 'lib/puppet/pops/types/type_parser.rb', line 117 def interpret_LiteralHash(o, context) result = {} o.entries.each do |entry| result[@type_transformer.visit_this_1(self, entry.key, context)] = @type_transformer.visit_this_1(self, entry.value, context) end result end |
#interpret_LiteralInteger(o, context) ⇒ 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.
126 127 128 |
# File 'lib/puppet/pops/types/type_parser.rb', line 126 def interpret_LiteralInteger(o, context) o.value end |
#interpret_LiteralList(o, context) ⇒ 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.
131 132 133 |
# File 'lib/puppet/pops/types/type_parser.rb', line 131 def interpret_LiteralList(o, context) o.values.map { |value| @type_transformer.visit_this_1(self, value, context) } end |
#interpret_LiteralRegularExpression(o, context) ⇒ 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.
136 137 138 |
# File 'lib/puppet/pops/types/type_parser.rb', line 136 def interpret_LiteralRegularExpression(o, context) o.value end |
#interpret_LiteralString(o, context) ⇒ 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.
141 142 143 |
# File 'lib/puppet/pops/types/type_parser.rb', line 141 def interpret_LiteralString(o, context) o.value end |
#interpret_LiteralUndef(o, context) ⇒ 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.
146 147 148 |
# File 'lib/puppet/pops/types/type_parser.rb', line 146 def interpret_LiteralUndef(o, context) nil end |
#interpret_Object(o, context) ⇒ 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.
63 64 65 |
# File 'lib/puppet/pops/types/type_parser.rb', line 63 def interpret_Object(o, context) raise_invalid_type_specification_error(o) end |
#interpret_Program(o, context) ⇒ 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.
68 69 70 |
# File 'lib/puppet/pops/types/type_parser.rb', line 68 def interpret_Program(o, context) interpret_any(o.body, context) end |
#interpret_QualifiedName(o, context) ⇒ 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.
97 98 99 |
# File 'lib/puppet/pops/types/type_parser.rb', line 97 def interpret_QualifiedName(o, context) o.value end |
#interpret_QualifiedReference(name_ast, context) ⇒ 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.
208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/puppet/pops/types/type_parser.rb', line 208 def interpret_QualifiedReference(name_ast, context) name = name_ast.value if found = self.class.type_map[name] found else loader = loader_from_context(name_ast, context) unless loader.nil? type = loader.load(:type, name) type = type.resolve(loader) unless type.nil? end type || TypeFactory.type_reference(name_ast.cased_value) end end |
#interpret_String(o, context) ⇒ 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.
151 152 153 |
# File 'lib/puppet/pops/types/type_parser.rb', line 151 def interpret_String(o, context) o end |
#interpret_SubLocatedExpression(o, context) ⇒ 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.
92 93 94 |
# File 'lib/puppet/pops/types/type_parser.rb', line 92 def interpret_SubLocatedExpression(o, context) interpret_any(o.expr, context) end |
#interpret_TypeAlias(o, context) ⇒ 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.
73 74 75 |
# File 'lib/puppet/pops/types/type_parser.rb', line 73 def interpret_TypeAlias(o, context) Loader::TypeDefinitionInstantiator.create_type(o.name, o.type_expr, Pcore::RUNTIME_NAME_AUTHORITY).resolve(loader_from_context(o, context)) end |
#interpret_TypeDefinition(o, context) ⇒ 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.
78 79 80 |
# File 'lib/puppet/pops/types/type_parser.rb', line 78 def interpret_TypeDefinition(o, context) Loader::TypeDefinitionInstantiator.create_runtime_type(o) end |
#interpret_UnaryMinusExpression(o, context) ⇒ 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.
156 157 158 |
# File 'lib/puppet/pops/types/type_parser.rb', line 156 def interpret_UnaryMinusExpression(o, context) -@type_transformer.visit_this_1(self, o.expr, context) end |
#loader_from_context(ast, context) ⇒ 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.
223 224 225 |
# File 'lib/puppet/pops/types/type_parser.rb', line 223 def loader_from_context(ast, context) Adapters::LoaderAdapter.loader_for_model_object(ast, nil, context) end |
#parse(string, context = nil) ⇒ PAnyType
Produces a *puppet type* based on the given string.
35 36 37 38 |
# File 'lib/puppet/pops/types/type_parser.rb', line 35 def parse(string, context = nil) model = @parser.parse_string(string) interpret(model.model.body, context) end |
#parse_literal(string, context = nil) ⇒ 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.
41 42 43 44 |
# File 'lib/puppet/pops/types/type_parser.rb', line 41 def parse_literal(string, context = nil) factory = @parser.parse_string(string) interpret_any(factory.model.body, context) end |