forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
344 lines (281 loc) · 10.7 KB
/
Copy pathparser.py
File metadata and controls
344 lines (281 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright 2008-2015 Nokia Networks
# Copyright 2016- Robot Framework Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .vendor import yacc
from .lexer import Token
from .lexerwrapper import LexerWrapper
from .nodes import (
DataFile, SettingSection, VariableSection, TestCaseSection,
KeywordSection, Variable, DocumentationSetting, SuiteSetupSetting,
SuiteTeardownSetting, MetadataSetting, TestSetupSetting,
TestTeardownSetting, TestTemplateSetting, TestTimeoutSetting,
ForceTagsSetting, DefaultTagsSetting, LibrarySetting,
ResourceSetting, VariablesSetting, SetupSetting, TeardownSetting,
TimeoutSetting, TagsSetting, TemplateSetting, ArgumentsSetting,
ReturnSetting, TemplateArguments, TestCase, Keyword, KeywordCall, ForLoop
)
class RobotFrameworkParser(object):
tokens = Token.DATA_TOKENS
def __init__(self, lexer):
self.lexer = lexer
def parse(self, source):
parser = yacc.yacc(module=self)
return parser.parse(lexer=LexerWrapper(self.lexer, source))
def p_datafile(self, p):
'''datafile :
| sections'''
p[0] = DataFile(p[1] if len(p) == 2 else [])
def p_sections(self, p):
'''sections : section
| sections section'''
append_to_list_value(p)
def p_section(self, p):
'''section : setting_section
| variable_section
| testcase_section
| keyword_section'''
p[0] = p[1]
def p_setting_section(self, p):
'''setting_section : setting_header EOS
| setting_header EOS settings'''
p[0] = SettingSection(p[3] if len(p) == 4 else [])
def p_setting_header(self, p):
'''setting_header : SETTING_HEADER
| setting_header SETTING_HEADER'''
def p_settings(self, p):
'''settings : setting
| settings setting'''
append_to_list_value(p)
def p_setting(self, p):
'''setting : documentation_setting
| suite_setup_setting EOS
| suite_teardown_setting EOS
| metadata_setting EOS
| test_setup_setting EOS
| test_teardown_setting EOS
| test_template_setting EOS
| test_timeout_setting EOS
| force_tags_setting EOS
| default_tags_setting EOS
| library_setting EOS
| resource_setting EOS
| variables_setting EOS
| setup_setting EOS
| teardown_setting EOS
| template_setting EOS
| timeout_setting EOS
| tags_setting EOS
| arguments_setting EOS
| return_setting EOS'''
p[0] = p[1]
def p_documentation(self, p):
'''documentation_setting : DOCUMENTATION arguments EOS'''
p[0] = DocumentationSetting(p[2])
def p_suite_setup(self, p):
'''suite_setup_setting : SUITE_SETUP arguments'''
p[0] = SuiteSetupSetting(p[2])
def p_suite_teardown(self, p):
'''suite_teardown_setting : SUITE_TEARDOWN arguments'''
p[0] = SuiteTeardownSetting(p[2])
def p_metadata(self, p):
'''metadata_setting : METADATA arguments'''
p[0] = MetadataSetting(p[2][0], p[2][1:])
def p_test_setup(self, p):
'''test_setup_setting : TEST_SETUP arguments'''
p[0] = TestSetupSetting(p[2])
def p_test_teardown(self, p):
'''test_teardown_setting : TEST_TEARDOWN arguments'''
p[0] = TestTeardownSetting(p[2])
def p_test_template(self, p):
'''test_template_setting : TEST_TEMPLATE arguments'''
p[0] = TestTemplateSetting(p[2])
def p_test_timeout(self, p):
'''test_timeout_setting : TEST_TIMEOUT arguments'''
p[0] = TestTimeoutSetting(p[2])
def p_force_tags(self, p):
'''force_tags_setting : FORCE_TAGS arguments'''
p[0] = ForceTagsSetting(p[2])
def p_default_tags(self, p):
'''default_tags_setting : DEFAULT_TAGS arguments'''
p[0] = DefaultTagsSetting(p[2])
def p_library(self, p):
'''library_setting : LIBRARY arguments'''
name, args = parse_name_and_args(p)
p[0] = LibrarySetting(name, args)
def p_resource(self, p):
'''resource_setting : RESOURCE arguments'''
name, args = parse_name_and_args(p)
p[0] = ResourceSetting(name, args)
def p_variables_import(self, p):
'''variables_setting : VARIABLES arguments'''
name, args = parse_name_and_args(p)
p[0] = VariablesSetting(name, args)
def p_setup(self, p):
'''setup_setting : SETUP arguments'''
p[0] = SetupSetting(p[2])
def p_teardown(self, p):
'''teardown_setting : TEARDOWN arguments'''
p[0] = TeardownSetting(p[2])
def p_template(self, p):
'''template_setting : TEMPLATE arguments'''
p[0] = TemplateSetting(p[2])
def p_timeout(self, p):
'''timeout_setting : TIMEOUT arguments'''
p[0] = TimeoutSetting(p[2])
def p_tags(self, p):
'''tags_setting : TAGS arguments'''
p[0] = TagsSetting(p[2])
def p_arguments_setting(self, p):
'''arguments_setting : ARGUMENTS arguments'''
p[0] = ArgumentsSetting(p[2])
def p_return(self, p):
'''return_setting : RETURN arguments'''
p[0] = ReturnSetting(p[2])
def p_variable_section(self, p):
'''variable_section : variable_header EOS
| variable_header EOS variables'''
p[0] = VariableSection(p[3] if len(p) == 4 else [])
def p_variable_header(self, p):
'''variable_header : VARIABLE_HEADER
| variable_header VARIABLE_HEADER'''
def p_variables(self, p):
'''variables : variable
| variables variable'''
append_to_list_value(p)
def p_variable(self, p):
'''variable : VARIABLE arguments EOS'''
p[0] = Variable(p[1], p[2])
def p_testcase_section(self, p):
'''testcase_section : testcase_header EOS
| testcase_header EOS tests'''
p[0] = TestCaseSection(p[3] if len(p) == 4 else [], p[1])
def p_testcase_header(self, p):
'''testcase_header : TESTCASE_HEADER
| testcase_header TESTCASE_HEADER'''
append_to_list_value(p)
def p_keyword_section(self, p):
'''keyword_section : keyword_header EOS
| keyword_header EOS keywords'''
p[0] = KeywordSection(p[3] if len(p) == 4 else [])
def p_keyword_header(self, p):
'''keyword_header : KEYWORD_HEADER
| keyword_header KEYWORD_HEADER'''
def p_tests(self, p):
'''tests : test
| tests test'''
append_to_list_value(p)
def p_keywords(self, p):
'''keywords : keyword
| keywords keyword'''
append_to_list_value(p)
def p_test(self, p):
'''test : NAME EOS
| NAME EOS body_items'''
if len(p) == 3:
p[0] = TestCase(p[1], [])
else:
p[0] = TestCase(p[1], p[3])
def p_keyword(self, p):
'''keyword : NAME EOS
| NAME EOS body_items'''
if len(p) == 3:
p[0] = Keyword(p[1], [])
else:
p[0] = Keyword(p[1], p[3])
def p_body_items(self, p):
'''body_items : body_item
| body_items body_item'''
append_to_list_value(p)
def p_body_item(self, p):
'''body_item : forloop
| setting
| step
| templatearguments
| invalid_forloop'''
p[0] = p[1]
def p_step(self, p):
'''step : KEYWORD arguments EOS'''
p[0] = KeywordCall(None, p[1], p[2])
def p_step_with_assignment(self, p):
'''step : assignments EOS
| assignments KEYWORD arguments EOS'''
if len(p) == 3:
p[0] = KeywordCall(p[1], None)
else:
p[0] = KeywordCall(p[1], p[2], p[3])
def p_forloop(self, p):
'''forloop : for_header for_body END EOS
| for_header END EOS
| END EOS'''
if len(p) == 3: # FIXME: Better way to handle dangling END
p[0] = KeywordCall(None, p[1])
else:
p[1].body = p[2] if len(p) == 5 else []
p[0] = p[1]
def p_for_header(self, p):
'''for_header : FOR arguments FOR_SEPARATOR arguments EOS
| FOR arguments EOS'''
if len(p) == 6:
p[0] = ForLoop(p[2], p[3], p[4])
else:
p[0] = ForLoop(p[2], 'IN', [])
def p_for_body(self, p):
'''for_body : step
| for_body step
| templatearguments
| for_body templatearguments'''
append_to_list_value(p)
def p_templatearguments(self, p):
'''templatearguments : arguments EOS'''
p[0] = TemplateArguments(p[1])
def p_invalid_for_loop(self, p):
'''invalid_forloop : for_header for_body'''
p[0] = p[1]
def p_assignments(self, p):
'''assignments : ASSIGN
| assignments ASSIGN'''
append_to_list_value(p)
def p_arguments(self, p):
'''arguments : args
| empty_arg'''
p[0] = p[1]
def p_args(self, p):
'''args : ARGUMENT
| arguments ARGUMENT'''
append_to_list_value(p)
def p_empty_arg(self, p):
'''empty_arg : '''
p[0] = []
def p_error(self, e):
if e:
print(e.type)
print("Parse error:" + str(e))
# FIXME not a proper way to handle errors
#self.parser.errok()
def append_to_list_value(p):
if len(p) == 1:
p[0] = []
elif len(p) == 2:
p[0] = [p[1]]
else:
value = p[1]
# TODO: IS this check needed??
if p[2] is not None:
value.append(p[2])
p[0] = value
def parse_name_and_args(p):
if p[2]:
return p[2][0], p[2][1:]
else:
return '', []