diff options
| author | Luke Kanies <luke@madstop.com> | 2007-09-04 12:09:48 -0500 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2007-09-04 12:09:48 -0500 |
| commit | 3b2efd2a4b32478b6c6a71e1421061405a0bb11e (patch) | |
| tree | 72e5ada4b0eeb3c83a1f20dfb524363a3fc5db81 /lib/puppet/parser/ast | |
| parent | 0faf76ee187c7fa7c67a7fb7e7c345897006b7d8 (diff) | |
We now have a real configuration object, as a subclass of GRATR::Digraph, that has a resource graph including resources for the container objects like classes and nodes. It is apparently functional, but I have not gone through all of the other tests to fix them yet. That is next.
Diffstat (limited to 'lib/puppet/parser/ast')
22 files changed, 41 insertions, 351 deletions
diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb index c0212f919..2a8f4d4c1 100644 --- a/lib/puppet/parser/ast/astarray.rb +++ b/lib/puppet/parser/ast/astarray.rb @@ -65,21 +65,6 @@ class Puppet::Parser::AST return self end - - # Convert to a string. Only used for printing the parse tree. - def to_s - return "[" + @children.collect { |child| - child.to_s - }.join(", ") + "]" - end - - # Print the parse tree. - def tree(indent = 0) - #puts((AST.indent * indent) + self.pin) - self.collect { |child| - child.tree(indent) - }.join("\n" + (AST.midline * (indent+1)) + "\n") - end end # A simple container class, containing the parameters for an object. @@ -88,5 +73,3 @@ class Puppet::Parser::AST # meant completely different things. class ResourceInst < ASTArray; end end - -# $Id$ diff --git a/lib/puppet/parser/ast/branch.rb b/lib/puppet/parser/ast/branch.rb index 5a4d323f5..dcd09baa3 100644 --- a/lib/puppet/parser/ast/branch.rb +++ b/lib/puppet/parser/ast/branch.rb @@ -35,15 +35,5 @@ class Puppet::Parser::AST end } end - - # Pretty-print the parse tree. - def tree(indent = 0) - return ((@@indline * indent) + - self.typewrap(self.pin)) + "\n" + self.collect { |child| - child.tree(indent + 1) - }.join("\n") - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/caseopt.rb b/lib/puppet/parser/ast/caseopt.rb index 11483d082..d1d9d0e9c 100644 --- a/lib/puppet/parser/ast/caseopt.rb +++ b/lib/puppet/parser/ast/caseopt.rb @@ -56,16 +56,5 @@ class Puppet::Parser::AST def evaluate(hash) return @statements.safeevaluate(hash) end - - def tree(indent = 0) - rettree = [ - @value.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)), - @statements.tree(indent + 1) - ] - return rettree.flatten.join("\n") - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/casestatement.rb b/lib/puppet/parser/ast/casestatement.rb index c56e33bc9..3c6f9c7e2 100644 --- a/lib/puppet/parser/ast/casestatement.rb +++ b/lib/puppet/parser/ast/casestatement.rb @@ -51,20 +51,8 @@ class Puppet::Parser::AST return retvalue end - def tree(indent = 0) - rettree = [ - @test.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)), - @options.tree(indent + 1) - ] - - return rettree.flatten.join("\n") - end - def each [@test,@options].each { |child| yield child } end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/collection.rb b/lib/puppet/parser/ast/collection.rb index a7564f0c9..e05977a47 100644 --- a/lib/puppet/parser/ast/collection.rb +++ b/lib/puppet/parser/ast/collection.rb @@ -26,5 +26,3 @@ class Collection < AST::Branch end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/collexpr.rb b/lib/puppet/parser/ast/collexpr.rb index 5fb11c709..4a96d9c61 100644 --- a/lib/puppet/parser/ast/collexpr.rb +++ b/lib/puppet/parser/ast/collexpr.rb @@ -77,5 +77,3 @@ class CollExpr < AST::Branch end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/component.rb b/lib/puppet/parser/ast/component.rb index 17cfa9d61..1d7fe9cdd 100644 --- a/lib/puppet/parser/ast/component.rb +++ b/lib/puppet/parser/ast/component.rb @@ -222,5 +222,3 @@ class Puppet::Parser::AST end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/definition.rb b/lib/puppet/parser/ast/definition.rb index c350c2cdb..9ad1f539d 100644 --- a/lib/puppet/parser/ast/definition.rb +++ b/lib/puppet/parser/ast/definition.rb @@ -33,8 +33,6 @@ class Puppet::Parser::AST # Create a new scope. scope = subscope(origscope, resource) - scope.virtual = true if resource.virtual or origscope.virtual? - scope.exported = true if resource.exported or origscope.exported? # Additionally, add a tag for whatever kind of class # we are @@ -126,21 +124,16 @@ class Puppet::Parser::AST # Create a new subscope in which to evaluate our code. def subscope(scope, resource = nil) - if resource - type = resource.type - else - type = self.classname + unless resource + raise ArgumentError, "Resources are required when creating subscopes" end args = { :resource => resource, - :type => type, :keyword => self.keyword, :namespace => self.namespace, :source => self } - args[:name] = resource.title if resource - oldscope = scope scope = scope.newscope(args) scope.source = self diff --git a/lib/puppet/parser/ast/else.rb b/lib/puppet/parser/ast/else.rb index ff2f233b7..e76051372 100644 --- a/lib/puppet/parser/ast/else.rb +++ b/lib/puppet/parser/ast/else.rb @@ -16,15 +16,5 @@ class Puppet::Parser::AST scope = hash[:scope] return @statements.safeevaluate(:scope => scope) end - - def tree(indent = 0) - rettree = [ - ((@@indline * indent) + self.typewrap(self.pin)), - @statements.tree(indent + 1) - ] - return rettree.flatten.join("\n") - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/function.rb b/lib/puppet/parser/ast/function.rb index 052b8a8b1..0cd1fff62 100644 --- a/lib/puppet/parser/ast/function.rb +++ b/lib/puppet/parser/ast/function.rb @@ -53,5 +53,3 @@ class Puppet::Parser::AST end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/hostclass.rb b/lib/puppet/parser/ast/hostclass.rb index 41ca34432..0a8e33970 100644 --- a/lib/puppet/parser/ast/hostclass.rb +++ b/lib/puppet/parser/ast/hostclass.rb @@ -22,6 +22,7 @@ class Puppet::Parser::AST # Evaluate the code associated with this class. def evaluate(options) scope = options[:scope] + raise(ArgumentError, "Classes require resources") unless options[:resource] # Verify that we haven't already been evaluated. This is # what provides the singleton aspect. if existing_scope = scope.compile.class_scope(self) @@ -31,13 +32,15 @@ class Puppet::Parser::AST pnames = nil if pklass = self.parentobj - pklass.safeevaluate :scope => scope + pklass.safeevaluate :scope => scope, :resource => options[:resource] scope = parent_scope(scope, pklass) pnames = scope.namespaces end - unless options[:nosubscope] + # Don't create a subscope for the top-level class, since it already + # has its own scope. + unless options[:resource].title == :main scope = subscope(scope, options[:resource]) end diff --git a/lib/puppet/parser/ast/ifstatement.rb b/lib/puppet/parser/ast/ifstatement.rb index 300f68dab..66a07b01f 100644 --- a/lib/puppet/parser/ast/ifstatement.rb +++ b/lib/puppet/parser/ast/ifstatement.rb @@ -26,18 +26,5 @@ class Puppet::Parser::AST end end end - - def tree(indent = 0) - rettree = [ - @test.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)), - @statements.tree(indent + 1), - @else.tree(indent + 1) - ] - - return rettree.flatten.join("\n") - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb index c2fd0939d..225253061 100644 --- a/lib/puppet/parser/ast/leaf.rb +++ b/lib/puppet/parser/ast/leaf.rb @@ -10,11 +10,6 @@ class Puppet::Parser::AST return @value end - # Print the value in parse tree context. - def tree(indent = 0) - return ((@@indent * indent) + self.typewrap(self.value)) - end - def to_s return @value end @@ -92,7 +87,4 @@ class Puppet::Parser::AST end end end - end - -# $Id$ diff --git a/lib/puppet/parser/ast/node.rb b/lib/puppet/parser/ast/node.rb index d46df3cff..20c03f4ce 100644 --- a/lib/puppet/parser/ast/node.rb +++ b/lib/puppet/parser/ast/node.rb @@ -7,7 +7,6 @@ class Puppet::Parser::AST @name = :node attr_accessor :name - #def evaluate(scope, facts = {}) def evaluate(options) scope = options[:scope] @@ -24,7 +23,7 @@ class Puppet::Parser::AST end scope = scope.newscope( - :type => self.name, + :resource => options[:resource], :keyword => @keyword, :source => self, :namespace => "" # nodes are always in "" diff --git a/lib/puppet/parser/ast/resourcedef.rb b/lib/puppet/parser/ast/resourcedef.rb index 7a43f6b32..02eac2b7b 100644 --- a/lib/puppet/parser/ast/resourcedef.rb +++ b/lib/puppet/parser/ast/resourcedef.rb @@ -1,88 +1,27 @@ require 'puppet/parser/ast/branch' -# Any normal puppet object declaration. Can result in a class or a -# component, in addition to builtin types. +# Any normal puppet resource declaration. Can point to a definition or a +# builtin type. class Puppet::Parser::AST class ResourceDef < AST::Branch attr_accessor :title, :type, :exported, :virtual attr_reader :params - # probably not used at all - def []=(index,obj) - @params[index] = obj - end - - # probably not used at all - def [](index) - return @params[index] - end - - # Iterate across all of our children. - def each - [@type,@title,@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] - @scope = scope - hash = {} - - # Get our type and name. - objtype = @type - - # Disable definition inheritance, for now. 8/27/06, luke - #if objtype == "super" - # objtype = supertype() - # @subtype = true - #else - @subtype = false - #end + def evaluate(options) + scope = options[:scope] # Evaluate all of the specified params. paramobjects = @params.collect { |param| param.safeevaluate(:scope => scope) } - # Now collect info from our parent. - parentname = nil - if @subtype - parentname = supersetup(hash) - end - - objtitles = nil - # Determine our name if we have one. - if self.title - 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 - else - if parentname - objtitles = [parentname] - else - # See if they specified the name as a parameter instead of - # as a normal name (i.e., before the colon). - unless object # we're a builtin - if objclass = Puppet::Type.type(objtype) - namevar = objclass.namevar + objtitles = @title.safeevaluate(:scope => scope) - tmp = hash["name"] || hash[namevar.to_s] - - if tmp - objtitles = [tmp] - end - else - # This isn't grammatically legal. - raise Puppet::ParseError, "Got a resource with no title" - end - end - end + # it's easier to always use an array, even for only one name + unless objtitles.is_a?(Array) + objtitles = [objtitles] end # This is where our implicit iteration takes place; if someone @@ -90,26 +29,26 @@ class ResourceDef < AST::Branch # many times. objtitles.collect { |objtitle| exceptwrap :type => Puppet::ParseError do - exp = self.exported || scope.exported? + 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.virtual? || exported + virt = self.virtual || scope.resource.virtual? || exp obj = Puppet::Parser::Resource.new( - :type => objtype, + :type => @type, :title => objtitle, :params => paramobjects, - :file => @file, - :line => @line, + :file => self.file, + :line => self.line, :exported => exp, :virtual => virt, :source => scope.source, :scope => scope ) - # And then store the resource in the scope. + # And then store the resource in the compile. # XXX 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) @@ -118,14 +57,6 @@ class ResourceDef < AST::Branch }.reject { |obj| obj.nil? } end - # Create our ResourceDef. Handles type checking for us. - def initialize(hash) - @checked = false - super - - #self.typecheck(@type.value) - end - # Set the parameters for our object. def params=(params) if params.is_a?(AST::ASTArray) @@ -138,85 +69,5 @@ class ResourceDef < AST::Branch ) end end - - def supercomp - unless defined? @supercomp - if @scope and comp = @scope.inside - @supercomp = comp - else - error = Puppet::ParseError.new( - "'super' is only valid within definitions" - ) - error.line = self.line - error.file = self.file - raise error - end - end - @supercomp - end - - # Take all of the arguments of our parent and add them into our own, - # without overriding anything. - def supersetup(hash) - comp = supercomp() - - # Now check each of the arguments from the parent. - comp.arguments.each do |name, value| - unless hash.has_key? name - hash[name] = value - end - end - - # Return the parent name, so it can be used if appropriate. - return comp.name - end - - # Retrieve our supertype. - def supertype - unless defined? @supertype - if parent = supercomp.parentclass - @supertype = parent - else - error = Puppet::ParseError.new( - "%s does not have a parent class" % comp.type - ) - error.line = self.line - error.file = self.file - raise error - end - end - @supertype - end - - # Print this object out. - def tree(indent = 0) - return [ - @type.tree(indent + 1), - @title.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)), - @params.collect { |param| - begin - param.tree(indent + 1) - rescue NoMethodError => detail - Puppet.err @params.inspect - error = Puppet::DevError.new( - "failed to tree a %s" % self.class - ) - error.set_backtrace detail.backtrace - raise error - end - }.join("\n") - ].join("\n") - end - - def to_s - return "%s => { %s }" % [@title, - @params.collect { |param| - param.to_s - }.join("\n") - ] - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/resourcedefaults.rb b/lib/puppet/parser/ast/resourcedefaults.rb index df16b1b59..44ec146b0 100644 --- a/lib/puppet/parser/ast/resourcedefaults.rb +++ b/lib/puppet/parser/ast/resourcedefaults.rb @@ -6,10 +6,6 @@ class Puppet::Parser::AST class ResourceDefaults < AST::Branch attr_accessor :type, :params - def each - [@type,@params].each { |child| yield child } - end - # As opposed to ResourceDef, this stores each default for the given # object type. def evaluate(hash) @@ -21,20 +17,5 @@ class Puppet::Parser::AST scope.setdefaults(type, params) end end - - def tree(indent = 0) - return [ - @type.tree(indent + 1), - ((@@indline * 4 * indent) + self.typewrap(self.pin)), - @params.tree(indent + 1) - ].join("\n") - end - - def to_s - return "%s { %s }" % [@type,@params] - end end - end - -# $Id$ diff --git a/lib/puppet/parser/ast/resourceoverride.rb b/lib/puppet/parser/ast/resourceoverride.rb index 418c9c8e4..4232737fc 100644 --- a/lib/puppet/parser/ast/resourceoverride.rb +++ b/lib/puppet/parser/ast/resourceoverride.rb @@ -58,5 +58,3 @@ class Puppet::Parser::AST end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/resourceparam.rb b/lib/puppet/parser/ast/resourceparam.rb index d87720160..8b1e7b367 100644 --- a/lib/puppet/parser/ast/resourceparam.rb +++ b/lib/puppet/parser/ast/resourceparam.rb @@ -20,19 +20,5 @@ class Puppet::Parser::AST :add => self.add ) end - - def tree(indent = 0) - return [ - @param.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)), - @value.tree(indent + 1) - ].join("\n") - end - - def to_s - return "%s => %s" % [@param,@value] - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/resourceref.rb b/lib/puppet/parser/ast/resourceref.rb index e5bb69a46..167417f13 100644 --- a/lib/puppet/parser/ast/resourceref.rb +++ b/lib/puppet/parser/ast/resourceref.rb @@ -4,6 +4,14 @@ 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| @@ -21,38 +29,24 @@ class Puppet::Parser::AST objtype = @type.downcase title = @title.safeevaluate(:scope => scope) - if scope.builtintype?(objtype) - # nothing - elsif dtype = scope.finddefine(objtype) - objtype = dtype.classname - elsif objtype == "class" - # Look up the full path to the class - if classobj = scope.findclass(title) - title = classobj.classname + unless builtintype?(objtype) + if dtype = scope.finddefine(objtype) + objtype = dtype.classname + elsif objtype == "class" + # 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 else - raise Puppet::ParseError, "Could not find class %s" % title + raise Puppet::ParseError, "Could not find resource type %s" % objtype end - else - raise Puppet::ParseError, "Could not find resource type %s" % objtype end return Puppet::Parser::Resource::Reference.new( :type => objtype, :title => title ) end - - def tree(indent = 0) - return [ - @type.tree(indent + 1), - @title.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)) - ].join("\n") - end - - def to_s - return "%s[%s]" % [@type,@title] - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/selector.rb b/lib/puppet/parser/ast/selector.rb index e5eb3b6f5..d363ab7e4 100644 --- a/lib/puppet/parser/ast/selector.rb +++ b/lib/puppet/parser/ast/selector.rb @@ -60,15 +60,5 @@ class Puppet::Parser::AST return retvalue end - - def tree(indent = 0) - return [ - @param.tree(indent + 1), - ((@@indline * indent) + self.typewrap(self.pin)), - @values.tree(indent + 1) - ].join("\n") - end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/tag.rb b/lib/puppet/parser/ast/tag.rb index 4a2015cde..e2882d2f0 100644 --- a/lib/puppet/parser/ast/tag.rb +++ b/lib/puppet/parser/ast/tag.rb @@ -24,5 +24,3 @@ class Puppet::Parser::AST end end end - -# $Id$ diff --git a/lib/puppet/parser/ast/vardef.rb b/lib/puppet/parser/ast/vardef.rb index ecc10bd14..1e7f874bc 100644 --- a/lib/puppet/parser/ast/vardef.rb +++ b/lib/puppet/parser/ast/vardef.rb @@ -22,20 +22,6 @@ class Puppet::Parser::AST def each [@name,@value].each { |child| yield child } end - - def tree(indent = 0) - return [ - @name.tree(indent + 1), - ((@@indline * 4 * indent) + self.typewrap(self.pin)), - @value.tree(indent + 1) - ].join("\n") - end - - def to_s - return "%s => %s" % [@name,@value] - end end end - -# $Id$ |
