Class: Puppet::Pops::Types::TypeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/type_parser.rb

Overview

This class provides parsing of Type Specification from a string into the Type Model that is produced by the Puppet::Pops::Types::TypeFactory.

The Type Specifications that are parsed are the same as the stringified forms of types produced by the TypeCalculator.

Constant Summary collapse

TYPES =

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

Puppet::Pops::Types::TypeFactory

Instance Method Summary collapse

Constructor Details

#initializeTypeParser

Returns a new instance of TypeParser.



13
14
15
16
# File 'lib/puppet/pops/types/type_parser.rb', line 13

def initialize
  @parser = Puppet::Pops::Parser::Parser.new()
  @type_transformer = Puppet::Pops::Visitor.new(nil, "interpret", 0, 0)
end

Instance Method Details

#interpret(ast) ⇒ 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.



42
43
44
# File 'lib/puppet/pops/types/type_parser.rb', line 42

def interpret(ast)
  @type_transformer.visit_this(self, ast)
end

#interpret_AccessExpression(parameterized_ast) ⇒ 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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppet/pops/types/type_parser.rb', line 76

def interpret_AccessExpression(parameterized_ast)
  parameters = parameterized_ast.keys.collect { |param| interpret(param) }
  case parameterized_ast.left_expr.value
  when "array"
    if parameters.size != 1
      raise_invalid_parameters_error("Array", 1, parameters.size)
    end
    TYPES.array_of(parameters[0])
  when "hash"
    if parameters.size == 1
      TYPES.hash_of(parameters[0])
    elsif parameters.size != 2
      raise_invalid_parameters_error("Hash", "1 or 2", parameters.size)
    else
      TYPES.hash_of(parameters[1], parameters[0])
    end
  else
    raise_unknown_type_error(parameterized_ast.left_expr)
  end
end

#interpret_Object(anything) ⇒ 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.



47
48
49
# File 'lib/puppet/pops/types/type_parser.rb', line 47

def interpret_Object(anything)
  raise_invalid_type_specification_error
end

#interpret_QualifiedReference(name_ast) ⇒ 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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppet/pops/types/type_parser.rb', line 52

def interpret_QualifiedReference(name_ast)
  case name_ast.value
  when "integer"
    TYPES.integer
  when "float"
    TYPES.float
  when "string"
    TYPES.string
  when "boolean"
    TYPES.boolean
  when "pattern"
    TYPES.pattern
  when "data"
    TYPES.data
  when "array"
    TYPES.array_of_data
  when "hash"
    TYPES.hash_of_data
  else
    raise_unknown_type_error(name_ast)
  end
end

#parse(string) ⇒ Puppet::Pops::Types::PObjectType

Produces a *puppet type* based on the given string.

Examples:

parser.parse('Integer')
parser.parse('Array[String]')
parser.parse('Hash[Integer, Array[String]]')

Parameters:

Returns:



31
32
33
34
35
36
37
38
39
# File 'lib/puppet/pops/types/type_parser.rb', line 31

def parse(string)
  @string = string
  model = @parser.parse_string(@string)
  if model
    interpret(model.current)
  else
    raise_invalid_type_specification_error
  end
end