diff options
Diffstat (limited to 'lib/puppet/parser/ast')
-rw-r--r-- | lib/puppet/parser/ast/asthash.rb | 34 | ||||
-rw-r--r-- | lib/puppet/parser/ast/leaf.rb | 21 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lib/puppet/parser/ast/asthash.rb b/lib/puppet/parser/ast/asthash.rb new file mode 100644 index 000000000..2860657e0 --- /dev/null +++ b/lib/puppet/parser/ast/asthash.rb @@ -0,0 +1,34 @@ +require 'puppet/parser/ast/leaf' + +class Puppet::Parser::AST + class ASTHash < Leaf + include Enumerable + + def [](index) + end + + # Evaluate our children. + def evaluate(scope) + items = {} + + @value.each_pair do |k,v| + items.merge!({ k => v.safeevaluate(scope) }) + end + + return items + end + + def merge(hash) + case hash + when ASTHash + @value = @value.merge(hash.value) + when Hash + @value = @value.merge(hash) + end + end + + def to_s + "{" + @value.collect { |v| v.collect { |a| a.to_s }.join(' => ') }.join(', ') + "}" + end + end +end diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb index b583149b3..206ea5cbe 100644 --- a/lib/puppet/parser/ast/leaf.rb +++ b/lib/puppet/parser/ast/leaf.rb @@ -139,6 +139,27 @@ class Puppet::Parser::AST end end + class HashOrArrayAccess < AST::Leaf + attr_accessor :variable, :key + + def evaluate(scope) + container = variable.respond_to?(:evaluate) ? variable.safeevaluate(scope) : variable + object = (container.is_a?(Hash) or container.is_a?(Array)) ? container : scope.lookupvar(container) + + accesskey = key.respond_to?(:evaluate) ? key.safeevaluate(scope) : key + + unless object.is_a?(Hash) or object.is_a?(Array) + raise Puppet::ParseError, "#{variable} is not an hash or array when accessing it with #{accesskey}" + end + + return object[accesskey] + end + + def to_s + "\$#{variable.to_s}[#{key.to_s}]" + end + end + class Regex < AST::Leaf def initialize(hash) super |