From b7f42441b91c921cd31f3d8c7875ce98bdedf786 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 6 Sep 2007 18:53:00 -0500 Subject: Renaming some ast resource classes and files so they make a lot more sense. --- lib/puppet/parser/ast/astarray.rb | 2 +- lib/puppet/parser/ast/resource.rb | 75 +++++++++++++++++++++++++++++ lib/puppet/parser/ast/resource_defaults.rb | 21 ++++++++ lib/puppet/parser/ast/resource_override.rb | 60 +++++++++++++++++++++++ lib/puppet/parser/ast/resource_reference.rb | 66 +++++++++++++++++++++++++ lib/puppet/parser/ast/resourcedef.rb | 75 ----------------------------- lib/puppet/parser/ast/resourcedefaults.rb | 21 -------- lib/puppet/parser/ast/resourceoverride.rb | 60 ----------------------- lib/puppet/parser/ast/resourceref.rb | 66 ------------------------- lib/puppet/parser/grammar.ra | 14 +++--- lib/puppet/parser/parser.rb | 18 +++---- 11 files changed, 239 insertions(+), 239 deletions(-) create mode 100644 lib/puppet/parser/ast/resource.rb create mode 100644 lib/puppet/parser/ast/resource_defaults.rb create mode 100644 lib/puppet/parser/ast/resource_override.rb create mode 100644 lib/puppet/parser/ast/resource_reference.rb delete mode 100644 lib/puppet/parser/ast/resourcedef.rb delete mode 100644 lib/puppet/parser/ast/resourcedefaults.rb delete mode 100644 lib/puppet/parser/ast/resourceoverride.rb delete mode 100644 lib/puppet/parser/ast/resourceref.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb index 2a8f4d4c1..9a2dc286c 100644 --- a/lib/puppet/parser/ast/astarray.rb +++ b/lib/puppet/parser/ast/astarray.rb @@ -71,5 +71,5 @@ class Puppet::Parser::AST # 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 ResourceInst < ASTArray; end + class ResourceInstance < ASTArray; end end diff --git a/lib/puppet/parser/ast/resource.rb b/lib/puppet/parser/ast/resource.rb new file mode 100644 index 000000000..c53ab0a68 --- /dev/null +++ b/lib/puppet/parser/ast/resource.rb @@ -0,0 +1,75 @@ +require 'puppet/parser/ast/resource_reference' + +# Any normal puppet resource declaration. Can point to a definition or a +# builtin type. +class Puppet::Parser::AST +class Resource < AST::ResourceReference + attr_accessor :title, :type, :exported, :virtual + attr_reader :params + + # Does not actually return an object; instead sets an object + # in the current scope. + def evaluate(options) + scope = options[:scope] + + # Evaluate all of the specified params. + paramobjects = @params.collect { |param| + param.safeevaluate(:scope => scope) + } + + objtitles = @title.safeevaluate(:scope => scope) + + # it's easier to always use an array, even for only one name + unless objtitles.is_a?(Array) + objtitles = [objtitles] + end + + objtype = qualified_type(scope) + + # This is where our implicit iteration takes place; if someone + # passed an array as the name, then we act just like the called us + # many times. + objtitles.collect { |objtitle| + exceptwrap :type => Puppet::ParseError do + exp = self.exported || scope.resource.exported? + # We want virtual to be true if exported is true. We can't + # just set :virtual => self.virtual in the initialization, + # because sometimes the :virtual attribute is set *after* + # :exported, in which case it clobbers :exported if :exported + # is true. Argh, this was a very tough one to track down. + virt = self.virtual || scope.resource.virtual? || exp + obj = Puppet::Parser::Resource.new( + :type => objtype, + :title => objtitle, + :params => paramobjects, + :file => self.file, + :line => self.line, + :exported => exp, + :virtual => virt, + :source => scope.source, + :scope => scope + ) + + # And then store the resource in the compile. + # At some point, we need to switch all of this to return + # objects instead of storing them like this. + scope.compile.store_resource(scope, obj) + obj + end + }.reject { |obj| obj.nil? } + end + + # Set the parameters for our object. + def params=(params) + if params.is_a?(AST::ASTArray) + @params = params + else + @params = AST::ASTArray.new( + :line => params.line, + :file => params.file, + :children => [params] + ) + end + end +end +end diff --git a/lib/puppet/parser/ast/resource_defaults.rb b/lib/puppet/parser/ast/resource_defaults.rb new file mode 100644 index 000000000..44ec146b0 --- /dev/null +++ b/lib/puppet/parser/ast/resource_defaults.rb @@ -0,0 +1,21 @@ +require 'puppet/parser/ast/branch' + +class Puppet::Parser::AST + # A statement syntactically similar to an ResourceDef, but uses a + # capitalized object type and cannot have a name. + class ResourceDefaults < AST::Branch + attr_accessor :type, :params + + # As opposed to ResourceDef, this stores each default for the given + # object type. + def evaluate(hash) + scope = hash[:scope] + type = @type.downcase + params = @params.safeevaluate(:scope => scope) + + parsewrap do + scope.setdefaults(type, params) + end + end + end +end diff --git a/lib/puppet/parser/ast/resource_override.rb b/lib/puppet/parser/ast/resource_override.rb new file mode 100644 index 000000000..46c930902 --- /dev/null +++ b/lib/puppet/parser/ast/resource_override.rb @@ -0,0 +1,60 @@ +require 'puppet/parser/ast/resource' + +class Puppet::Parser::AST + # Set a parameter on a resource specification created somewhere else in the + # configuration. The object is responsible for verifying that this is allowed. + class ResourceOverride < Resource + attr_accessor :object + attr_reader :params + + # Iterate across all of our children. + def each + [@object,@params].flatten.each { |param| + #Puppet.debug("yielding param %s" % param) + yield param + } + end + + # Does not actually return an object; instead sets an object + # in the current scope. + def evaluate(hash) + scope = hash[:scope] + + # Get our object reference. + object = @object.safeevaluate(:scope => scope) + + hash = {} + + # Evaluate all of the specified params. + params = @params.collect { |param| + param.safeevaluate(:scope => scope) + } + + # Now we just create a normal resource, but we call a very different + # method on the scope. + obj = Puppet::Parser::Resource.new( + :type => object.type, + :title => object.title, + :params => params, + :file => @file, + :line => @line, + :source => scope.source, + :scope => scope + ) + + # Now we tell the scope that it's an override, and it behaves as + # necessary. + scope.compile.store_override(obj) + + obj + end + + # Create our ResourceDef. Handles type checking for us. + def initialize(hash) + @checked = false + super + + #self.typecheck(@type.value) + end + end +end diff --git a/lib/puppet/parser/ast/resource_reference.rb b/lib/puppet/parser/ast/resource_reference.rb new file mode 100644 index 000000000..b8edff8f1 --- /dev/null +++ b/lib/puppet/parser/ast/resource_reference.rb @@ -0,0 +1,66 @@ +require 'puppet/parser/ast/branch' + +class Puppet::Parser::AST + # A reference to an object. Only valid as an rvalue. + class ResourceReference < AST::Branch + attr_accessor :title, :type + # Is the type a builtin type? + def builtintype?(type) + if typeklass = Puppet::Type.type(type) + return typeklass + else + return false + end + end + + def each + [@type,@title].flatten.each { |param| + #Puppet.debug("yielding param %s" % param) + yield param + } + end + + # Evaluate our object, but just return a simple array of the type + # and name. + def evaluate(hash) + scope = hash[:scope] + + title = @title.safeevaluate(:scope => scope) + if @type == "class" + objtype = @type + title = qualified_class(scope, title) + else + objtype = qualified_type(scope) + end + + return Puppet::Parser::Resource::Reference.new( + :type => objtype, :title => title + ) + end + + # Look up a fully qualified class name. + def qualified_class(scope, title) + # Look up the full path to the class + if classobj = scope.findclass(title) + title = classobj.classname + else + raise Puppet::ParseError, "Could not find class %s" % title + end + end + + # Look up a fully-qualified type. This method is + # also used in AST::Resource. + def qualified_type(scope, title = nil) + # We want a lower-case type. For some reason. + objtype = @type.downcase + unless builtintype?(objtype) + if dtype = scope.finddefine(objtype) + objtype = dtype.classname + else + raise Puppet::ParseError, "Could not find resource type %s" % objtype + end + end + return objtype + end + end +end diff --git a/lib/puppet/parser/ast/resourcedef.rb b/lib/puppet/parser/ast/resourcedef.rb deleted file mode 100644 index a432268f1..000000000 --- a/lib/puppet/parser/ast/resourcedef.rb +++ /dev/null @@ -1,75 +0,0 @@ -require 'puppet/parser/ast/resourceref' - -# Any normal puppet resource declaration. Can point to a definition or a -# builtin type. -class Puppet::Parser::AST -class ResourceDef < AST::ResourceRef - attr_accessor :title, :type, :exported, :virtual - attr_reader :params - - # Does not actually return an object; instead sets an object - # in the current scope. - def evaluate(options) - scope = options[:scope] - - # Evaluate all of the specified params. - paramobjects = @params.collect { |param| - param.safeevaluate(:scope => scope) - } - - objtitles = @title.safeevaluate(:scope => scope) - - # it's easier to always use an array, even for only one name - unless objtitles.is_a?(Array) - objtitles = [objtitles] - end - - objtype = qualified_type(scope) - - # This is where our implicit iteration takes place; if someone - # passed an array as the name, then we act just like the called us - # many times. - objtitles.collect { |objtitle| - exceptwrap :type => Puppet::ParseError do - exp = self.exported || scope.resource.exported? - # We want virtual to be true if exported is true. We can't - # just set :virtual => self.virtual in the initialization, - # because sometimes the :virtual attribute is set *after* - # :exported, in which case it clobbers :exported if :exported - # is true. Argh, this was a very tough one to track down. - virt = self.virtual || scope.resource.virtual? || exp - obj = Puppet::Parser::Resource.new( - :type => objtype, - :title => objtitle, - :params => paramobjects, - :file => self.file, - :line => self.line, - :exported => exp, - :virtual => virt, - :source => scope.source, - :scope => scope - ) - - # And then store the resource in the compile. - # At some point, we need to switch all of this to return - # objects instead of storing them like this. - scope.compile.store_resource(scope, obj) - obj - end - }.reject { |obj| obj.nil? } - end - - # Set the parameters for our object. - def params=(params) - if params.is_a?(AST::ASTArray) - @params = params - else - @params = AST::ASTArray.new( - :line => params.line, - :file => params.file, - :children => [params] - ) - end - end -end -end diff --git a/lib/puppet/parser/ast/resourcedefaults.rb b/lib/puppet/parser/ast/resourcedefaults.rb deleted file mode 100644 index 44ec146b0..000000000 --- a/lib/puppet/parser/ast/resourcedefaults.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'puppet/parser/ast/branch' - -class Puppet::Parser::AST - # A statement syntactically similar to an ResourceDef, but uses a - # capitalized object type and cannot have a name. - class ResourceDefaults < AST::Branch - attr_accessor :type, :params - - # As opposed to ResourceDef, this stores each default for the given - # object type. - def evaluate(hash) - scope = hash[:scope] - type = @type.downcase - params = @params.safeevaluate(:scope => scope) - - parsewrap do - scope.setdefaults(type, params) - end - end - end -end diff --git a/lib/puppet/parser/ast/resourceoverride.rb b/lib/puppet/parser/ast/resourceoverride.rb deleted file mode 100644 index 4232737fc..000000000 --- a/lib/puppet/parser/ast/resourceoverride.rb +++ /dev/null @@ -1,60 +0,0 @@ -require 'puppet/parser/ast/resourcedef' - -class Puppet::Parser::AST - # Set a parameter on a resource specification created somewhere else in the - # configuration. The object is responsible for verifying that this is allowed. - class ResourceOverride < ResourceDef - attr_accessor :object - attr_reader :params - - # Iterate across all of our children. - def each - [@object,@params].flatten.each { |param| - #Puppet.debug("yielding param %s" % param) - yield param - } - end - - # Does not actually return an object; instead sets an object - # in the current scope. - def evaluate(hash) - scope = hash[:scope] - - # Get our object reference. - object = @object.safeevaluate(:scope => scope) - - hash = {} - - # Evaluate all of the specified params. - params = @params.collect { |param| - param.safeevaluate(:scope => scope) - } - - # Now we just create a normal resource, but we call a very different - # method on the scope. - obj = Puppet::Parser::Resource.new( - :type => object.type, - :title => object.title, - :params => params, - :file => @file, - :line => @line, - :source => scope.source, - :scope => scope - ) - - # Now we tell the scope that it's an override, and it behaves as - # necessary. - scope.compile.store_override(obj) - - obj - end - - # Create our ResourceDef. Handles type checking for us. - def initialize(hash) - @checked = false - super - - #self.typecheck(@type.value) - end - end -end diff --git a/lib/puppet/parser/ast/resourceref.rb b/lib/puppet/parser/ast/resourceref.rb deleted file mode 100644 index 02a19d88d..000000000 --- a/lib/puppet/parser/ast/resourceref.rb +++ /dev/null @@ -1,66 +0,0 @@ -require 'puppet/parser/ast/branch' - -class Puppet::Parser::AST - # A reference to an object. Only valid as an rvalue. - class ResourceRef < AST::Branch - attr_accessor :title, :type - # Is the type a builtin type? - def builtintype?(type) - if typeklass = Puppet::Type.type(type) - return typeklass - else - return false - end - end - - def each - [@type,@title].flatten.each { |param| - #Puppet.debug("yielding param %s" % param) - yield param - } - end - - # Evaluate our object, but just return a simple array of the type - # and name. - def evaluate(hash) - scope = hash[:scope] - - title = @title.safeevaluate(:scope => scope) - if @type == "class" - objtype = @type - title = qualified_class(scope, title) - else - objtype = qualified_type(scope) - end - - return Puppet::Parser::Resource::Reference.new( - :type => objtype, :title => title - ) - end - - # Look up a fully qualified class name. - def qualified_class(scope, title) - # Look up the full path to the class - if classobj = scope.findclass(title) - title = classobj.classname - else - raise Puppet::ParseError, "Could not find class %s" % title - end - end - - # Look up a fully-qualified type. This method is - # also used in AST::Resource. - def qualified_type(scope, title = nil) - # We want a lower-case type. For some reason. - objtype = @type.downcase - unless builtintype?(objtype) - if dtype = scope.finddefine(objtype) - objtype = dtype.classname - else - raise Puppet::ParseError, "Could not find resource type %s" % objtype - end - end - return objtype - end - end -end diff --git a/lib/puppet/parser/grammar.ra b/lib/puppet/parser/grammar.ra index c7216186a..26cb42217 100644 --- a/lib/puppet/parser/grammar.ra +++ b/lib/puppet/parser/grammar.ra @@ -116,19 +116,19 @@ resourcerefs: resourceref resource: classname LBRACE resourceinstances endsemi RBRACE { array = val[2] - if array.instance_of?(AST::ResourceInst) + if array.instance_of?(AST::ResourceInstance) array = [array] end result = ast AST::ASTArray # this iterates across each specified resourceinstance array.each { |instance| - unless instance.instance_of?(AST::ResourceInst) + unless instance.instance_of?(AST::ResourceInstance) raise Puppet::Dev, "Got something that isn't an instance" end # now, i need to somehow differentiate between those things with # arrays in their names, and normal things - result.push ast(AST::ResourceDef, + result.push ast(AST::Resource, :type => val[0], :title => instance[0], :params => instance[1]) @@ -248,12 +248,12 @@ colllval: variable | name resourceinst: resourcename COLON params endcomma { - result = ast AST::ResourceInst, :children => [val[0],val[2]] + result = ast AST::ResourceInstance, :children => [val[0],val[2]] } resourceinstances: resourceinst | resourceinstances SEMIC resourceinst { - if val[0].instance_of?(AST::ResourceInst) + if val[0].instance_of?(AST::ResourceInstance) result = ast AST::ASTArray, :children => [val[0],val[2]] else val[0].push val[2] @@ -385,9 +385,9 @@ boolean: BOOLEAN { resourceref: NAME LBRACK rvalue RBRACK { Puppet.warning addcontext("Deprecation notice: Resource references should now be capitalized") - result = ast AST::ResourceRef, :type => val[0], :title => val[2] + result = ast AST::ResourceReference, :type => val[0], :title => val[2] } | classref LBRACK rvalue RBRACK { - result = ast AST::ResourceRef, :type => val[0], :title => val[2] + result = ast AST::ResourceReference, :type => val[0], :title => val[2] } ifstatement: IF iftest LBRACE statements RBRACE else { diff --git a/lib/puppet/parser/parser.rb b/lib/puppet/parser/parser.rb index aaffa816e..907bddc0a 100644 --- a/lib/puppet/parser/parser.rb +++ b/lib/puppet/parser/parser.rb @@ -29,7 +29,7 @@ module Puppet class Parser < Racc::Parser -module_eval <<'..end grammar.ra modeval..idc3bb85ed76', 'grammar.ra', 640 +module_eval <<'..end grammar.ra modeval..id8b4fcf8e20', 'grammar.ra', 640 # It got too annoying having code in a file that needs to be compiled. require 'puppet/parser/parser_support' @@ -41,7 +41,7 @@ require 'puppet/parser/parser_support' # $Id$ -..end grammar.ra modeval..idc3bb85ed76 +..end grammar.ra modeval..id8b4fcf8e20 ##### racc 1.4.5 generates ### @@ -929,19 +929,19 @@ module_eval <<'.,.,', 'grammar.ra', 115 module_eval <<'.,.,', 'grammar.ra', 136 def _reduce_34( val, _values, result ) array = val[2] - if array.instance_of?(AST::ResourceInst) + if array.instance_of?(AST::ResourceInstance) array = [array] end result = ast AST::ASTArray # this iterates across each specified resourceinstance array.each { |instance| - unless instance.instance_of?(AST::ResourceInst) + unless instance.instance_of?(AST::ResourceInstance) raise Puppet::Dev, "Got something that isn't an instance" end # now, i need to somehow differentiate between those things with # arrays in their names, and normal things - result.push ast(AST::ResourceDef, + result.push ast(AST::Resource, :type => val[0], :title => instance[0], :params => instance[1]) @@ -1115,7 +1115,7 @@ module_eval <<'.,.,', 'grammar.ra', 245 module_eval <<'.,.,', 'grammar.ra', 252 def _reduce_57( val, _values, result ) - result = ast AST::ResourceInst, :children => [val[0],val[2]] + result = ast AST::ResourceInstance, :children => [val[0],val[2]] result end .,., @@ -1124,7 +1124,7 @@ module_eval <<'.,.,', 'grammar.ra', 252 module_eval <<'.,.,', 'grammar.ra', 262 def _reduce_59( val, _values, result ) - if val[0].instance_of?(AST::ResourceInst) + if val[0].instance_of?(AST::ResourceInstance) result = ast AST::ASTArray, :children => [val[0],val[2]] else val[0].push val[2] @@ -1344,14 +1344,14 @@ module_eval <<'.,.,', 'grammar.ra', 384 module_eval <<'.,.,', 'grammar.ra', 389 def _reduce_105( val, _values, result ) Puppet.warning addcontext("Deprecation notice: Resource references should now be capitalized") - result = ast AST::ResourceRef, :type => val[0], :title => val[2] + result = ast AST::ResourceReference, :type => val[0], :title => val[2] result end .,., module_eval <<'.,.,', 'grammar.ra', 391 def _reduce_106( val, _values, result ) - result = ast AST::ResourceRef, :type => val[0], :title => val[2] + result = ast AST::ResourceReference, :type => val[0], :title => val[2] result end .,., -- cgit