From 4da88fb4cd57871f16649d50572240ac3f7420f0 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Fri, 13 Aug 2010 15:43:34 -0700 Subject: [#4496]+[#4521]+[#4522] Add structures to the AST to represent type definitions (classes, definitions, and nodes). Previously, type definitions were not represented directly in the AST. Instead, the parser would instantiate types and insert them into known_resource_types as soon as they were parsed. This made it difficult to distinguish which types had come from the file that was just parsed and which types had been loaded previously, which led to bug 4496. A side-effect of this change is that the user is no longer allowed to define types inside of conditional constructs (such as if/else). This was allowed before but had unexpected semantics (bugs 4521 and 4522). It is still possible, however, to place an "include" statement inside a conditional construct, and have that "include" statement trigger the autoloading of a file that instantiates types. --- lib/puppet/parser/ast/astarray.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/ast/astarray.rb') diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb index 529998e3c..432300c7a 100644 --- a/lib/puppet/parser/ast/astarray.rb +++ b/lib/puppet/parser/ast/astarray.rb @@ -9,6 +9,11 @@ class Puppet::Parser::AST class ASTArray < Branch include Enumerable + # True if this ASTArray represents a list of statements in a + # context that defines a namespace. Classes and definitions may + # only appear in such a context. + attr_accessor :is_a_namespace + # Return a child by index. Probably never used. def [](index) @children[index] @@ -32,7 +37,19 @@ class Puppet::Parser::AST } rets = items.flatten.collect { |child| - child.safeevaluate(scope) + if child.respond_to? :instantiate + if is_a_namespace + # no problem, just don't evaluate it. + else + msg = "Classes, definitions, and nodes may only appear at toplevel or inside other classes" + error = Puppet::Error.new(msg) + error.line = child.line + error.file = child.file + raise error + end + else + child.safeevaluate(scope) + end } rets.reject { |o| o.nil? } end -- cgit From df088c9ba16dce50c17a79920c1ac186db67b9e9 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Wed, 25 Aug 2010 11:29:23 -0700 Subject: [4638] Cleanup of plurals and inheritance relationships in AST Changed the grammar so that the following "plural" constructs always parse as an ASTArray: - funcvalues - rvalues - resourceinstances - anyparams - params - caseopts - casevalues And the following "singluar" construct never parses as an ASTArray: - statement The previous behavior was for these constructs to parse as a scalar when they represented a single item and an ASTArray when they contained zero or multiple items. ("Statement" could sometimes represent a single item because a single resource declaration could represent multiple resources). This complicated other grammar rules and caused ambiguous handling of nested arrays. Also made these changes to the AST class hierarchy: - ResourceInstance no longer derives from ASTArray. This relationship was not meaningful because a ResourceInstance is a (title, parameters) pair, not an array, and it produced complications when we wanted to represent an array of ResourceInstance objects. - Resource no longer derives from ResourceReference. No significant functionality was being inherited and the relationship doesn't make sense in an AST context. - ResourceOverride no longer derives from Resource. No significant functionality was being inherited and the relationship doesn't make sense in an AST context. - Resource can now represent a compound resource instance such as "notify { foo: ; bar: }". This saves the parser from having to use represent a statement as an array of objects. - ASTArray's evaluate method never flattens out arrays of arrays. --- lib/puppet/parser/ast/astarray.rb | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) (limited to 'lib/puppet/parser/ast/astarray.rb') diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb index 432300c7a..b62c820ca 100644 --- a/lib/puppet/parser/ast/astarray.rb +++ b/lib/puppet/parser/ast/astarray.rb @@ -21,22 +21,8 @@ class Puppet::Parser::AST # Evaluate our children. def evaluate(scope) - # Make a new array, so we don't have to deal with the details of - # flattening and such - items = [] - - # First clean out any AST::ASTArrays - @children.each { |child| - if child.instance_of?(AST::ASTArray) - child.each do |ac| - items << ac - end - else - items << child - end - } - - rets = items.flatten.collect { |child| + result = [] + @children.each do |child| if child.respond_to? :instantiate if is_a_namespace # no problem, just don't evaluate it. @@ -48,10 +34,14 @@ class Puppet::Parser::AST raise error end else - child.safeevaluate(scope) + item = child.safeevaluate(scope) + if !item.nil? + # nil values are implicitly removed. + result.push(item) + end end - } - rets.reject { |o| o.nil? } + end + result end def push(*ary) @@ -69,10 +59,4 @@ class Puppet::Parser::AST "[" + @children.collect { |c| c.to_s }.join(', ') + "]" end end - - # A simple container class, containing the parameters for an object. - # Used for abstracting the grammar declarations. Basically unnecessary - # except that I kept finding bugs because I had too many arrays that - # meant completely different things. - class ResourceInstance < ASTArray; end end -- cgit From 25048ecc40db746f7e88bb6c5e1fc4f2c0150a4f Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Wed, 1 Sep 2010 14:18:41 -0700 Subject: [#4685] Classes, defines, and nodes allowed inside of non-evaluated conditionals Previously, ASTArray#evaluate() was responsible for checking whether the user had tried to declare a class, define, or node in a prohibited location (such as a conditional construct). This meant that errors would only be reported to the user if the conditional code was actually evaluated. Moved the checking into the parser, so that errors are always reported. --- lib/puppet/parser/ast/astarray.rb | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'lib/puppet/parser/ast/astarray.rb') diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb index 432300c7a..e856570f8 100644 --- a/lib/puppet/parser/ast/astarray.rb +++ b/lib/puppet/parser/ast/astarray.rb @@ -9,11 +9,6 @@ class Puppet::Parser::AST class ASTArray < Branch include Enumerable - # True if this ASTArray represents a list of statements in a - # context that defines a namespace. Classes and definitions may - # only appear in such a context. - attr_accessor :is_a_namespace - # Return a child by index. Probably never used. def [](index) @children[index] @@ -37,17 +32,10 @@ class Puppet::Parser::AST } rets = items.flatten.collect { |child| - if child.respond_to? :instantiate - if is_a_namespace - # no problem, just don't evaluate it. - else - msg = "Classes, definitions, and nodes may only appear at toplevel or inside other classes" - error = Puppet::Error.new(msg) - error.line = child.line - error.file = child.file - raise error - end - else + # Skip things that respond to :instantiate (classes, nodes, + # and definitions), because they have already been + # instantiated. + if !child.respond_to?(:instantiate) child.safeevaluate(scope) end } -- cgit