Class: Connect::Dsl

Inherits:
Racc::Parser show all
Extended by:
Forwardable
Defined in:
lib/connect/dsl.rb,
lib/connect/lexer.rb,
lib/connect/parser.rb
more...

Overview

This class contains all methods called by the DSL parser

rubocop:disable ClassLength

Defined Under Namespace

Classes: ScanError

Constant Summary collapse

STATE_VARIABLES =

This instance variables are saved when pushing and popping parser state The parser is popped and pushed on includes

[
  :ss,
  :lineno,
  :current_file,
  :racc_state,
  :racc_t,
  :racc_val,
  :racc_read_next,
]
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",
"SCOPE",
"IDENTIFIER",
"INTEGER",
"FLOAT",
"\"-\"",
"\"+\"",
"connection",
"DO",
"\"{\"",
"END",
"\"}\"",
"DOUBLE_QUOTED",
"SINGLE_QUOTED",
"REGEXP",
"BOOLEAN",
"UNDEF",
"\",\"",
"\"^\"",
"\"*\"",
"\"/\"",
"OR",
"AND",
"\"[\"",
"\"]\"",
"DOUBLE_DOTS",
"\".\"",
"\"(\"",
"\")\"",
"\"&\"",
"\":\"",
"WITH",
"HASH_ROCKET",
"\"=\"",
"INCLUDE",
"INTO",
"IMPORT",
"FROM",
"TO",
"ITERATE",
"STEP",
"$start",
"dsl",
"config",
"number",
"assignment",
"include",
"import",
"object_definition",
"with",
"block_begin",
"block_end",
"literal",
"string",
"regexp",
"scalar",
"string_number_reference",
"reference",
"value",
"array",
"hash",
"object_reference",
"values",
"expression",
"parameters",
"parameter",
"selectors",
"selector",
"array_selector",
"function_selector",
"special_selector",
"with_scope_do",
"optional_comma",
"hashpairs",
"hashpair",
"hashkey",
"hash_seperator",
"import_with_scope_begin",
"import_with_scope_end",
"datasource",
"import_block",
"import_statements",
"import_statement",
"iterators",
"definition_block",
"iterator",
"step" ]
Racc_debug_parser =
false

Constants inherited from Racc::Parser

Racc::Parser::Racc_Runtime_Core_Revision_R, Racc::Parser::Racc_Runtime_Core_Version_R, Racc::Parser::Racc_Runtime_Revision, Racc::Parser::Racc_Runtime_Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Racc::Parser

#_racc_do_parse_rb, #_racc_do_reduce, #_racc_evalact, #_racc_init_sysvars, #_racc_setup, #_racc_yyparse_rb, #do_parse, #on_error, #racc_accept, #racc_e_pop, #racc_next_state, #racc_print_stacks, #racc_print_states, #racc_read_token, #racc_reduce, racc_runtime_type, #racc_shift, #racc_token2str, #token_to_str, #yyaccept, #yyerrok, #yyerror, #yyparse

Constructor Details

#initialize(values_table = nil, objects_table = nil, interpolator = nil, includer = nil) ⇒ Dsl

Returns a new instance of Dsl.

[View source]

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/connect/dsl.rb', line 105

def initialize(values_table    = nil,
                  objects_table = nil,
                  interpolator  = nil,
                  includer      = nil
                  )
  # @yydebug = true
  @values_table  = values_table || ValuesTable.new
  @objects_table = objects_table || ObjectsTable.new
  Connect::Entry::Base.values_table  = @values_table
  Connect::Entry::Base.objects_table = @objects_table
  @interpolator  = interpolator || Interpolator.new(@values_table)
  @includer      = includer || Includer.new
  @include_stack = []
  @current_scope = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.


69
70
71
# File 'lib/connect/dsl.rb', line 69

def config
  @config
end

#current_fileObject (readonly)

Returns the value of attribute current_file.


69
70
71
# File 'lib/connect/dsl.rb', line 69

def current_file
  @current_file
end

#filenameObject (readonly)

Returns the value of attribute filename.


16
17
18
# File 'lib/connect/lexer.rb', line 16

def filename
  @filename
end

#interpolatorObject (readonly)

Returns the value of attribute interpolator.


69
70
71
# File 'lib/connect/dsl.rb', line 69

def interpolator
  @interpolator
end

#linenoObject (readonly)

Returns the value of attribute lineno.


15
16
17
# File 'lib/connect/lexer.rb', line 15

def lineno
  @lineno
end

