Sha256: 42a67cc1d8a9d9909696ec852499ccdb85b696cdf512cc38ef72e934e56ada8a

Contents?: true

Size: 1.94 KB

Versions: 90

Compression:

Stored size: 1.94 KB

Contents

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, :pblock

    def evaluate(scope)
      # Make sure it's a defined function
      raise Puppet::ParseError, "Unknown function #{@name}" unless Puppet::Parser::Functions.function(@name)

      # Now check that it's been used correctly
      case @ftype
      when :rvalue
        raise Puppet::ParseError, "Function '#{@name}' does not return a value" unless Puppet::Parser::Functions.rvalue?(@name)

      when :statement
        # It is harmless to produce an ignored rvalue, the alternative is to mark functions
        # as appropriate for both rvalue and statements
        # Keeping the old behavior when a pblock is not present. This since it is not known
        # if the lambda contains a statement or not (at least not without a costly search).
        # The purpose of the check is to protect a user for producing a meaningless rvalue where the
        # operation has no side effects.
        #
        if !pblock && Puppet::Parser::Functions.rvalue?(@name)
          raise Puppet::ParseError,
            "Function '#{@name}' must be the value of a statement"
        end
      else
        raise Puppet::DevError, "Invalid function type #{@ftype.inspect}"
      end

      # We don't need to evaluate the name, because it's plaintext
      args = @arguments.safeevaluate(scope).map { |x| x == :undef ? '' : x }

      # append a puppet lambda (unevaluated) if it is defined
      args << pblock if pblock

      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

Version data entries

90 entries across 90 versions & 2 rubygems

Version Path
puppet-retrospec-0.12.2 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-3.8.7 lib/puppet/parser/ast/function.rb
puppet-3.8.7-x86-mingw32 lib/puppet/parser/ast/function.rb
puppet-3.8.7-x64-mingw32 lib/puppet/parser/ast/function.rb
puppet-3.8.6 lib/puppet/parser/ast/function.rb
puppet-3.8.6-x86-mingw32 lib/puppet/parser/ast/function.rb
puppet-retrospec-0.12.1 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-3.8.6-x64-mingw32 lib/puppet/parser/ast/function.rb
puppet-retrospec-0.12.0 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-3.8.5 lib/puppet/parser/ast/function.rb
puppet-3.8.5-x86-mingw32 lib/puppet/parser/ast/function.rb
puppet-3.8.5-x64-mingw32 lib/puppet/parser/ast/function.rb
puppet-3.8.4 lib/puppet/parser/ast/function.rb
puppet-3.8.4-x86-mingw32 lib/puppet/parser/ast/function.rb
puppet-3.8.4-x64-mingw32 lib/puppet/parser/ast/function.rb
puppet-retrospec-0.11.0 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-retrospec-0.10.0 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-retrospec-0.9.1 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-retrospec-0.9.0 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb
puppet-retrospec-0.8.1 vendor/gems/puppet-3.7.3/lib/puppet/parser/ast/function.rb