Class: Puppet::Pal::Compiler
Overview
A configured compiler as obtained in the callback from ‘Puppet::Pal.with_script_compiler`. (Later, there may also be a catalog compiler available.)
Direct Known Subclasses
Instance Method Summary collapse
-
#call_function(function_name, *args, &block) ⇒ Object
Calls a function given by name with arguments specified in an ‘Array`, and optionally accepts a code block.
-
#create(data_type, *arguments) ⇒ Object
Creates a new instance of a given data type.
-
#evaluate(ast) ⇒ Object
Evaluates an AST obtained from ‘parse_string` or `parse_file` in topscope.
-
#evaluate_file(file) ⇒ Object
Evaluates a puppet language file in top scope.
-
#evaluate_literal(ast) ⇒ Object
Produces a literal value if the AST obtained from ‘parse_string` or `parse_file` does not require any actual evaluation.
-
#evaluate_string(puppet_code, source_file = nil) ⇒ Object
Evaluates a string of puppet language code in top scope.
-
#function_signature(function_name) ⇒ Puppet::Pal::FunctionSignature
Returns a Puppet::Pal::FunctionSignature object or nil if function is not found The returned FunctionSignature has information about all overloaded signatures of the function.
-
#has_catalog? ⇒ Boolean
Returns true if this is a compiler that compiles a catalog.
-
#initialize(internal_compiler) ⇒ Compiler
constructor
A new instance of Compiler.
-
#list_functions(filter_regex = nil, error_collector = nil) ⇒ Array<Puppet::Pops::Loader::TypedName>
Returns an array of TypedName objects for all functions, optionally filtered by a regular expression.
-
#parse_file(file) ⇒ Puppet::Pops::Model::Program
Parses and validates a puppet language file and returns an instance of Puppet::Pops::Model::Program on success.
-
#parse_string(code_string, source_file = nil) ⇒ Puppet::Pops::Model::Program
Parses and validates a puppet language string and returns an instance of Puppet::Pops::Model::Program on success.
-
#type(type_string) ⇒ Puppet::Pops::Types::PAnyType
Parses a puppet data type given in String format and returns that type, or raises an error.
Constructor Details
Instance Method Details
#call_function(function_name, *args, &block) ⇒ Object
Calls a function given by name with arguments specified in an ‘Array`, and optionally accepts a code block.
25 26 27 28 29 30 |
# File 'lib/puppet/pal/compiler.rb', line 25 def call_function(function_name, *args, &block) # TRANSLATORS: do not translate variable name strings in these assertions Pal::assert_non_empty_string(function_name, 'function_name', false) Pal::assert_type(Pal::T_ANY_ARRAY, args, 'args', false) internal_evaluator.evaluator.external_call_function(function_name, args, topscope, &block) end |
#create(data_type, *arguments) ⇒ Object
Creates a new instance of a given data type.
187 188 189 190 191 192 193 |
# File 'lib/puppet/pal/compiler.rb', line 187 def create(data_type, *arguments) t = data_type.is_a?(String) ? type(data_type) : data_type unless t.is_a?(Puppet::Pops::Types::PAnyType) raise ArgumentError, _("Given data_type value is not a data type, got '%{type}'") % {type: t.class} end call_function('new', t, *arguments) end |
#evaluate(ast) ⇒ Object
Evaluates an AST obtained from ‘parse_string` or `parse_file` in topscope. If the ast is a `Puppet::Pops::Model::Program` (what is returned from the `parse` methods, any definitions in the program (that is, any function, plan, etc. that is defined will be made available for use).
108 109 110 111 112 113 114 |
# File 'lib/puppet/pal/compiler.rb', line 108 def evaluate(ast) if ast.is_a?(Puppet::Pops::Model::Program) loaders = Puppet.lookup(:loaders) loaders.instantiate_definitions(ast, loaders.public_environment_loader) end internal_evaluator.evaluate(topscope, ast) end |
#evaluate_file(file) ⇒ Object
Evaluates a puppet language file in top scope. The file must exist and contain valid puppet language code or an error is raised.
97 98 99 |
# File 'lib/puppet/pal/compiler.rb', line 97 def evaluate_file(file) evaluate(parse_file(file)) end |
#evaluate_literal(ast) ⇒ Object
Produces a literal value if the AST obtained from ‘parse_string` or `parse_file` does not require any actual evaluation. This method is useful if obtaining an AST that represents literal values; string, integer, float, boolean, regexp, array, hash; for example from having read this from the command line or as values in some file.
123 124 125 126 127 128 129 |
# File 'lib/puppet/pal/compiler.rb', line 123 def evaluate_literal(ast) catch :not_literal do return Puppet::Pops::Evaluator::LiteralEvaluator.new().literal(ast) end # TRANSLATORS, the 'ast' is the name of a parameter, do not translate raise ArgumentError, _("The given 'ast' does not represent a literal value") end |
#evaluate_string(puppet_code, source_file = nil) ⇒ Object
Evaluates a string of puppet language code in top scope. A “source_file” reference to a source can be given - if not an actual file name, by convention the name should be bracketed with < > to indicate it is something symbolic; for example ‘<commandline>` if the string was given on the command line.
If the given ‘puppet_code` is `nil` or an empty string, `nil` is returned, otherwise the result of evaluating the puppet language string. The given string must form a complete and valid expression/statement as an error is raised otherwise. That is, it is not possible to divide a compound expression by line and evaluate each line individually.
83 84 85 86 87 88 89 |
# File 'lib/puppet/pal/compiler.rb', line 83 def evaluate_string(puppet_code, source_file = nil) return nil if puppet_code.nil? || puppet_code == '' unless puppet_code.is_a?(String) raise ArgumentError, _("The argument 'puppet_code' must be a String, got %{type}") % { type: puppet_code.class } end evaluate(parse_string(puppet_code, source_file)) end |
#function_signature(function_name) ⇒ Puppet::Pal::FunctionSignature
Returns a Puppet::Pal::FunctionSignature object or nil if function is not found The returned FunctionSignature has information about all overloaded signatures of the function
42 43 44 45 46 47 48 49 |
# File 'lib/puppet/pal/compiler.rb', line 42 def function_signature(function_name) loader = internal_compiler.loaders.private_environment_loader if func = loader.load(:function, function_name) return FunctionSignature.new(func.class) end # Could not find function nil end |
#has_catalog? ⇒ Boolean
Returns true if this is a compiler that compiles a catalog. This implementation returns ‘false`
198 199 200 |
# File 'lib/puppet/pal/compiler.rb', line 198 def has_catalog? false end |
#list_functions(filter_regex = nil, error_collector = nil) ⇒ Array<Puppet::Pops::Loader::TypedName>
Returns an array of TypedName objects for all functions, optionally filtered by a regular expression. The returned array has more information than just the leaf name - the typical thing is to just get the name as showing the following example.
Errors that occur during function discovery will either be logged as warnings or collected by the optional ‘error_collector` array. When provided, it will receive DataTypes::Error instances describing each error in detail and no warnings will be logged.
66 67 68 |
# File 'lib/puppet/pal/compiler.rb', line 66 def list_functions(filter_regex = nil, error_collector = nil) list_loadable_kind(:function, filter_regex, error_collector) end |
#parse_file(file) ⇒ Puppet::Pops::Model::Program
Parses and validates a puppet language file and returns an instance of Puppet::Pops::Model::Program on success. If the content is not valid an error is raised.
151 152 153 154 155 156 |
# File 'lib/puppet/pal/compiler.rb', line 151 def parse_file(file) unless file.is_a?(String) raise ArgumentError, _("The argument 'file' must be a String, got %{type}") % { type: file.class } end internal_evaluator.parse_file(file) end |
#parse_string(code_string, source_file = nil) ⇒ Puppet::Pops::Model::Program
Parses and validates a puppet language string and returns an instance of Puppet::Pops::Model::Program on success. If the content is not valid an error is raised.
138 139 140 141 142 143 |
# File 'lib/puppet/pal/compiler.rb', line 138 def parse_string(code_string, source_file = nil) unless code_string.is_a?(String) raise ArgumentError, _("The argument 'code_string' must be a String, got %{type}") % { type: code_string.class } end internal_evaluator.parse_string(code_string, source_file) end |
#type(type_string) ⇒ Puppet::Pops::Types::PAnyType
Parses a puppet data type given in String format and returns that type, or raises an error. A type is needed in calls to ‘new` to create an instance of the data type, or to perform type checking of values - typically using `type.instance?(obj)` to check if `obj` is an instance of the type.
177 178 179 |
# File 'lib/puppet/pal/compiler.rb', line 177 def type(type_string) Puppet::Pops::Types::TypeParser.singleton.parse(type_string) end |