#stateObject

Returns the value of attribute state.


17
18
19
# File 'lib/connect/lexer.rb', line 17

def state
  @state
end

Class Method Details

.configObject

[View source]

93
94
95
# File 'lib/connect/dsl.rb', line 93

def config
  @config ||= Config.new
end

.instance(include_dir) ⇒ Object

Coveneance function to create a dsl object for a specfic include directory

[View source]

100
101
102
103
# File 'lib/connect/dsl.rb', line 100

def self.instance(include_dir)
  includer = Includer.new(include_dir)
  new(nil, nil, nil, includer)
end

Instance Method Details

#_next_tokenObject

[View source]

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/connect/lexer.rb', line 56

def _next_token
  text = @ss.peek(1)
  @lineno  +=  1  if text == "\n"
  token = case @state
  when nil
    case
    when (text = @ss.scan(/\n/))
      ;

    when (text = @ss.scan(/\#.*\n/))
       action { @lineno += 1; nil}

    when (text = @ss.scan(/\#.*$/))
      ;

    when (text = @ss.scan(/iterate\s/))
       action { [:ITERATE, text]}

    when (text = @ss.scan(/step\s/))
       action { [:STEP, text]}

    when (text = @ss.scan(/or\s|(\|\|)/))
       action { [:OR, text]}

    when (text = @ss.scan(/and\s|\&\&/))
       action { [:AND, text]}

    when (text = @ss.scan(/do\s/))
       action { [:DO, text]}

    when (text = @ss.scan(/end\s/))
       action { [:END, text]}

    when (text = @ss.scan(/from\s/))
       action { [:FROM, text]}

    when (text = @ss.scan(/to\s/))
       action { [:TO, text]}

    when (text = @ss.scan(/import\s/))
       action { [:IMPORT, text]}

    when (text = @ss.scan(/into\s/))
       action { [:INTO, text]}

    when (text = @ss.scan(/with\s/))
       action { [:WITH, text]}

    when (text = @ss.scan(/include\s/))
       action { [:INCLUDE, text] }

    when (text = @ss.scan(/TRUE|true/))
       action { [:BOOLEAN, true]}

    when (text = @ss.scan(/FALSE|false/))
       action { [:BOOLEAN, false]}

    when (text = @ss.scan(/undefined|undef|nil/))
       action { [:UNDEF, nil]}

    when (text = @ss.scan(/(?:(?:[a-zA-Z][a-zA-Z0-9_]*)?::)+/))
       action { [:SCOPE, text]}

    when (text = @ss.scan(/[a-zA-Z][a-zA-Z0-9_]*/))
       action { [:IDENTIFIER, text] }

    when (text = @ss.scan(/\=\>/))
       action { [:HASH_ROCKET, text]}

    when (text = @ss.scan(/[0-9]+\.[0-9]+/))
       action { [:FLOAT, text.to_f] }

    when (text = @ss.scan(/[0-9]+/))
       action { [:INTEGER, text.to_i] }

    when (text = @ss.scan(/\"(\\.|[^\\"])*\"/))
       action { [:DOUBLE_QUOTED, dequote(text)]}

    when (text = @ss.scan(/\'(\\.|[^\\'])*\'/))
       action { [:SINGLE_QUOTED, dequote(text)]}

    when (text = @ss.scan(/\/.*\//))
       action { [:REGEXP, dequote(text)]}

    when (text = @ss.scan(/\.\./))
       action { [:DOUBLE_DOTS, text]}

    when (text = @ss.scan(/[\s|\t]+/))
      ;

    when (text = @ss.scan(/./))
       action { [text, text] }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  else
    raise  ScanError, "undefined state: '" + state.to_s + "'"
  end  # case state
  token
end

#_reduce_none(val, _values, result) ⇒ Object

[View source]

1020
1021
1022
# File 'lib/connect/parser.rb', line 1020

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

#actionObject

[View source]

25
26
27
# File 'lib/connect/lexer.rb', line 25

def action
  yield
end

#add_objects_with_iterators(type, name, values, xdef, iterators) ⇒ Object

[View source]

256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/connect/dsl.rb', line 256

def add_objects_with_iterators(type, name, values, xdef, iterators)
  name = interpolated_value(name)
  iterator_values = {}
  iterators.each_pair {|k,v| iterator_values[k] = values_from_iterator(v,k)}
  max_size  = iterator_values.collect{|k,v| v.size}.max
  iterator_values.each_pair {|k,v| iterator_values[k] = v * ((max_size/v.size) + (max_size % v.size))}
  (0..max_size).each do | index|
    value_hash = iterator_values.keys.reduce({}) {|v,k| v.merge!({k.to_sym => iterator_values[k][index]})}
    object_name   = name % value_hash
    object_values = substitute_values(values, value_hash)
    add_object(type, object_name, object_values, xdef)
  end
end

#assign(name, value, xdef = nil) ⇒ Object

Assign the value to the name

Parameters:

  • name (String)

    the name of the assignment

  • value (Any)

    the value of the assignment

[View source]

127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/connect/dsl.rb', line 127

def assign(name, value, xdef = nil)
  interpolator_not_allowed(name, 'lvar in assignment')
  process_multiline_value(value)
  if value.is_a?(Connect::Entry::Base)
    Connect.debug "Assign #{value.inspect} to #{name}."
  else
    Connect.debug "Assign #{value} to #{name}."
  end
  name = scoped_name_for(name)
  entry = ValuesTable.value_entry(name, value, nil, xdef)
  add_value(entry)
end

#datasource(name, *parameters) ⇒ Object

create a datasource with the specfied name. A datasource object will be created who’s type is Connect::Datasources::name. The Puppet autoloader will try to locate this object in any of the loaded gems and modules.

When it is found, it is initialized whit the name and the passed parameters

Parameters:

  • name (String)

    the name of the datasource

  • parameters (Array)

    an arry of parameters to pass to the datasource

[View source]

213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/connect/dsl.rb', line 213

def datasource(name, *parameters)
  name = interpolated_value(name)
  Connect.debug "Loading datasource #{name}"
  source_name = name.to_s.split('_').collect(&:capitalize).join # Camelize
  klass_name = "Connect::Datasources::#{source_name}"
  klass = Puppet::Pops::Types::ClassLoader.provide_from_string(klass_name)
  if klass
    @current_importer = klass.new(name, *parameters)
  else
    fail ArgumentError, "specfied importer '#{name}' doesn't exist"
  end
end

#define_object(type, name, values = nil, iterators = nil, xdef = nil) ⇒ Object

Define an object. If the values is empty, this method returns just the values. It the values parameter is set, a new entry will be added to the objects table

[View source]

243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/connect/dsl.rb', line 243

def define_object(type, name, values = nil, iterators = nil, xdef = nil)
  name = interpolated_value(name)
  Connect.debug("Defining object #{name} as type #{type}.")
  fail ArgumentError, 'Iterators only allowed with block definition' if values.nil? && !iterators.nil?
  validate_iterators(iterators) unless iterators.nil?
  if iterators
    add_objects_with_iterators(type, name, values, xdef, iterators)
  else
    add_object(type, name, values, xdef) if values
  end
  Entry::ObjectReference.new(type, name, nil, xdef)
end

#dequote(line) ⇒ Object

def _next_token

[View source]

160
161
162
163
# File 'lib/connect/lexer.rb', line 160

def dequote(line)
  line.chop![0] = ''
  line
end

#double_quoted(value, xref = nil) ⇒ Object

[View source]

140
141
142
143
144
145
146
# File 'lib/connect/dsl.rb', line 140

def double_quoted(value, xref = nil)
  if contains_interpolation?(value)
    interpolate(value, xref)
  else
    value
  end
end

#import(variable_name, lookup) ⇒ Object

Import the specified data into the values list

[View source]

230
231
232
233
234
235
236
# File 'lib/connect/dsl.rb', line 230

def import(variable_name, lookup)
  name = interpolated_value(name)
  Connect.debug "Importing variable #{variable_name}."
  fail 'no current importer' unless @current_importer
  value = @current_importer.lookup(lookup)
  assign(variable_name, value)
end

#include_file(names, scope = nil) ⇒ Object

include the specfied file in the parse process.

[View source]

189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/connect/dsl.rb', line 189

def include_file(names, scope = nil)
  in_scope(scope) do
    names = interpolated_value(names)
    @includer.include(names) do |content, file_name|
      push_current_parse_state
      @current_file = file_name
      Connect.debug "parsing Connect config file #{file_name}."
      scan_str(content) unless empty_definition?(content)
      pop_current_parse_state
    end
  end
end

#interpolate(value, xref = nil) ⇒ Object

Connect the variable to an other variable in the value table

[View source]

173
174
175
# File 'lib/connect/dsl.rb', line 173

def interpolate(value, xref = nil)
  Entry::Interpolator.new(value, nil, xref)
end

#load_file(filename) ⇒ Object

[View source]

35
36
37
38
39
40
# File 'lib/connect/lexer.rb', line 35

def load_file( filename )
  @filename = filename
  open(filename, "r") do |f|
    scan_setup(f.read)
  end
end

#lookup_objects(type, keys) ⇒ Object

[View source]

148
149
150
151
152
153
154
# File 'lib/connect/dsl.rb', line 148

def lookup_objects(type, keys)
  @objects_table.entries(type, keys).reduce([]) do | c, v |
    type = v[0]
    name = v[1]
    c << [type, lookup_object(type, name).full_representation]
  end
end

#lookup_values(keys) ⇒ Object

[View source]

156
157
158
# File 'lib/connect/dsl.rb', line 156

def lookup_values(keys)
  @values_table.entries(keys).reduce([]) {| c, v| c << [v,lookup_value(v)]}
end

#next_tokenObject

[View source]

48
49
50
51
52
53
54
# File 'lib/connect/lexer.rb', line 48

def next_token
  return if @ss.eos?
  
  # skips empty actions
  until token = _next_token or @ss.eos?; end
  token
end

#pop_scopeObject

Pop’s the last scope from the stack

[View source]

302
303
304
# File 'lib/connect/dsl.rb', line 302

def pop_scope
  @current_scope.pop
end

#push_scope(scope) ⇒ Object

Push the current variable scope to the stack.

Parameters:

  • scope (String)

    the new scope scope

[View source]

294
295
296
# File 'lib/connect/dsl.rb', line 294

def push_scope(scope)
  @current_scope << scope unless scope.nil?
end

#reference(parameter, xref = nil) ⇒ Object

Connect the variable to an other variable in the value table

[View source]

181
182
183
184
# File 'lib/connect/dsl.rb', line 181

def reference(parameter, xref = nil)
  value_reference(parameter, xref)
  Entry::Reference.new(parameter, nil, xref)
end

#reference_object(type, name, xref = nil) ⇒ Object

Create an object reference.

[View source]

274
275
276
277
278
# File 'lib/connect/dsl.rb', line 274

def reference_object(type, name, xref = nil)
  name = interpolated_value(name)
  object_reference(type, name, xref)
  Entry::ObjectReference.new(type, name, nil, xref)
end

#reference_objects(type, regexp_str, xref = nil) ⇒ Object

Create an regexp object reference.

[View source]

284
285
286
287
# File 'lib/connect/dsl.rb', line 284

def reference_objects(type, regexp_str, xref = nil)
  regexp = Regexp.new(regexp_str)
  Entry::RegexpObjectReference.new(type, regexp, nil, xref)
end

#scan_file(filename) ⇒ Object

[View source]

42
43
44
45
# File 'lib/connect/lexer.rb', line 42

def scan_file( filename )
  load_file(filename)
  do_parse
end

#scan_setup(str) ⇒ Object

[View source]

19
20
21
22
23
# File 'lib/connect/lexer.rb', line 19

def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end

#scan_str(str) ⇒ Object Also known as: scan

[View source]

29
30
31
32
# File 'lib/connect/lexer.rb', line 29

def scan_str(str)
  scan_setup(str)
  do_parse
end

#selector(value, selector) ⇒ Object

Add the selector to the last object

[View source]

164
165
166
167
# File 'lib/connect/dsl.rb', line 164

def selector(value, selector)
  value.selector = selector
  value
end

#to_param(parameters) ⇒ Object

Translate strings in a set of parameters to quoted strings

Parameters:

  • parameters (Array)

    the set of parameters to recieve from the parser

[View source]

312
313
314
315
# File 'lib/connect/dsl.rb', line 312

def to_param(parameters)
  parameters = [parameters] unless parameters.is_a?(Array)
  parameters.collect { |p| p.is_a?(String) ? "'#{p}'" : p }.join(',')
end

#tokenize(code) ⇒ Object

[View source]

164
165
166
167
168
169
170
171
# File 'lib/connect/lexer.rb', line 164

def tokenize(code)
  scan_setup(code)
  tokens = []
  while token = next_token
    tokens << token
  end
  tokens
end

#xdefObject

[View source]

321
322
323
# File 'lib/connect/dsl.rb', line 321

def xdef
  Xdef.new(current_file, lineno)
end

#xrefObject

[View source]

317
318
319
# File 'lib/connect/dsl.rb', line 317

def xref
  Xref.new(current_file, lineno)
end