-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsyn.py
506 lines (412 loc) · 15.9 KB
/
tsyn.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!python3
"""Syntactic analysis for the Catch test sources.
The methods of the SyntacticAnalyzerForCatch class that start with
a capital letter do implement recursive parsing of the related nonterminal.
"""
import re
import sys
import tlex
import textwrap
class SyntacticAnalyzerForCatch:
def __init__(self, source):
self.source = source
self.lextoken = None
self.sym = None
self.value = None
self.lexem = None
self.lexextra_info = None
self.it = iter(tlex.Container(self.source))
self.syntax_tree = [] # syntax tree as the list of tuples with lists...
self.lex() # prepare the very first token
def lex(self):
"""Get the next lexical token.
"""
try:
self.lextoken = next(self.it)
self.sym, self.value, self.lexem, self.lexextra_info = self.lextoken
except StopIteration:
pass
def expect(self, *expected_symbols):
"""Checks the symbol and gets the next one or reports error.
"""
if self.sym in expected_symbols:
self.lex()
else:
line_no = self.it.lineno
source_name = self.it.source_name
msg = 'Expected symbol(s): {}\n'.format(expected_symbols)
msg += ('Unexpected content in {!r} at line {}:\n'
'{!r}, {!r}\n').format(
source_name, line_no, self.sym, self.value)
raise RuntimeError(msg)
#-------------------------------------------------------------------------
def Start(self):
"""Implements the start nonterminal.
"""
self.Feature_or_story()
self.Test_case_or_scenario_serie()
self.expect('$')
return self.syntax_tree
def Ignored_symbols(self):
"""Nonterminal for the sequence of zero or more 'newline' or 'line' tokens.
"""
if self.sym in ('comment', 'hash', 'identifier', 'newline',
'stringlit', 'lpar', 'rpar', 'semic', 'assignment',
'num', 'colon'):
self.lex()
self.Ignored_symbols()
#-------------------------------------------------------------------------
def Feature_or_story(self):
"""Nonterminal for processing the story/feature inside comment tokens.
"""
self.Ignored_symbols()
if self.sym in ('story', 'feature'):
self.syntax_tree.append( (self.sym, self.value) )
self.lex()
comment_lst = []
self.Comments(comment_lst)
if comment_lst:
while comment_lst[0] == '':
del comment_lst[0]
self.syntax_tree.append( ('description', comment_lst) )
def Comments(self, comment_lst):
"""Nonterminal for collecting the content of comments.
"""
if self.sym == 'comment':
comment_lst.append(self.value)
self.lex()
self.Comments(comment_lst)
def Test_case_or_scenario_serie(self):
"""Nonterminal for a serie of test cases or scenarios.
"""
self.Ignored_symbols()
if self.sym == 'test_case':
self.Test_case()
self.Test_case_or_scenario_serie()
elif self.sym == 'scenario':
self.Scenario()
self.Test_case_or_scenario_serie()
#-------------------------------------------------------------------------
def Test_case(self):
"""Nonterminal for one TEST_CASE.
"""
assert self.sym == 'test_case'
item = [self.sym] # first element of the item = symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element = test identification
self.lex()
else:
self.expect('stringlit')
# Optional argument with tags.
if self.sym == 'comma':
self.lex()
if self.sym == 'stringlit':
raise NotImplementedError('syntax tree for tags not implemented')
self.lex()
else:
expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
# Collect the subree of the test_case body.
bodylst = []
item.append(bodylst) # third element of the item = subtree
# Append the test case item to the syntax tree. The bodylst will be
# filled later.
self.syntax_tree.append(tuple(item))
# Skip the other lines -- 'section' expected.
self.Ignored_symbols()
if self.sym == 'section':
self.Section_serie(bodylst)
elif self.sym == 'lbrace':
self.Block_of_code()
self.Ignored_symbols()
self.expect('rbrace')
def Section_serie(self, upperlst):
"""Nonterminal for any other code between the {}.
"""
self.Ignored_symbols()
if self.sym == 'section':
self.Section(upperlst)
self.Ignored_symbols()
self.Section_serie(upperlst)
def Section(self, upperlst):
"""Nonterminal for SECTION
"""
assert self.sym == 'section'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the value
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Simplified implementation. The sections are not expected to be nested.
# Just skip the other lines.
self.Ignored_symbols()
self.expect('rbrace')
# Output the previously collected symbol, identifier, and body
# of the section into the syntaxt tree.
upperlst.append(tuple(item))
#-------------------------------------------------------------------------
def Scenario(self):
"""Nonterminal for one SCENARIO.
"""
assert self.sym == 'scenario'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the value
self.lex()
else:
self.expect('stringlit')
# Optional argument with tags.
if self.sym == 'comma':
self.lex()
if self.sym == 'stringlit':
raise NotImplementedError('syntax tree for tags not implemented')
self.lex()
else:
expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the scenario item to the syntax tree. The bodylst will be
# filled later.
self.syntax_tree.append(tuple(item))
# Skip the other lines -- 'given' expected.
self.Ignored_symbols()
if self.sym == 'given':
self.Given_serie(bodylst)
elif self.sym == 'lbrace':
print('block')
self.Block_of_code()
self.Ignored_symbols()
self.expect('rbrace')
#-------------------------------------------------------------------------
def Given_serie(self, upperlst):
"""Zero or more GIVEN items (at the same level).
"""
self.Ignored_symbols() # neccessary for the recursion
if self.sym == 'given':
self.Given(upperlst)
self.Given_serie(upperlst)
def Given(self, upperlst):
"""Nonterminal for one GIVEN definition.
"""
assert self.sym == 'given'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the identifier
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the item to the upperlst. The bodylst will be filled by
# the syntax subtree later.
upperlst.append(tuple(item))
# Skip the other lines, and process the nested items.
self.Ignored_symbols()
if self.sym == 'when':
self.When_serie(bodylst) # nested to the given
elif self.sym == 'given':
self.sym = 'and_given' # symbol transformation
self.And_given(bodylst) # nested to the given
self.Ignored_symbols()
self.expect('rbrace')
def And_given(self, upperlst):
"""Nonterminal for one AND_GIVEN definition -- always nested.
"""
assert self.sym == 'and_given'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the identifier
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the item to the upperlst. The bodylst will be filled by
# the syntax subtree later.
upperlst.append(tuple(item))
# Skip the other lines, and process the nested items.
self.Ignored_symbols()
if self.sym == 'when':
self.When_serie(bodylst) # nested to the and_given
elif self.sym == 'given': # Catch does not know AND_GIVEN
self.sym = 'and_given' # symbol transformation
self.And_given(bodylst) # nested to this and_given
self.Ignored_symbols()
self.expect('rbrace')
#-------------------------------------------------------------------------
def When_serie(self, upperlst):
"""Nonterminal for a serie of WHEN definitions.
"""
self.Ignored_symbols() # neccessary for the recursion
if self.sym == 'when':
self.When(upperlst)
self.When_serie(upperlst)
def When(self, upperlst):
"""Nonterminal for one WHEN definition.
"""
assert self.sym == 'when'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the identifier
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the item to the upperlst. The bodylst will be filled by
# the syntax subtree later.
upperlst.append(tuple(item))
# Skip the other lines, and process the nested items.
self.Ignored_symbols()
if self.sym == 'then':
self.Then(bodylst) # nested
elif self.sym == 'and_when':
self.And_when(bodylst) # nested to this when
self.Ignored_symbols()
self.expect('rbrace')
def And_when(self, upperlst):
"""Nonterminal for one AND_WHEN definition.
"""
assert self.sym == 'and_when'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the identifier
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the item to the upperlst. The bodylst will be filled by
# the syntax subtree later.
upperlst.append(tuple(item))
# Skip the other lines, and process the nested items.
self.Ignored_symbols()
if self.sym == 'then':
self.Then(bodylst) # nested
elif self.sym == 'and_when':
self.And_when(bodylst) # nested to this and_when
self.Ignored_symbols()
self.expect('rbrace')
#-------------------------------------------------------------------------
def Then(self, upperlst):
"""Nonterminal for one THEN definition.
"""
assert self.sym == 'then'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the identifier
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the item to the upperlst. The bodylst will be filled by
# the syntax subtree later.
upperlst.append(tuple(item))
# Skip the other lines, and process the nested items.
self.Ignored_symbols()
if self.sym == 'and_then':
self.And_then(bodylst) # nested to the previous then-item
self.Ignored_symbols()
self.expect('rbrace')
def And_then(self, upperlst):
"""Nonterminal for one AND_THEN definition.
"""
assert self.sym == 'and_then'
item = [self.sym] # first element with the symbol
self.lex()
self.expect('lpar')
if self.sym == 'stringlit':
item.append(self.value) # second element with the identifier
self.lex()
else:
self.expect('stringlit')
self.expect('rpar')
self.expect('lbrace')
bodylst = []
item.append(bodylst) # third element with the subtree
# Append the item to the upperlst. The bodylst will be filled by
# the syntax subtree later.
upperlst.append(tuple(item))
# Skip the other lines, and process the nested items.
self.Ignored_symbols()
if self.sym == 'and_then':
self.And_then(bodylst) # nested to the previous then-item
self.Ignored_symbols()
self.expect('rbrace')
#-------------------------------------------------------------------------
def Block_of_code(self):
"""C/C++ block of code in curly braces -- or sequence of block or nested.
"""
if self.sym == 'lbrace':
self.expect('lbrace')
self.Ignored_symbols()
self.Block_of_code() # nested
self.expect('rbrace')
self.Ignored_symbols()
self.Block_of_code() # sequence
#-----------------------------------------------------------------------
if __name__ == '__main__':
source = textwrap.dedent('''\
// Story: story identifier
//
// As a user
// I want the feature
// so that my life is to be easier.
SCENARIO( "scenario 1 identifier" ) {
}
SCENARIO( "scenario 2 identifier" ) {
}
TEST_CASE( "test case 1 identifier" ) {
}
TEST_CASE( "test case 2 identifier" ) {
}
SCENARIO( "scenario identifier" ) {
GIVEN( "given identifier" ) {
// set up initial state
WHEN( "when identifier" ) {
// perform operation
THEN( "then identifier" ) {
// assert expected state
}
}
}
}
''')
sa = SyntacticAnalyzerForCatch(source)
sa.Start() # run from the start nonterminal
print(sa.tree())