summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast/function.rb
blob: 98204b14ab654287f85821ab795f835afec52482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'puppet/parser/ast/branch'

class Puppet::Parser::AST
    # An AST object to call a function.
    class Function < AST::Branch

        associates_doc

        attr_accessor :name, :arguments

        @settor = true

        def evaluate(scope)

            # Make sure it's a defined function
            unless Puppet::Parser::Functions.function(@name)
                raise Puppet::ParseError, "Unknown function %s" % @name
            end

            # Now check that it's been used correctly
            case @ftype
            when :rvalue
                unless Puppet::Parser::Functions.rvalue?(@name)
                    raise Puppet::ParseError, "Function '%s' does not return a value" % @name
                end
            when :statement
                if Puppet::Parser::Functions.rvalue?(@name)
                    raise Puppet::ParseError,
                        "Function '%s' must be the value of a statement" % @name
                end
            else
                raise Puppet::DevError, "Invalid function type %s" % @ftype.inspect
            end

            # We don't need to evaluate the name, because it's plaintext
            args = @arguments.safeevaluate(scope)

            return scope.send("function_" + @name, args)
        end

        def initialize(hash)
            @ftype = hash[:ftype] || :rvalue
            hash.delete(:ftype) if hash.include? :ftype

            super(hash)

            # Lastly, check the parity
        end

        def to_s
            args = arguments.is_a?(ASTArray) ? arguments.to_s.gsub(/\[(.*)\]/,'\1') : arguments
            "#{name}(#{args})"
        end
    end
end