Class: Puppet::Parser::Parser

Inherits:
Racc::Parser
  • Object
show all
Extended by:
Forwardable
Includes:
Resource::TypeCollectionHelper
Defined in:
lib/puppet/parser/parser.rb,
lib/puppet/parser/parser_support.rb

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"STRING",
"DQPRE",
"DQMID",
"DQPOST",
"LBRACK",
"RBRACK",
"LBRACE",
"RBRACE",
"SYMBOL",
"FARROW",
"COMMA",
"TRUE",
"FALSE",
"EQUALS",
"APPENDS",
"LESSEQUAL",
"NOTEQUAL",
"DOT",
"COLON",
"LLCOLLECT",
"RRCOLLECT",
"QMARK",
"LPAREN",
"RPAREN",
"ISEQUAL",
"GREATEREQUAL",
"GREATERTHAN",
"LESSTHAN",
"IF",
"ELSE",
"IMPORT",
"DEFINE",
"ELSIF",
"VARIABLE",
"CLASS",
"INHERITS",
"NODE",
"BOOLEAN",
"NAME",
"SEMIC",
"CASE",
"DEFAULT",
"AT",
"LCOLLECT",
"RCOLLECT",
"CLASSREF",
"NOT",
"OR",
"AND",
"UNDEF",
"PARROW",
"PLUS",
"MINUS",
"TIMES",
"DIV",
"LSHIFT",
"RSHIFT",
"UMINUS",
"MATCH",
"NOMATCH",
"REGEX",
"IN_EDGE",
"OUT_EDGE",
"IN_EDGE_SUB",
"OUT_EDGE_SUB",
"IN",
"UNLESS",
"MODULO",
"$start",
"program",
"statements_and_declarations",
"nil",
"statement_or_declaration",
"statements",
"resource",
"virtualresource",
"collection",
"assignment",
"casestatement",
"ifstatement_begin",
"unlessstatement",
"import",
"fstatement",
"definition",
"hostclass",
"nodedef",
"resourceoverride",
"append",
"relationship",
"keyword",
"relationship_side",
"edge",
"resourceref",
"variable",
"quotedtext",
"selector",
"hasharrayaccesses",
"expressions",
"funcvalues",
"rvalue",
"expression",
"comma",
"name",
"type",
"boolean",
"array",
"funcrvalue",
"undef",
"classname",
"resourceinstances",
"endsemi",
"params",
"endcomma",
"anyparams",
"at",
"collectrhand",
"collstatements",
"collstatement",
"colljoin",
"collexpr",
"colllval",
"resourceinst",
"resourcename",
"hasharrayaccess",
"param",
"param_name",
"addparam",
"anyparam",
"dqrval",
"dqtail",
"ifstatement",
"else",
"hash",
"regex",
"caseopts",
"caseopt",
"casevalues",
"selectlhand",
"svalues",
"selectval",
"sintvalues",
"string",
"strings",
"argumentlist",
"classparent",
"hostnames",
"nodeparent",
"nodename",
"hostname",
"nothing",
"arguments",
"argument",
"classnameordefault",
"hashpairs",
"hashpair",
"key" ]
Racc_debug_parser =
false
AST =
Puppet::Parser::AST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Resource::TypeCollectionHelper

#known_resource_types

Constructor Details

#initialize(env) ⇒ Parser

Returns a new instance of Parser.



123
124
125
126
# File 'lib/puppet/parser/parser_support.rb', line 123

def initialize(env)
  @environment = env
  initvars
end

Instance Attribute Details

#environmentObject (readonly)



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

def environment
  @environment
end

#filesObject



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

def files
  @files
end

#lexerObject



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

def lexer
  @lexer
end

#versionObject (readonly)



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

def version
  @version
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



2553
2554
2555
# File 'lib/puppet/parser/parser.rb', line 2553

def _reduce_none(val, _values, result)
  val[0]
end

#addcontext(message, obj = nil) ⇒ Object

Add context to a message; useful for error messages and such.



25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/parser/parser_support.rb', line 25

def addcontext(message, obj = nil)
  obj ||= @lexer

  message += " on line #{obj.line}"
  if file = obj.file
    message += " in file #{file}"
  end

  message
end

#aryfy(arg) ⇒ Object

Create an AST array containing a single element



37
38
39
# File 'lib/puppet/parser/parser_support.rb', line 37

def aryfy(arg)
  ast AST::ASTArray, :children => [arg]
end

#ast(klass, hash = {}) ⇒ Object

Create an AST object, and automatically add the file and line information if available.



48
49
50
# File 'lib/puppet/parser/parser_support.rb', line 48

def ast(klass, hash = {})
  klass.new ast_context(klass.use_docs, hash[:line]).merge(hash)
end

#ast_context(include_docs = false, ast_line = nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/puppet/parser/parser_support.rb', line 52

