choria-mcorpc-support 2.23.0 → 2.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,118 +0,0 @@
1
- # rubocop:disable Metrics/BlockNesting
2
- module MCollective
3
- module Matcher
4
- class Parser
5
- attr_reader :scanner, :execution_stack
6
-
7
- def initialize(args)
8
- @scanner = Scanner.new(args)
9
- @execution_stack = []
10
- @parse_errors = []
11
- @token_errors = []
12
- @paren_errors = []
13
- parse
14
- exit_with_token_errors unless @token_errors.empty?
15
- exit_with_parse_errors unless @parse_errors.empty?
16
- exit_with_paren_errors unless @paren_errors.empty?
17
- end
18
-
19
- # Exit and highlight any malformed tokens
20
- def exit_with_token_errors
21
- @token_errors.each do |error_range|
22
- (error_range[0]..error_range[1]).each do |i|
23
- @scanner.arguments[i] = Util.colorize(:red, @scanner.arguments[i])
24
- end
25
- end
26
- raise "Malformed token(s) found while parsing -S input #{@scanner.arguments.join}"
27
- end
28
-
29
- def exit_with_parse_errors
30
- @parse_errors.each do |error_range|
31
- (error_range[0]..error_range[1]).each do |i|
32
- @scanner.arguments[i] = Util.colorize(:red, @scanner.arguments[i])
33
- end
34
- end
35
- raise "Parse errors found while parsing -S input #{@scanner.arguments.join}"
36
- end
37
-
38
- def exit_with_paren_errors
39
- @paren_errors.each do |i|
40
- @scanner.arguments[i] = Util.colorize(:red, @scanner.arguments[i])
41
- end
42
- raise "Missing parenthesis found while parsing -S input #{@scanner.arguments.join}"
43
- end
44
-
45
- # Parse the input string, one token at a time a contruct the call stack
46
- def parse # rubocop:disable Metrics/MethodLength
47
- pre_index = @scanner.token_index
48
- p_token = nil
49
- c_token, c_token_value = @scanner.get_token
50
-
51
- until c_token.nil?
52
- @scanner.token_index += 1
53
- n_token, n_token_value = @scanner.get_token
54
-
55
- unless n_token == " "
56
- case c_token
57
- when "bad_token"
58
- @token_errors << c_token_value
59
-
60
- when "and"
61
- unless (n_token =~ /not|fstatement|statement|\(/) || (scanner.token_index == scanner.arguments.size) && !n_token.nil?
62
- @parse_errors << [pre_index, scanner.token_index]
63
- end
64
-
65
- case p_token
66
- when nil
67
- @parse_errors << [pre_index - c_token.size, scanner.token_index]
68
- when "and", "or"
69
- @parse_errors << [pre_index - 1 - p_token.size, pre_index - 1]
70
- end
71
-
72
- when "or"
73
- unless (n_token =~ /not|fstatement|statement|\(/) || (scanner.token_index == scanner.arguments.size) && !n_token.nil?
74
- @parse_errors << [pre_index, scanner.token_index]
75
- end
76
-
77
- case p_token
78
- when nil
79
- @parse_errors << [pre_index - c_token.size, scanner.token_index]
80
- when "and", "or"
81
- @parse_errors << [pre_index - 1 - p_token.size, pre_index - 1]
82
- end
83
-
84
- when "not"
85
- @parse_errors << [pre_index, scanner.token_index] unless n_token =~ /fstatement|statement|\(|not/ && !n_token.nil?
86
-
87
- when "statement", "fstatement"
88
- @parse_errors << [pre_index, scanner.token_index] if n_token !~ /and|or|\)/ && scanner.token_index != scanner.arguments.size
89
-
90
- when ")"
91
- @parse_errors << [pre_index, scanner.token_index] if n_token !~ /|and|or|not|\(/ && scanner.token_index != scanner.arguments.size
92
- if @paren_errors.empty?
93
- @paren_errors.push(n_token.nil? ? scanner.token_index - 1 : scanner.token_index - n_token_value.size)
94
- else
95
- @paren_errors.pop
96
- end
97
-
98
- when "("
99
- @parse_errors << [pre_index, scanner.token_index] unless n_token =~ /fstatement|statement|not|\(/
100
- @paren_errors.push(n_token.nil? ? scanner.token_index - 1 : scanner.token_index - n_token_value.size)
101
-
102
- else
103
- @parse_errors << [pre_index, scanner.token_index]
104
- end
105
-
106
- @execution_stack << {c_token => c_token_value} unless n_token == " " || c_token == "bad_token"
107
-
108
- p_token = c_token
109
- c_token = n_token
110
- c_token_value = n_token_value
111
- end
112
- pre_index = @scanner.token_index
113
- end
114
- end
115
- end
116
- end
117
- end
118
- # rubocop:enable Metrics/BlockNesting
@@ -1,236 +0,0 @@
1
- module MCollective
2
- module Matcher
3
- class Scanner
4
- attr_accessor :arguments, :token_index
5
-
6
- def initialize(arguments)
7
- @token_index = 0
8
- @arguments = arguments.split("")
9
- @seperation_counter = 0
10
- @white_spaces = 0
11
- end
12
-
13
- # Scans the input string and identifies single language tokens
14
- def get_token # rubocop:disable Naming/AccessorMethodName
15
- return nil if @token_index >= @arguments.size
16
-
17
- case @arguments[@token_index]
18
- when "("
19
- ["(", "("]
20
-
21
- when ")"
22
- [")", ")"]
23
-
24
- when "n"
25
- if (@arguments[@token_index + 1] == "o") && (@arguments[@token_index + 2] == "t") && ((@arguments[@token_index + 3] == " ") || (@arguments[@token_index + 3] == "("))
26
- @token_index += 2
27
- ["not", "not"]
28
- else
29
- gen_statement
30
- end
31
-
32
- when "!"
33
- ["not", "not"]
34
-
35
- when "a"
36
- if (@arguments[@token_index + 1] == "n") && (@arguments[@token_index + 2] == "d") && ((@arguments[@token_index + 3] == " ") || (@arguments[@token_index + 3] == "("))
37
- @token_index += 2
38
- ["and", "and"]
39
- else
40
- gen_statement
41
- end
42
-
43
- when "o"
44
- if (@arguments[@token_index + 1] == "r") && ((@arguments[@token_index + 2] == " ") || (@arguments[@token_index + 2] == "("))
45
- @token_index += 1
46
- ["or", "or"]
47
- else
48
- gen_statement
49
- end
50
-
51
- when " "
52
- [" ", " "]
53
-
54
- else
55
- gen_statement
56
- end
57
- end
58
-
59
- private
60
-
61
- # Helper generates a statement token
62
- def gen_statement # rubocop:disable Metrics/MethodLength
63
- func = false
64
- current_token_value = ""
65
- j = @token_index
66
- escaped = false
67
-
68
- begin
69
- case @arguments[j]
70
- when "/"
71
- loop do
72
- current_token_value << @arguments[j]
73
- j += 1
74
- break if (j >= @arguments.size) || (@arguments[j] =~ /\s/)
75
- end
76
- when /=|<|>/
77
- while @arguments[j] !~ /=|<|>/
78
- current_token_value << @arguments[j]
79
- j += 1
80
- end
81
-
82
- current_token_value << @arguments[j]
83
- j += 1
84
-
85
- if @arguments[j] == "/"
86
- loop do
87
- current_token_value << @arguments[j]
88
- j += 1
89
- if @arguments[j] == "/"
90
- current_token_value << "/"
91
- break
92
- end
93
- break if (j >= @arguments.size) || (@arguments[j] =~ /\//)
94
- end
95
- while (j < @arguments.size) && ((@arguments[j] != " ") && (@arguments[j] != ")"))
96
- current_token_value << @arguments[j]
97
- j += 1
98
- end
99
- end
100
- else
101
- loop do
102
- # Identify and tokenize regular expressions by ignoring everything between /'s
103
- if @arguments[j] == "/"
104
- current_token_value << "/"
105
- j += 1
106
- while j < @arguments.size && @arguments[j] != "/"
107
- if @arguments[j] == '\\' # rubocop:disable Metrics/BlockNesting
108
- # eat the escape char
109
- current_token_value << @arguments[j]
110
- j += 1
111
- escaped = true
112
- end
113
-
114
- current_token_value << @arguments[j]
115
- j += 1
116
- end
117
- current_token_value << @arguments[j] if @arguments[j]
118
- break
119
- end
120
-
121
- case @arguments[j]
122
- when "("
123
- func = true
124
-
125
- current_token_value << @arguments[j]
126
- j += 1
127
-
128
- while j < @arguments.size
129
- current_token_value << @arguments[j]
130
- if @arguments[j] == ")" # rubocop:disable Metrics/BlockNesting
131
- j += 1
132
- break
133
- end
134
- j += 1
135
- end
136
- when '"', "'"
137
- escaped = true
138
- escaped_with = @arguments[j]
139
-
140
- j += 1 # step over first " or '
141
- @white_spaces += 1
142
- # identified "..." or '...'
143
- # rubocop:disable Metrics/BlockNesting
144
- while j < @arguments.size
145
- case @arguments[j]
146
- when '\\'
147
- # eat the escape char but don't add it to the token, or we
148
- # end up with \\\"
149
- j += 1
150
- @white_spaces += 1
151
- break unless j < @arguments.size
152
- when escaped_with
153
- j += 1
154
- @white_spaces += 1
155
- break
156
- end
157
- current_token_value << @arguments[j]
158
- j += 1
159
- end
160
- # rubocop:enable Metrics/BlockNesting
161
- else
162
- current_token_value << @arguments[j]
163
- j += 1
164
- end
165
-
166
- break if @arguments[j] == " " && (is_klass?(j) && @arguments[j - 1] !~ /=|<|>/)
167
-
168
- if (@arguments[j] == " ") && (@seperation_counter < 2) && !current_token_value.match(/^.+(=|<|>).+$/) && (index = lookahead(j))
169
- j = index
170
- end
171
- break if (j >= @arguments.size) || (@arguments[j] =~ /\s|\)/)
172
- end
173
- @seperation_counter = 0
174
- end
175
- rescue Exception => e # rubocop:disable Lint/RescueException
176
- raise "An exception was raised while trying to tokenize '#{current_token_value} - #{e}'"
177
- end
178
-
179
- @token_index += current_token_value.size + @white_spaces - 1
180
- @white_spaces = 0
181
-
182
- # bar(
183
- if current_token_value.match(/.+?\($/)
184
- ["bad_token", [@token_index - current_token_value.size + 1, @token_index]]
185
- # /foo/=bar
186
- elsif current_token_value.match(/^\/.+?\/(<|>|=).+/)
187
- ["bad_token", [@token_index - current_token_value.size + 1, @token_index]]
188
- elsif current_token_value.match(/^.+?\/(<|>|=).+/)
189
- ["bad_token", [@token_index - current_token_value.size + 1, @token_index]]
190
- elsif func
191
- if current_token_value.match(/^.+?\((\s*(')[^']*(')\s*(,\s*(')[^']*('))*)?\)(\.[a-zA-Z0-9_]+)?((!=|<=|>=|=|>|<).+)?$/) ||
192
- current_token_value.match(/^.+?\((\s*(")[^"]*(")\s*(,\s*(")[^"]*("))*)?\)(\.[a-zA-Z0-9_]+)?((!=|<=|>=|=|>|<).+)?$/)
193
- ["fstatement", current_token_value]
194
- else
195
- ["bad_token", [@token_index - current_token_value.size + 1, @token_index]]
196
- end
197
- else
198
- return "statement", current_token_value if escaped
199
-
200
- slash_err = false
201
- current_token_value.split("").each do |c|
202
- slash_err = !slash_err if c == "/"
203
- end
204
- return "bad_token", [@token_index - current_token_value.size + 1, @token_index] if slash_err
205
-
206
- ["statement", current_token_value]
207
- end
208
- end
209
-
210
- # Deal with special puppet class statement
211
- def is_klass?(klass)
212
- klass += 1 while klass < @arguments.size && @arguments[klass] == " "
213
-
214
- if @arguments[klass] =~ /=|<|>/
215
- false
216
- else
217
- true
218
- end
219
- end
220
-
221
- # Eat spaces while looking for the next comparison symbol
222
- def lookahead(index)
223
- index += 1
224
- while index <= @arguments.size
225
- @white_spaces += 1
226
- unless @arguments[index] =~ /\s/
227
- @seperation_counter += 1
228
- return index
229
- end
230
- index += 1
231
- end
232
- nil
233
- end
234
- end
235
- end
236
- end