Debian Codegraph @

src/drel/drel_ast_yacc.py

raw symbols SCIP
1 # A dREL grammar written for python-ply
2 #
3 # The output is an Abstract Syntax Tree that represents a
4 # function fragment that needs to be wrapped with information
5 # appropriate to the target language.
6
7 # The grammar is based on the Python 2.7 grammar, in
8 # consultation with Doug du Boulay's JsCifBrowser
9 # grammar (also derived from a Python grammar).
10
11 # To maximize python3/python2 compatibility
12 from __future__ import print_function
13 from __future__ import unicode_literals
14 from __future__ import absolute_import
15
16 from .drel_lex import lexer,tokens
17 import ply.yacc as yacc
18
19 # Overall translation unit
20
21 # Our input is a sequence of statements
22 def p_input(p):
23 '''input : maybe_nline statement
24 | input statement '''
25 if p[1] is None:
26 p[0] = p[2]
27 else:
28 so_far = p[1][1]
29 new_statements = p[2][1]
30 p[0] = ["STATEMENTS",p[1][1] + p[2][1]]
31 #print('input now {!r}'.format(p[0]))
32
33 # We distinguish between compound statements and
34 # small statements. Small statements may be
35 # chained together on a single line with semicolon
36 # separators. Compound statements are not separated
37 # in this way, and will always be terminated by
38 # a newline.
39 def p_statement(p):
40 '''statement : simple_stmt newlines
41 | simple_stmt ";" newlines
42 | compound_stmt '''
43 p[0] = p[1]
44
45 # A simple statement is a sequence of small statements terminated by
46 # a NEWLINE or EOF
47 def p_simple_stmt(p):
48 ''' simple_stmt : small_stmt
49 | simple_stmt ";" small_stmt '''
50 if len(p) == 2:
51 p[0] = ["STATEMENTS",[p[1]]]
52 else:
53 p[0] = ["STATEMENTS",p[1][1] + [p[3]]]
54
55 # This production appears inside a set of braces. Any statement
56 # will be automatically terminated by a newline so we do not
57 # need to include that here
58 def p_statements(p):
59 '''statements : statement
60 | statements statement '''
61 if len(p) == 2: p[0] = p[1]
62 else: p[0] = ["STATEMENTS", p[1][1] + [p[2]]]
63
64 def p_small_stmt(p):
65 '''small_stmt : expr_stmt
66 | break_stmt
67 | next_stmt'''
68 p[0] = p[1]
69
70 def p_break_stmt(p):
71 '''break_stmt : BREAK'''
72 p[0] = ["BREAK"]
73
74 def p_next_stmt(p):
75 '''next_stmt : NEXT'''
76 p[0] = ["NEXT"]
77
78 # Note here that a simple testlist_star_expr is useless as in our
79 # side-effect-free world it will be evaluated and discarded. We
80 # could just drop it right now but we let it go through to the
81 # AST processor for language-dependent processing
82
83 # We test for an explicit nline as it is not ignored otherwise
84 #
85 def p_expr_stmt(p):
86 ''' expr_stmt : testlist_star_expr
87 | testlist_star_expr AUGOP testlist_star_expr
88 | testlist_star_expr "=" testlist_star_expr
89 | testlist_star_expr "=" maybe_nline testlist_star_expr
90 | testlist_star_expr AUGOP maybe_nline testlist_star_expr
91 | fancy_drel_assignment_stmt '''
92 if len(p) == 2 and p[1][0] != 'FANCY_ASSIGN': # we have a list of expressions which we
93 p[0] = ["EXPRLIST",p[1]]
94 elif len(p) == 2 and p[1][0] == 'FANCY_ASSIGN':
95 p[0] = p[1]
96 elif len(p) == 5:
97 p[0] = ["ASSIGN",p[1], p[2], p[4]]
98 else:
99 p[0] = ["ASSIGN",p[1],p[2],p[3]]
100
101 def p_testlist_star_expr(p): # list of expressions in fact
102 ''' testlist_star_expr : expression
103 | testlist_star_expr "," maybe_nline expression '''
104 if len(p) == 2:
105 p[0] = [p[1]]
106 else:
107 p[0] = p[1] + [p[4]]
108
109 # Simplified from the python 2.5 version due to apparent conflict with
110 # the other type of IF expression...
111 #
112 def p_expression(p):
113 '''expression : or_test '''
114 p[0] = ["EXPR",p[1]]
115
116 # This is too generous, as it allows a function call on the
117 # LHS to be assigned to. This will cause a syntax error on
118 # execution.
119
120 def p_or_test(p):
121 ''' or_test : and_test
122 | or_test OR and_test
123 | or_test BADOR and_test'''
124 if len(p) == 2: p[0] = p[1]
125 else: p[0] = ["MATHOP","or",p[1],p[3]]
126
127 def p_and_test(p):
128 '''and_test : not_test
129 | and_test AND not_test
130 | and_test BADAND not_test'''
131 if len(p) == 2: p[0] = p[1]
132 else: p[0] = ["MATHOP","and", p[1],p[3]]
133
134 def p_not_test(p):
135 '''not_test : comparison
136 | NOT not_test'''
137 if len(p) == 2: p[0] = p[1]
138 else: p[0] = ["UNARY","not",p[2]]
139
140 def p_comparison(p):
141 '''comparison : a_expr
142 | a_expr comp_operator a_expr'''
143 if len(p) == 2: p[0] = p[1]
144 else:
145 p[0] = ["MATHOP",p[2],p[1],p[3]]
146
147 def p_comp_operator(p):
148 '''comp_operator : restricted_comp_operator
149 | IN
150 | NOT IN '''
151 if len(p)==3:
152 p[0] = "not in"
153 else: p[0] = p[1]
154
155 def p_restricted_comp_operator(p): #for loop tests
156 '''restricted_comp_operator : "<"
157 | ">"
158 | GTE
159 | LTE
160 | NEQ
161 | ISEQUAL '''
162 p[0] = p[1]
163
164 def p_a_expr(p):
165 '''a_expr : m_expr
166 | a_expr "+" m_expr
167 | a_expr "-" m_expr'''
168 if len(p) == 2:
169 p[0] = p[1]
170 else:
171 p[0] = ["MATHOP",p[2], p[1], p[3]]
172
173 def p_m_expr(p): #note bitwise and and or are pycifrw extensions
174 '''m_expr : u_expr
175 | m_expr "*" u_expr
176 | m_expr "/" u_expr
177 | m_expr "^" u_expr
178 | m_expr "&" u_expr
179 | m_expr "|" u_expr '''
180 if len(p) == 2:
181 p[0] = p[1]
182 else:
183 p[0] = ["MATHOP",p[2],p[1],p[3]]
184
185 def p_u_expr(p):
186 '''u_expr : power
187 | "-" u_expr
188 | "+" u_expr'''
189 if len(p) == 2:
190 p[0] = p[1]
191 else:
192 p[0] = ["SIGN",p[1],p[2]]
193
194 def p_power(p):
195 '''power : primary
196 | primary POWER u_expr'''
197 if len(p) == 2:
198 p[0] = p[1]
199 else:
200 p[0] = ["MATHOP","**",p[1] , p[3]]
201 # print('At power: p[0] is {!r}'.format(p[0]))
202
203 def p_primary(p):
204 '''primary : atom
205 | attributeref
206 | subscription
207 | slicing
208 | call'''
209 # print 'Primary -> %s' % repr(p[1])
210 p[0] = p[1]
211
212 def p_atom(p):
213 '''atom : ID
214 | item_tag
215 | literal
216 | enclosure'''
217 # print 'Atom -> %s' % repr(p[1])
218 p[0] = ["ATOM",p[1]]
219
220 def p_item_tag(p):
221 '''item_tag : ITEM_TAG'''
222 p[0] = ["ITEM_TAG",p[1]]
223
224 def p_literal(p):
225 '''literal : stringliteral
226 | INTEGER
227 | HEXINT
228 | OCTINT
229 | BININT
230 | REAL
231 | IMAGINARY'''
232 # print 'literal-> %s' % repr(p[1])
233 p[0] = ["LITERAL",p[1]]
234
235 def p_stringliteral(p):
236 '''stringliteral : STRPREFIX SHORTSTRING
237 | STRPREFIX LONGSTRING
238 | SHORTSTRING
239 | LONGSTRING'''
240 if len(p)==3: p[0] = p[1]+p[2]
241 else: p[0] = p[1]
242
243 def p_enclosure(p):
244 '''enclosure : parenth_form
245 | string_conversion
246 | list_display
247 | table_display '''
248 p[0]=p[1]
249
250 def p_parenth_form(p):
251 '''parenth_form : OPEN_PAREN testlist_star_expr CLOSE_PAREN
252 | OPEN_PAREN CLOSE_PAREN '''
253 if len(p) == 3: p[0] = ["GROUP"]
254 else:
255 p[0] = ["GROUP",p[2]]
256 # print('Parens: {!r}'.format(p[0]))
257
258 def p_string_conversion(p):
259 '''string_conversion : "`" testlist_star_expr "`" '''
260 # WARNING: NOT IN PUBLISHED dREL papaer
261 p[0] = ["FUNC_CALL","str",p[2]]
262
263 def p_list_display(p):
264 ''' list_display : "[" maybe_nline listmaker maybe_nline "]"
265 | "[" maybe_nline "]" '''
266 if len(p) == 4: p[0] = ["LIST"]
267 else:
268 p[0] = ["LIST"] + p[3]
269
270
271 # scrap the trailing comma
272 def p_listmaker(p):
273 '''listmaker : expression listmaker2 '''
274 p[0] = [p[1]] + p[2]
275 # print('listmaker: {!r}'.format(p[0]))
276
277 def p_listmaker2(p):
278 '''listmaker2 : "," maybe_nline expression
279 | listmaker2 "," maybe_nline expression
280 | '''
281 if len(p) == 4:
282 p[0] = [p[3]]
283 elif len(p) < 2:
284 p[0] = []
285 else:
286 p[0] = p[1] + [p[4]]
287
288 # define tables
289 def p_table_display(p):
290 ''' table_display : "{" maybe_nline tablemaker maybe_nline "}"
291 | "{" maybe_nline "}" '''
292 if len(p) == 4:
293 p[0] = ["TABLE"]
294 else:
295 p[0] = ["TABLE"] + p[3]
296
297 def p_tablemaker(p):
298 '''tablemaker : stringliteral ":" expression tablemaker2 '''
299 p[0] = [(p[1],p[3])] + p[4]
300
301 def p_tablemaker2(p):
302 '''tablemaker2 : "," maybe_nline stringliteral ":" expression
303 | tablemaker2 "," maybe_nline stringliteral ":" expression
304 | '''
305 if len(p) == 6:
306 p[0] = [(p[3],p[5])]
307 elif len(p) < 2:
308 p[0] = []
309 else:
310 p[0] = p[1] + [(p[4],p[6])]
311
312 # Note that we need to catch tags of the form 't.12', which
313 # our lexer will interpret as ID REAL. We therefore also
314 # accept t.12(3), which is not allowed, but we don't bother
315 # trying to catch this error here.
316
317 def p_attributeref(p):
318 '''attributeref : primary attribute_tag '''
319 p[0] = ["ATTRIBUTE",p[1],p[2]]
320
321 def p_attribute_tag(p):
322 '''attribute_tag : "." ID
323 | REAL '''
324 if len(p) == 3:
325 p[0] = p[2]
326 else:
327 p[0] = p[1][1:]
328
329 def p_subscription(p):
330 '''subscription : primary "[" expression "]" '''
331 p[0] = ["SUBSCRIPTION",p[1],p[3]]
332
333 def p_slicing(p):
334 '''slicing : primary "[" slice_list "]" '''
335 p[0] = ["SLICE", p[1], p[3] ]
336
337 def p_proper_slice(p):
338 '''proper_slice : short_slice
339 | long_slice '''
340 p[0] = p[1]
341
342 # Our AST slice convention is that, if anything is mentioned,
343 # the first element is always
344 # explicitly mentioned. A single element will be a starting
345 # element. Two elements are start and finish. An empty list
346 # is all elements. Three elements are start, finish, step.
347 # We combine these into a list of slices.
348
349 def p_short_slice(p):
350 '''short_slice : ":"
351 | expression ":" expression
352 | ":" expression
353 | expression ":" '''
354 if len(p) == 2: p[0] = []
355 if len(p) == 4: p[0] = [p[1],p[3]]
356 if len(p) == 3 and p[1] == ":":
357 p[0] = [[],p[2]]
358 if len(p) == 3 and p[2] == ":":
359 p[0] = [p[1]]
360
361 def p_long_slice(p):
362 '''long_slice : short_slice ":" expression '''
363 if len(p) == 4:
364 p[0] = p[1] + [p[3]]
365 else:
366 p[0] = p[1]
367
368 def p_slice_list(p):
369 ''' slice_list : slice_item
370 | slice_list "," slice_item '''
371 if len(p) == 2:
372 p[0] = [p[1]]
373 else:
374 p[0] = p[1] + [p[3]]
375
376 def p_slice_item(p):
377 ''' slice_item : expression
378 | proper_slice '''
379 p[0] = p[1]
380
381 def p_call(p):
382 '''call : ID OPEN_PAREN CLOSE_PAREN
383 | ID OPEN_PAREN argument_list CLOSE_PAREN '''
384 if len(p) == 4:
385 p[0] = ["FUNC_CALL",p[1],[]]
386 else:
387 p[0] = ["FUNC_CALL",p[1],p[3]]
388 #print("Function call: {!r}".format(p[0]))
389
390 # These are the arguments to a call, not a definition
391
392 def p_argument_list(p):
393 '''argument_list : func_arg
394 | argument_list "," func_arg '''
395 if len(p) == 2:
396 p[0] = [p[1]]
397 else:
398 p[0] = p[1] + [p[3]]
399
400 def p_func_arg(p):
401 '''func_arg : expression '''
402 p[0] = p[1]
403
404 def p_fancy_drel_assignment_stmt(p):
405 '''fancy_drel_assignment_stmt : ID OPEN_PAREN dotlist CLOSE_PAREN '''
406 p[0] = ["FANCY_ASSIGN",p[1],p[3]]
407 # print("Fancy assignment -> {!r}".format(p[0]))
408
409 # Something made up specially for drel. A newline is OK between assignments
410
411 def p_dotlist(p):
412 '''dotlist : "." ID "=" expression
413 | dotlist "," "." ID "=" expression '''
414 if len(p) <= 5: #first element of dotlist
415 p[0] = [[p[2],p[4]]]
416 else: #append to previous elements
417 p[0] = p[1] + [[p[4],p[6]]]
418
419 def p_exprlist(p):
420 ''' exprlist : a_expr
421 | exprlist "," a_expr '''
422 if len(p) == 2:
423 p[0] = [p[1]]
424 else: p[0] = p[1] + [p[3]]
425
426 # a (potentially enclosed) list of ids
427 def p_id_list(p):
428 ''' id_list : ID
429 | id_list "," ID '''
430
431 if len(p) == 2:
432 p[0] = [p[1]]
433 else:
434 p[0] = p[1] + [p[3]]
435
436 # now for the compound statements. We prepare them as a "STATEMENT"
437 # list for smooth processing in the simple_stmt production
438
439 def p_compound_stmt(p):
440 '''compound_stmt : if_stmt
441 | if_else_stmt
442 | for_stmt
443 | do_stmt
444 | loop_stmt
445 | with_stmt
446 | repeat_stmt
447 | funcdef '''
448 p[0] = ["STATEMENTS",[p[1]]]
449 #print "Compound statement: \n" + p[0]
450
451 # There must only be one else statement at the end of the else if statements,
452 # so we show this by creating a separate production
453 def p_if_else_stmt(p):
454 '''if_else_stmt : if_stmt ELSE maybe_nline suite'''
455 p[0] = p[1]
456 p[0].append(p[4])
457
458 # The AST node is [IF_EXPR,cond, suite,[[elseif cond1,suite],[elseifcond2,suite]...]]
459 def p_if_stmt(p):
460 '''if_stmt : IF OPEN_PAREN expression CLOSE_PAREN maybe_nline suite
461 | if_stmt ELSEIF OPEN_PAREN expression CLOSE_PAREN maybe_nline suite '''
462 if len(p) == 7:
463 p[0] = ["IF_EXPR"]
464 p[0].append(p[3])
465 p[0].append(p[6])
466 p[0].append([])
467 else:
468 p[0] = p[1]
469 p[0][3].append([p[4],p[7]])
470
471 # Note the dREL divergence from Python here: we allow compound
472 # statements to follow without a separate block (like C etc.) Where
473 # we have a single statement immediately following we have to make the
474 # statement block. A small_stmt will be a single production, so must
475 # be put into a list in order to match the 'statements' structure
476 # (i.e. 2nd element is a list of statements). A compound_stmt is thus
477 # forced to be also a non-listed object.
478
479 def p_suite(p):
480 '''suite : suite_long
481 | suite_short'''
482 p[0] = p[1]
483
484 def p_suite_short(p):
485 '''suite_short : "{" simple_stmt "}"
486 | "{" simple_stmt "}" maybe_nline '''
487 p[0] = p[2]
488
489 def p_suite_long(p):
490 '''suite_long : statement
491 | "{" maybe_nline statements "}" maybe_nline
492 | "{" statements "}" maybe_nline'''
493 if len(p) == 2:
494 p[0] = p[1]
495 elif len(p) == 6:
496 p[0] = p[3]
497 else:
498 p[0] = p[2]
499
500 def p_for_stmt(p):
501 '''for_stmt : for_head suite '''
502 p[0] = p[1] + [p[2]]
503
504 def p_for_head(p):
505 '''for_head : FOR id_list IN testlist_star_expr
506 | FOR "[" id_list "]" IN testlist_star_expr '''
507 if len(p) == 5:
508 p[0] = ["FOR", p[2], p[4]]
509 else:
510 p[0] = ["FOR", p[3], p[6]]
511
512 def p_loop_stmt(p):
513 '''loop_stmt : loop_head suite
514 | loop_head maybe_nline suite'''
515 if len(p) == 3:
516 p[0] = ["LOOP"] + p[1] + [p[2]]
517 else:
518 p[0] = ["LOOP"] + p[1] + [p[3]]
519
520 # We capture a list of all the actually present items in the current
521 # datafile
522 def p_loop_head(p):
523 '''loop_head : LOOP ID AS ID
524 | LOOP ID AS ID ":" ID
525 | LOOP ID AS ID ":" ID restricted_comp_operator ID '''
526
527 p[0] = [p[2],p[4]]
528 if len(p)>= 7:
529 p[0] = p[0] + [p[6]]
530 else: p[0] = p[0] + [""]
531 if len(p) >= 9:
532 p[0] = p[0] + [p[7],p[8]]
533 else: p[0] = p[0] + ["",""]
534
535 def p_do_stmt(p):
536 '''do_stmt : do_stmt_head suite
537 | do_stmt_head maybe_nline suite
538 '''
539 if len(p) == 3:
540 p[0] = p[1] + [p[2]]
541 else:
542 p[0] = p[1] + [p[3]]
543
544 # To translate the dREL do to a for statement, we need to make the
545 # end of the range included in the range
546
547 def p_do_stmt_head(p):
548 '''do_stmt_head : DO ID "=" expression "," expression
549 | DO ID "=" expression "," expression "," expression'''
550
551 p[0] = ["DO",p[2],p[4],p[6]]
552 if len(p)==9:
553 p[0] = p[0] + [p[8]]
554 else:
555 p[0] = p[0] + [["EXPR",["LITERAL","1"]]]
556
557 def p_repeat_stmt(p):
558 '''repeat_stmt : REPEAT suite'''
559 p[0] = ["REPEAT",p[2]]
560
561 def p_with_stmt(p):
562 '''with_stmt : with_head maybe_nline suite'''
563 p[0] = p[1]+[p[3]]
564
565 def p_with_head(p):
566 '''with_head : WITH ID AS ID'''
567 p[0] = ["WITH",p[2],p[4]]
568
569 def p_funcdef(p):
570 ''' funcdef : FUNCTION ID OPEN_PAREN arglist CLOSE_PAREN suite '''
571 p[0] = ["FUNCTION",p[2],p[4],p[6]]
572
573 def p_arglist(p):
574 ''' arglist : ID ":" list_display
575 | arglist "," ID ":" list_display '''
576 if len(p) == 4: p[0] = [(p[1],p[2])]
577 else: p[0] = p[1] + [(p[3],p[5])]
578
579 # This production allows us to insert optional newlines
580
581 def p_maybe_nline(p):
582 ''' maybe_nline : newlines
583 | empty'''
584 pass
585
586 # We need to allow multiple newlines here and not just in the lexer as
587 # an intervening comment can cause multiple newline tokens to appear
588
589 def p_newlines(p):
590 ''' newlines : NEWLINE
591 | newlines NEWLINE '''
592 pass
593
594 def p_empty(p):
595 ''' empty : '''
596 pass
597
598 def p_error(p):
599 try:
600 print('Syntax error at position %d, line %d token %s, value %s' % (p.lexpos,p.lineno,p.type,p.value))
601 print('Surrounding text: ' + p.lexer.lexdata[max(p.lexpos - 100,0): p.lexpos] + "*" + \
602 p.lexer.lexdata[p.lexpos:min(p.lexpos + 100,len(p.lexer.lexdata))])
603 except:
604 pass
605 parser.restart()
606 raise SyntaxError
607
608 #lexer = drel_lex.lexer
609 parser = yacc.yacc()