Class: Puppet::Parser::AST

Inherits:
Object show all
Includes:
Util::Docs, Util::Errors, Util::MethodHelper
Defined in:
lib/vendor/puppet/parser/ast/not.rb,
lib/vendor/puppet/parser/ast/nop.rb,
lib/vendor/puppet/parser/ast/tag.rb,
lib/vendor/puppet/parser/ast/else.rb,
lib/vendor/puppet/parser/ast/leaf.rb,
lib/vendor/puppet/parser/ast/minus.rb,
lib/vendor/puppet/parser/ast/branch.rb,
lib/vendor/puppet/parser/ast/vardef.rb,
lib/vendor/puppet/parser/ast/asthash.rb,
lib/vendor/puppet/parser/ast/caseopt.rb,
lib/vendor/puppet/parser/ast/astarray.rb,
lib/vendor/puppet/parser/ast/collexpr.rb,
lib/vendor/puppet/parser/ast/function.rb,
lib/vendor/puppet/parser/ast/resource.rb,
lib/vendor/puppet/parser/ast/selector.rb,
lib/vendor/puppet/parser/ast/collection.rb,
lib/vendor/puppet/parser/ast/ifstatement.rb,
lib/vendor/puppet/parser/ast/in_operator.rb,
lib/vendor/puppet/parser/ast/casestatement.rb,
lib/vendor/puppet/parser/ast/resourceparam.rb,
lib/vendor/puppet/parser/ast/match_operator.rb,
lib/vendor/puppet/parser/ast/boolean_operator.rb,
lib/vendor/puppet/parser/ast/resource_defaults.rb,
lib/vendor/puppet/parser/ast/resource_instance.rb,
lib/vendor/puppet/parser/ast/resource_override.rb,
lib/vendor/puppet/parser/ast/arithmetic_operator.rb,
lib/vendor/puppet/parser/ast/comparison_operator.rb,
lib/vendor/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, TopLevelConstruct

Defined Under Namespace

Classes: ASTArray, ASTHash, ArithmeticOperator, Boolean, BooleanOperator, Branch, CaseOpt, CaseStatement, ClassName, CollExpr, Collection, ComparisonOperator, Concat, Default, Definition, Else, FlatString, Function, HashOrArrayAccess, HostName, Hostclass, IfStatement, InOperator, Leaf, MatchOperator, Minus, Name, Node, Nop, Not, Regex, Relationship, Resource, ResourceDefaults, ResourceInstance, ResourceOverride, ResourceParam, ResourceReference, Selector, String, Tag, TopLevelConstruct, 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

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Docs

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

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Errors

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

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.



88
89
90
# File 'lib/vendor/puppet/parser/ast.rb', line 88

def initialize(args)
  set_options(args)
end

Class Attribute Details

.use_docsObject

Returns the value of attribute use_docs.



30
31
32
# File 'lib/vendor/puppet/parser/ast.rb', line 30

def use_docs
  @use_docs
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



17
18
19
# File 'lib/vendor/puppet/parser/ast.rb', line 17

def file
  @file
end

#lineObject

Returns the value of attribute line.



17
18
19
# File 'lib/vendor/puppet/parser/ast.rb', line 17

def line
  @line
end

#parentObject

Returns the value of attribute parent.



17
18
19
# File 'lib/vendor/puppet/parser/ast.rb', line 17

def parent
  @parent
end

#scopeObject

Returns the value of attribute scope.



17
18
19
# File 'lib/vendor/puppet/parser/ast.rb', line 17

def scope
  @scope
end

Class Method Details

.associates_docObject



31
32
33
# File 'lib/vendor/puppet/parser/ast.rb', line 31

def associates_doc
self.use_docs = true
end

.settor?Boolean

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

Returns:



37
38
39
40
41
42
43
# File 'lib/vendor/puppet/parser/ast.rb', line 37

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:



49
50
51
# File 'lib/vendor/puppet/parser/ast.rb', line 49

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

#evaluate_match(value, scope) ⇒ Object

evaluate ourselves, and match



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vendor/puppet/parser/ast.rb', line 93

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) or (obj == :undef and value == "")
end

#inspectObject



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

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

#parsefail(message) ⇒ Object

Throw a parse error.



54
55
56
# File 'lib/vendor/puppet/parser/ast.rb', line 54

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

#parsewrapObject

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



59
60
61
62
63
# File 'lib/vendor/puppet/parser/ast.rb', line 59

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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vendor/puppet/parser/ast.rb', line 69

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



24
25
26
# File 'lib/vendor/puppet/parser/ast.rb', line 24

def use_docs
  self.class.use_docs
end