diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb index 012090e44..7c878cdfd 100644 --- a/lib/puppet/parser/ast.rb +++ b/lib/puppet/parser/ast.rb @@ -1,65 +1,62 @@ -# the parent class for all of our syntactical objects - require 'puppet' require 'puppet/util/autoload' -# The base class for all of the objects that make up the parse trees. +# The base class for the 3x "parse tree", now only used by the top level +# constructs and the compiler. # Handles things like file name, line #, and also does the initialization # for all of the parameters of all of the child objects. +# class Puppet::Parser::AST - # Do this so I don't have to type the full path in all of the subclasses AST = Puppet::Parser::AST include Puppet::Util::Errors include Puppet::Util::MethodHelper - include Puppet::Util::Docs attr_accessor :parent, :scope, :file, :line, :pos def inspect "( #{self.class} #{self.to_s} #{@children.inspect} )" end # Evaluate the current object. Just a stub method, since the subclass # should override this method. def evaluate(*options) end # The version of the evaluate method that should be called, because it # correctly handles errors. It is critical to use this method because # it can enable you to catch the error where it happens, rather than # much higher up the stack. def safeevaluate(*options) # We duplicate code here, rather than using exceptwrap, because this # is called so many times during parsing. begin return self.evaluate(*options) rescue Puppet::Error => detail raise adderrorcontext(detail) rescue => detail error = Puppet::ParseError.new(detail.to_s, nil, nil, detail) # We can't use self.fail here because it always expects strings, # not exceptions. raise adderrorcontext(error, detail) end end # Initialize the object. Requires a hash as the argument, and # takes each of the parameters of the hash and calls the settor # method for them. This is probably pretty inefficient and should # likely be changed at some point. def initialize(args) set_options(args) end end # And include all of the AST subclasses. -require 'puppet/parser/ast/astarray' require 'puppet/parser/ast/block_expression' require 'puppet/parser/ast/hostclass' # PUP-3274 cannot remove until environment uses a different representation require 'puppet/parser/ast/leaf' require 'puppet/parser/ast/node' require 'puppet/parser/ast/resource' require 'puppet/parser/ast/resource_instance' require 'puppet/parser/ast/resourceparam' diff --git a/lib/puppet/parser/ast/astarray.rb b/lib/puppet/parser/ast/astarray.rb deleted file mode 100644 index 42a47a1fd..000000000 --- a/lib/puppet/parser/ast/astarray.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'puppet/parser/ast/branch' - -class Puppet::Parser::AST - # The basic container class. This object behaves almost identically - # to a normal array except at initialization time. Note that its name - # is 'AST::ASTArray', rather than plain 'AST::Array'; I had too many - # bugs when it was just 'AST::Array', because things like - # 'object.is_a?(Array)' never behaved as I expected. - class ASTArray < Branch - include Enumerable - - # Return a child by index. Used (at least) by tests. - def [](index) - @children[index] - end - - # Evaluate our children. - def evaluate(scope) - result = [] - @children.each do |child| - # Skip things that respond to :instantiate (classes, nodes, - # and definitions), because they have already been - # instantiated. - if !child.respond_to?(:instantiate) - item = child.safeevaluate(scope) - if !item.nil? - # nil values are implicitly removed. - result.push(item) - end - end - end - result - end - - def push(*ary) - ary.each { |child| - #Puppet.debug "adding %s(%s) of type %s to %s" % - # [child, child.object_id, child.class.to_s.sub(/.+::/,''), - # self.object_id] - @children.push(child) - } - - self - end - - def to_s - "[" + @children.collect { |c| c.to_s }.join(', ') + "]" - end - end -end diff --git a/lib/puppet/pops/binder/bindings_model_dumper.rb b/lib/puppet/pops/binder/bindings_model_dumper.rb index 98b2049fd..4883f4dd6 100644 --- a/lib/puppet/pops/binder/bindings_model_dumper.rb +++ b/lib/puppet/pops/binder/bindings_model_dumper.rb @@ -1,187 +1,179 @@ # Dumps a Pops::Binder::Bindings model in reverse polish notation; i.e. LISP style # The intention is to use this for debugging output # TODO: BAD NAME - A DUMP is a Ruby Serialization # NOTE: use :break, :indent, :dedent in lists to do just that # class Puppet::Pops::Binder::BindingsModelDumper < Puppet::Pops::Model::TreeDumper Bindings = Puppet::Pops::Binder::Bindings attr_reader :type_calculator attr_reader :expression_dumper def initialize super @type_calculator = Puppet::Pops::Types::TypeCalculator.new() @expression_dumper = Puppet::Pops::Model::ModelTreeDumper.new() end def dump_BindingsFactory o do_dump(o.model) end def dump_BindingsBuilder o do_dump(o.model) end def dump_BindingsContainerBuilder o do_dump(o.model) end def dump_NamedLayer o result = ['named-layer', (o.name.nil? ? '': o.name), :indent] if o.bindings o.bindings.each do |b| result << :break result << do_dump(b) end end result << :dedent result end def dump_Array o o.collect {|e| do_dump(e) } end - def dump_ASTArray o - ["[]"] + o.children.collect {|x| do_dump(x)} - end - - def dump_ASTHash o - ["{}"] + o.value.sort_by{|k,v| k.to_s}.collect {|x| [do_dump(x[0]), do_dump(x[1])]} - end - def dump_Integer o o.to_s end # Dump a Ruby String in single quotes unless it is a number. def dump_String o "'#{o}'" end def dump_NilClass o "()" end def dump_Object o ['dev-error-no-polymorph-dump-for:', o.class.to_s, o.to_s] end def is_nop? o o.nil? || o.is_a?(Model::Nop) || o.is_a?(AST::Nop) end def dump_ProducerDescriptor o result = [o.class.name] result << expression_dumper.dump(o.transformer) if o.transformer result end def dump_NonCachingProducerDescriptor o dump_ProducerDescriptor(o) + do_dump(o.producer) end def dump_ConstantProducerDescriptor o ['constant', do_dump(o.value)] end def dump_EvaluatingProducerDescriptor o result = dump_ProducerDescriptor(o) result << expression_dumper.dump(o.expression) end def dump_InstanceProducerDescriptor o # TODO: o.arguments, o. transformer ['instance', o.class_name] end def dump_ProducerProducerDescriptor o # skip the transformer lambda... result = ['producer-producer', do_dump(o.producer)] result << expression_dumper.dump(o.transformer) if o.transformer result end def dump_LookupProducerDescriptor o ['lookup', do_dump(o.type), o.name] end def dump_PAnyType o type_calculator.string(o) end def dump_HashLookupProducerDescriptor o # TODO: transformer lambda result = ['hash-lookup', do_dump(o.type), o.name, "[#{do_dump(o.key)}]"] result << expression_dumper.dump(o.transformer) if o.transformer result end def dump_FirstFoundProducerDescriptor o # TODO: transformer lambda ['first-found', do_dump(o.producers)] end def dump_ArrayMultibindProducerDescriptor o ['multibind-array'] end def dump_HashMultibindProducerDescriptor o ['multibind-hash'] end def dump_NamedArgument o "#{o.name} => #{do_dump(o.value)}" end def dump_Binding o result = ['bind'] result << 'override' if o.override result << 'abstract' if o.abstract result.concat([do_dump(o.type), o.name]) result << "(in #{o.multibind_id})" if o.multibind_id result << ['to', do_dump(o.producer)] + do_dump(o.producer_args) result end def dump_Multibinding o result = ['multibind', o.id] result << 'override' if o.override result << 'abstract' if o.abstract result.concat([do_dump(o.type), o.name]) result << "(in #{o.multibind_id})" if o.multibind_id result << ['to', do_dump(o.producer)] + do_dump(o.producer_args) result end def dump_Bindings o do_dump(o.bindings) end def dump_NamedBindings o result = ['named-bindings', o.name, :indent] o.bindings.each do |b| result << :break result << do_dump(b) end result << :dedent result end def dump_LayeredBindings o result = ['layers', :indent] o.layers.each do |layer| result << :break result << do_dump(layer) end result << :dedent result end def dump_ContributedBindings o ['contributed', o.name, do_dump(o.bindings)] end end diff --git a/spec/unit/parser/ast/astarray_spec.rb b/spec/unit/parser/ast/astarray_spec.rb deleted file mode 100755 index 95cab5b76..000000000 --- a/spec/unit/parser/ast/astarray_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -#! /usr/bin/env ruby -require 'spec_helper' - -describe 'Puppet::Parser::AST::ASTArray' do - before :each do - node = Puppet::Node.new('localhost') - compiler = Puppet::Parser::Compiler.new(node) - @scope = Puppet::Parser::Scope.new(compiler) - end - - it "should have a [] accessor" do - array = Puppet::Parser::AST::ASTArray.new :children => [] - array.should respond_to(:[]) - end - - it "should evaluate all its children" do - item1 = stub "item1", :is_a? => true - item2 = stub "item2", :is_a? => true - - item1.expects(:safeevaluate).with(@scope).returns(123) - item2.expects(:safeevaluate).with(@scope).returns(246) - - operator = Puppet::Parser::AST::ASTArray.new :children => [item1,item2] - operator.evaluate(@scope) - end - - it "should not flatten children coming from children ASTArray" do - item = Puppet::Parser::AST::Leaf.new :value => 'foo' - inner_array = Puppet::Parser::AST::ASTArray.new :children => [item, item] - operator = Puppet::Parser::AST::ASTArray.new :children => [inner_array, inner_array] - operator.evaluate(@scope).should == [['foo', 'foo'], ['foo', 'foo']] - end - - it "should not flatten the results of children evaluation" do - item = Puppet::Parser::AST::Leaf.new :value => 'foo' - item.stubs(:evaluate).returns(['foo']) - operator = Puppet::Parser::AST::ASTArray.new :children => [item, item] - operator.evaluate(@scope).should == [['foo'], ['foo']] - end - - it "should discard nil results from children evaluation" do - item1 = Puppet::Parser::AST::Leaf.new :value => 'foo' - item2 = Puppet::Parser::AST::Leaf.new :value => 'foo' - item2.stubs(:evaluate).returns(nil) - operator = Puppet::Parser::AST::ASTArray.new :children => [item1, item2] - operator.evaluate(@scope).should == ['foo'] - end - - it "should return a valid string with to_s" do - a = stub 'a', :is_a? => true, :to_s => "a" - b = stub 'b', :is_a? => true, :to_s => "b" - array = Puppet::Parser::AST::ASTArray.new :children => [a,b] - - array.to_s.should == "[a, b]" - end -end