def ast_context(include_docs = false, ast_line = nil)
  result = {
    :line => ast_line || lexer.line,
    :file => lexer.file
  }
  result[:doc] = lexer.getcomment(result[:line]) if include_docs
  result
end

#block(arg) ⇒ Object

Create an AST block containing a single element



42
43
44
# File 'lib/puppet/parser/parser_support.rb', line 42

def block(arg)
  ast AST::BlockExpression, :children => [arg]
end

#classname(name) ⇒ Object

The fully qualifed name, with the full namespace.



62
63
64
# File 'lib/puppet/parser/parser_support.rb', line 62

def classname(name)
  [@lexer.namespace, name].join("::").sub(/^::/, '')
end

#clearObject



66
67
68
# File 'lib/puppet/parser/parser_support.rb', line 66

def clear
  initvars
end

#error(message, options = {}) ⇒ Object

Raise a Parse error.



71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/parser/parser_support.rb', line 71

def error(message, options = {})
  if @lexer.expected
    message += "; expected '%s'"
  end
  except = Puppet::ParseError.new(message)
  except.line = options[:line] || @lexer.line
  except.file = options[:file] || @lexer.file

  raise except
end

#file=(file) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/puppet/parser/parser_support.rb', line 84

def file=(file)
  unless Puppet::FileSystem.exist?(file)
    unless file =~ /\.pp$/
      file = file + ".pp"
    end
  end
  raise Puppet::AlreadyImportedError, "Import loop detected for #{file}" if known_resource_types.watching_file?(file)

  watch_file(file)
  @lexer.file = file
end

#import(file) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/puppet/parser/parser_support.rb', line 100

def import(file)
  deprecation_location_text =
  if @lexer.file && @lexer.line
    " at #{@lexer.file}:#{@lexer.line}"
  elsif @lexer.file
    " in file #{@lexer.file}"
  elsif @lexer.line
    " at #{@lexer.line}"
  end

  Puppet.deprecation_warning("The use of 'import' is deprecated#{deprecation_location_text}. See http://links.puppetlabs.com/puppet-import-deprecation")
  if @lexer.file
    # use a path relative to the file doing the importing
    dir = File.dirname(@lexer.file)
  else
    # otherwise assume that everything needs to be from where the user is
    # executing this command. Normally, this would be in a "puppet apply -e"
    dir = Dir.pwd
  end

  known_resource_types.loader.import(file, dir)
end

#initvarsObject

Initialize or reset all of our variables.



129
130
131
# File 'lib/puppet/parser/parser_support.rb', line 129

def initvars
  @lexer = Puppet::Parser::Lexer.new
end

#namesplit(fullname) ⇒ Object

Split an fq name into a namespace and name



134
135
136
137
138
139
# File 'lib/puppet/parser/parser_support.rb', line 134

def namesplit(fullname)
  ary = fullname.split("::")
  n = ary.pop || ""
  ns = ary.join("::")
  return ns, n
end

#on_error(token, value, stack) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/puppet/parser/parser_support.rb', line 141

def on_error(token,value,stack)
  if token == 0 # denotes end of file
    value = 'end of file'
  else
    value = "'#{value[:value]}'"
  end
  error = "Syntax error at #{value}"

  if brace = @lexer.expected
    error += "; expected '#{brace}'"
  end

  except = Puppet::ParseError.new(error)
  except.line = @lexer.line
  except.file = @lexer.file if @lexer.file

  raise except
end

#parse(string = nil) ⇒ Object

how should I do error handling here?



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/puppet/parser/parser_support.rb', line 161

def parse(string = nil)
  if self.file =~ /\.rb$/
    main = parse_ruby_file
  else
    self.string = string if string
    begin
      @yydebug = false
      main = yyparse(@lexer,:scan)
    rescue Puppet::ParseError => except
      except.line ||= @lexer.line
      except.file ||= @lexer.file
      except.pos ||= @lexer.pos
      raise except
    rescue => except
      raise Puppet::ParseError.new(except.message, @lexer.file, @lexer.line, nil, except)
    end
  end
  # Store the results as the top-level class.
  return Puppet::Parser::AST::Hostclass.new('', :code => main)
ensure
  @lexer.clear
end

#parse_ruby_fileObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/puppet/parser/parser_support.rb', line 184

def parse_ruby_file
  Puppet.deprecation_warning("Use of the Ruby DSL is deprecated.")

  # Execute the contents of the file inside its own "main" object so
  # that it can call methods in the resource type API.
  main_object = Puppet::DSL::ResourceTypeAPI.new
  main_object.instance_eval(File.read(self.file))

  # Then extract any types that were created.
  Puppet::Parser::AST::BlockExpression.new :children => main_object.instance_eval { @__created_ast_objects__ }
end