Class: Puppet::Parser::Parser

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

Overview

I pulled this into a separate file, because I got tired of rebuilding the parser.rb file all the time.

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",
"$start",
"program",
"statements_and_declarations",
"nil",
"statement_or_declaration",
"statements",
"resource",
"virtualresource",
"collection",
"assignment",
"casestatement",
"ifstatement_begin",
"import",
"fstatement",
"definition",
"hostclass",
"nodedef",
"resourceoverride",
"append",
"relationship",
"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",
"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.



107
108
109
110
111
# File 'lib/vendor/puppet/parser/parser_support.rb', line 107

def initialize(env)
  # The environment is needed to know how to find the resource type collection.
  @environment = env.is_a?(String) ? Puppet::Node::Environment.new(env) : env
  initvars
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



15
16
17
# File 'lib/vendor/puppet/parser/parser_support.rb', line 15

def environment
  @environment
end

#filesObject

Returns the value of attribute files.



16
17
18
# File 'lib/vendor/puppet/parser/parser_support.rb', line 16

def files
  @files
end

#lexerObject

Returns the value of attribute lexer.



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

def lexer
  @lexer
end

#versionObject (readonly)

Returns the value of attribute version.



15
16
17
# File 'lib/vendor/puppet/parser/parser_support.rb', line 15

def version
  @version
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



2415
2416
2417
# File 'lib/vendor/puppet/parser/parser.rb', line 2415

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.



21
22
23
24
25
26
27
28
29
30
# File 'lib/vendor/puppet/parser/parser_support.rb', line 21

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



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

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.



39
40
41
# File 'lib/vendor/puppet/parser/parser_support.rb', line 39

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



43
44
45
46
47
48
49
50
# File 'lib/vendor/puppet/parser/parser_support.rb', line 43

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

#classname(name) ⇒ Object

The fully qualifed name, with the full namespace.



53
54
55
# File 'lib/vendor/puppet/parser/parser_support.rb', line 53

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

#clearObject



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

def clear
  initvars
end

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

Raise a Parse error.



62
63
64
65
66
67
68
69
70
71
# File 'lib/vendor/puppet/parser/parser_support.rb', line 62

def error(message, options = {})
  if brace = @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

#fileObject



73
74
75
# File 'lib/vendor/puppet/parser/parser_support.rb', line 73

def file
  @lexer.file
end

#file=(file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vendor/puppet/parser/parser_support.rb', line 77

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

  watch_file(file)
  @lexer.file = file
end

#find_definition(namespace, name) ⇒ Object



99
100
101
# File 'lib/vendor/puppet/parser/parser_support.rb', line 99

def find_definition(namespace, name)
  known_resource_types.find_definition(namespace, name)
end

#find_hostclass(namespace, name) ⇒ Object



95
96
97
# File 'lib/vendor/puppet/parser/parser_support.rb', line 95

def find_hostclass(namespace, name)
  known_resource_types.find_hostclass(namespace, name)
end

#import(file) ⇒ Object



103
104
105
# File 'lib/vendor/puppet/parser/parser_support.rb', line 103

def import(file)
  known_resource_types.loader.import(file, @lexer.file)
end

#initvarsObject

Initialize or reset all of our variables.



114
115
116
# File 'lib/vendor/puppet/parser/parser_support.rb', line 114

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

#namesplit(fullname) ⇒ Object

Split an fq name into a namespace and name



119
120
121
122
123
124
# File 'lib/vendor/puppet/parser/parser_support.rb', line 119

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

#on_error(token, value, stack) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vendor/puppet/parser/parser_support.rb', line 126

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?



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vendor/puppet/parser/parser_support.rb', line 146

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 Racc::ParseError => except
      error = Puppet::ParseError.new(except)
      error.line = @lexer.line
      error.file = @lexer.file
      error.set_backtrace except.backtrace
      raise error
    rescue Puppet::ParseError => except
      except.line ||= @lexer.line
      except.file ||= @lexer.file
      raise except
    rescue Puppet::Error => except
      # and this is a framework error
      except.line ||= @lexer.line
      except.file ||= @lexer.file
      raise except
    rescue Puppet::DevError => except
      except.line ||= @lexer.line
      except.file ||= @lexer.file
      raise except
    rescue => except
      error = Puppet::DevError.new(except.message)
      error.line = @lexer.line
      error.file = @lexer.file
      error.set_backtrace except.backtrace
      raise error
    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



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

def parse_ruby_file
  # 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::ASTArray.new :children => main_object.instance_eval { @__created_ast_objects__ }
end

#string=(string) ⇒ Object



197
198
199
# File 'lib/vendor/puppet/parser/parser_support.rb', line 197

def string=(string)
  @lexer.string = string
end

#watch_file(filename) ⇒ Object

Add a new file to be checked when we’re checking to see if we should be reparsed. This is basically only used by the TemplateWrapper to let the parser know about templates that should be parsed.



208
209
210
# File 'lib/vendor/puppet/parser/parser_support.rb', line 208

def watch_file(filename)
  known_resource_types.watch_file(filename)
end