Class: Puppet::Parser::AST

Inherits:
Object show all
Includes:
FileCollection::Lookup, Util::Docs, Util::Errors, Util::MethodHelper
Defined in:
lib/puppet/parser/ast/not.rb,
lib/puppet/parser/ast/nop.rb,
lib/puppet/parser/ast/tag.rb,
lib/puppet/parser/ast/else.rb,
lib/puppet/parser/ast/leaf.rb,
lib/puppet/parser/ast/minus.rb,
lib/puppet/parser/ast/branch.rb,
lib/puppet/parser/ast/vardef.rb,
lib/puppet/parser/ast/asthash.rb,
lib/puppet/parser/ast/caseopt.rb,
lib/puppet/parser/ast/astarray.rb,
lib/puppet/parser/ast/collexpr.rb,
lib/puppet/parser/ast/function.rb,
lib/puppet/parser/ast/resource.rb,
lib/puppet/parser/ast/selector.rb,
lib/puppet/parser/ast/collection.rb,
lib/puppet/parser/ast/ifstatement.rb,
lib/puppet/parser/ast/in_operator.rb,
lib/puppet/parser/ast/casestatement.rb,
lib/puppet/parser/ast/resourceparam.rb,
lib/puppet/parser/ast/match_operator.rb,
lib/puppet/parser/ast/boolean_operator.rb,
lib/puppet/parser/ast/resource_defaults.rb,
lib/puppet/parser/ast/resource_override.rb,
lib/puppet/parser/ast/arithmetic_operator.rb,
lib/puppet/parser/ast/comparison_operator.rb,
lib/puppet/parser/ast.rb

Overview

The base class for all of the objects that make up the parse trees. Handles things like file name, line #, and also does the initialization for all of the parameters of all of the child objects.

Direct Known Subclasses

Branch, Leaf

Defined Under Namespace

Classes: ASTArray, ASTHash, ArithmeticOperator, Boolean, BooleanOperator, Branch, CaseOpt, CaseStatement, ClassName, CollExpr, Collection, ComparisonOperator, Concat, Default, Else, FlatString, Function, HashOrArrayAccess, HostName, IfStatement, InOperator, Leaf, MatchOperator, Minus, Name, Nop, Not, Regex, Relationship, Resource, ResourceDefaults, ResourceInstance, ResourceOverride, ResourceParam, ResourceReference, Selector, String, Tag, Type, Undef, VarDef, Variable

Constant Summary collapse

AST =

Do this so I don’t have to type the full path in all of the subclasses

Puppet::Parser::AST

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Attributes included from FileCollection::Lookup

#file_index, #line

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Docs

#desc, #dochook, #doctable, #nodoc?, #pad, scrub

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail

Methods included from FileCollection::Lookup

#file, #file=, #file_collection

Constructor Details

#initialize(args) ⇒ AST

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.



91
92
93
# File 'lib/puppet/parser/ast.rb', line 91

def initialize(args)
  set_options(args)
end

Class Attribute Details

.use_docsObject

Returns the value of attribute use_docs.



33
34
35
# File 'lib/puppet/parser/ast.rb', line 33

def use_docs
  @use_docs
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



20
21
22
# File 'lib/puppet/parser/ast.rb', line 20

def parent
  @parent
end

#scopeObject

Returns the value of attribute scope.



20
21
22
# File 'lib/puppet/parser/ast.rb', line 20

def scope
  @scope
end

Class Method Details

.associates_docObject



34
35
36
# File 'lib/puppet/parser/ast.rb', line 34

def associates_doc
self.use_docs = true
end

.settor?Boolean

Does this ast object set something? If so, it gets evaluated first.

Returns:



40
41
42
43
44
45
46
# File 'lib/puppet/parser/ast.rb', line 40

def self.settor?
  if defined?(@settor)
    @settor
  else
    false
  end
end

Instance Method Details

#evaluate(*options) ⇒ Object

Evaluate the current object. Just a stub method, since the subclass should override this method. of the contained children and evaluates them in turn, returning a list of all of the collected values, rejecting nil values

Raises:



52
53
54
# File 'lib/puppet/parser/ast.rb', line 52

def evaluate(*options)
  raise Puppet::DevError, "Did not override #evaluate in #{self.class}"
end

#evaluate_match(value, scope) ⇒ Object

evaluate ourselves, and match



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet/parser/ast.rb', line 96

def evaluate_match(value, scope)
  obj = self.safeevaluate(scope)

  obj   = obj.downcase   if obj.respond_to?(:downcase)
  value = value.downcase if value.respond_to?(:downcase)

  obj   = Puppet::Parser::Scope.number?(obj)   || obj
  value = Puppet::Parser::Scope.number?(value) || value

  # "" == undef for case/selector/if
  obj == value or (obj == "" and value == :undef)
end

#inspectObject



22
23
24
# File 'lib/puppet/parser/ast.rb', line 22

def inspect
  "( #{self.class} #{self.to_s} #{@children.inspect} )"
end

#parsefail(message) ⇒ Object

Throw a parse error.



57
58
59
# File 'lib/puppet/parser/ast.rb', line 57

def parsefail(message)
  self.fail(Puppet::ParseError, message)
end

#parsewrapObject

Wrap a statemp in a reusable way so we always throw a parse error.



62
63
64
65
66
# File 'lib/puppet/parser/ast.rb', line 62

def parsewrap
  exceptwrap :type => Puppet::ParseError do
    yield
  end
end

#safeevaluate(*options) ⇒ Object

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppet/parser/ast.rb', line 72

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::Error.new(detail.to_s)
    # We can't use self.fail here because it always expects strings,
    # not exceptions.
    raise adderrorcontext(error, detail)
  end
end

#use_docsObject

don’t fetch lexer comment by default



27
28
29
# File 'lib/puppet/parser/ast.rb', line 27

def use_docs
  self.class.use_docs
end