-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
374 lines (317 loc) · 13.1 KB
/
parse.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
__author__ = 'Alexandre'
import ply.yacc as yacc
from lex import tokens
import AST
def p_program_statement(p):
''' program : statement '''
p[0] = AST.ProgramNode(p[1])
def p_program_rec(p):
''' program : statement program '''
p[0] = AST.ProgramNode([p[1]]+p[2].children)
def p_statement(p):
''' statement : assignation
| transformation
| control '''
p[0] = p[1]
def p_integer_argument(p):
''' integer_argument : IDENTIFIER
| INTEGER
| STEP
'''
p[0] = AST.TokenNode(p[1])
def p_integer_argument_operator(p):
''' integer_argument : integer_argument ADD_OP integer_argument
| integer_argument MUL_OP integer_argument
'''
p[0] = AST.OpNode(p[2], [p[1], p[3]])
def p_string_argument_STRING(p):
''' string_argument : STRING '''
p[0] = AST.TokenNode(p[1], True)
def p_string_argument_ID(p):
''' string_argument : IDENTIFIER '''
p[0] = AST.TokenNode(p[1])
def p_point_argument(p):
''' point_argument : IDENTIFIER
| POINT
'''
p[0] = AST.TokenNode(p[1])
def p_color_argument(p):
''' color_argument : IDENTIFIER
| COLOR
'''
p[0] = AST.TokenNode(p[1])
def p_boolean_argument(p):
''' boolean_argument : IDENTIFIER
| BOOLEAN
'''
p[0] = AST.TokenNode(p[1])
def p_color_arguments(p):
''' color_arguments : RGB '(' integer_argument ',' integer_argument ',' integer_argument ')'
| HEX '(' string_argument ')'
| NAME '(' string_argument ')'
'''
key = p[1]
value = []
is_string = False
if len(p) == 5:
value = p[3]
is_string = True
elif len(p) == 9:
value = [p[3], p[5], p[7]]
p[0] = AST.ArgumentNode(AST.TokenNode(key), AST.TokenNode(value, is_string)) #TODO:
def p_point_arguments(p):
''' point_arguments : X '(' integer_argument ')'
| Y '(' integer_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_point_arguments_rec(p):
''' point_arguments : X '(' integer_argument ')' ':' point_arguments
| Y '(' integer_argument ')' ':' point_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_line_arguments(p):
''' line_arguments : P1 '(' point_argument ')'
| P2 '(' point_argument ')'
| FILL_COLOR '(' color_argument ')'
| WIDTH '(' integer_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_line_arguments_rec(p):
''' line_arguments : P1 '(' point_argument ')' ':' line_arguments
| P2 '(' point_argument ')' ':' line_arguments
| FILL_COLOR '(' color_argument ')' ':' line_arguments
| WIDTH '(' integer_argument ')' ':' line_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_circle_arguments(p):
''' circle_arguments : C '(' point_argument ')'
| R '(' integer_argument ')'
| BORDER_COLOR '(' color_argument ')'
| BORDER_WIDTH '(' integer_argument ')'
| FILL_COLOR '(' color_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_circle_arguments_rec(p):
''' circle_arguments : C '(' point_argument ')' ':' circle_arguments
| R '(' integer_argument ')' ':' circle_arguments
| BORDER_COLOR '(' color_argument ')' ':' circle_arguments
| BORDER_WIDTH '(' integer_argument ')' ':' circle_arguments
| FILL_COLOR '(' color_argument ')' ':' circle_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_rect_arguments(p):
''' rect_arguments : O '(' point_argument ')'
| WIDTH '(' integer_argument ')'
| HEIGHT '(' integer_argument ')'
| RX '(' integer_argument ')'
| RY '(' integer_argument ')'
| BORDER_COLOR '(' color_argument ')'
| BORDER_WIDTH '(' integer_argument ')'
| FILL_COLOR '(' color_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_rect_arguments_rec(p):
''' rect_arguments : O '(' point_argument ')' ':' rect_arguments
| WIDTH '(' integer_argument ')' ':' rect_arguments
| HEIGHT '(' integer_argument ')' ':' rect_arguments
| RX '(' integer_argument ')' ':' rect_arguments
| RY '(' integer_argument ')' ':' rect_arguments
| BORDER_COLOR '(' color_argument ')' ':' rect_arguments
| BORDER_WIDTH '(' integer_argument ')' ':' rect_arguments
| FILL_COLOR '(' color_argument ')' ':' rect_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_ellipse_arguments(p):
''' ellipse_arguments : C '(' point_argument ')'
| RY '(' integer_argument ')'
| RX '(' integer_argument ')'
| BORDER_COLOR '(' color_argument ')'
| BORDER_WIDTH '(' integer_argument ')'
| FILL_COLOR '(' color_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_ellipse_arguments_rec(p):
''' ellipse_arguments : C '(' point_argument ')' ':' ellipse_arguments
| RY '(' integer_argument ')' ':' ellipse_arguments
| RX '(' integer_argument ')' ':' ellipse_arguments
| BORDER_COLOR '(' color_argument ')' ':' ellipse_arguments
| BORDER_WIDTH '(' integer_argument ')' ':' ellipse_arguments
| FILL_COLOR '(' color_argument ')' ':' ellipse_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_customshape_arguments(p):
''' customshape_arguments : P '(' point_argument ')'
| BORDER_COLOR '(' color_argument ')'
| BORDER_WIDTH '(' integer_argument ')'
| CLOSE '(' boolean_argument ')'
| FILL_COLOR '(' color_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_customshape_arguments_rec(p):
''' customshape_arguments : P '(' point_argument ')' ':' customshape_arguments
| BORDER_COLOR '(' color_argument ')' ':' customshape_arguments
| BORDER_WIDTH '(' integer_argument ')' ':' customshape_arguments
| CLOSE '(' boolean_argument ')' ':' customshape_arguments
| FILL_COLOR '(' color_argument ')' ':' customshape_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_text_arguments(p):
''' text_arguments : CONTENT '(' string_argument ')'
| P '(' point_argument ')'
| FONT '(' string_argument ')'
| SIZE '(' integer_argument ')'
| FILL_COLOR '(' color_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_text_arguments_rec(p):
''' text_arguments : CONTENT '(' string_argument ')' ':' text_arguments
| P '(' point_argument ')' ':' text_arguments
| FONT '(' string_argument ')' ':' text_arguments
| SIZE '(' integer_argument ')' ':' text_arguments
| FILL_COLOR '(' color_argument ')' ':' text_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_color(p):
''' color : COLOR ':' color_arguments '''
p[0] = AST.ColorNode([AST.TokenNode(p[1]), p[3]])
def p_point(p):
''' point : POINT ':' point_arguments '''
p[0] = AST.PointNode(p[3])
def p_line(p):
''' line : LINE ':' line_arguments '''
p[0] = AST.ShapeNode([AST.TokenNode(p[1]), p[3]])
def p_circle(p):
''' circle : CIRCLE ':' circle_arguments '''
p[0] = AST.ShapeNode([AST.TokenNode(p[1]), p[3]])
def p_rect(p):
''' rect : RECT ':' rect_arguments '''
p[0] = AST.ShapeNode([AST.TokenNode(p[1]), p[3]])
def p_ellipse(p):
''' ellipse : ELLIPSE ':' ellipse_arguments '''
p[0] = AST.ShapeNode([AST.TokenNode(p[1]), p[3]])
def p_customshape(p):
''' customshape : CUSTOMSHAPE ':' customshape_arguments '''
p[0] = AST.ShapeNode([AST.TokenNode(p[1]), p[3]])
def p_text(p):
''' text : TEXT ':' text_arguments '''
p[0] = AST.ShapeNode([AST.TokenNode(p[1]), p[3]])
def p_shape(p):
''' shape : line
| circle
| rect
| ellipse
| customshape
| text
'''
p[0] = p[1]
def p_rotate_arguments(p):
''' rotate_arguments : ANGLE '(' integer_argument ')'
| C '(' point_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_rotate_arguments_rec(p):
''' rotate_arguments : ANGLE '(' integer_argument ')' ':' rotate_arguments
| C '(' point_argument ')' ':' rotate_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_scale_arguments(p):
''' scale_arguments : SX '(' integer_argument ')'
| SY '(' integer_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_scale_arguments_rec(p):
''' scale_arguments : SX '(' integer_argument ')' ':' scale_arguments
| SY '(' integer_argument ')' ':' scale_arguments
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])] + p[6].children)
def p_translate_arguments(p):
''' translate_argument : P '(' point_argument ')'
'''
p[0] = AST.ArgumentsNode(AST.ArgumentNode(p[1], p[3]))
def p_hide_arguments(p):
''' hide_argument : H '(' boolean_argument ')'
'''
p[0] = AST.ArgumentsNode([AST.ArgumentNode(p[1], p[3])])
def p_transformation(p):
''' transformation : ROTATE ':' rotate_arguments
| SCALE ':' scale_arguments
| TRANSLATE ':' translate_argument
| HIDE ':' hide_argument
'''
p[0] = AST.TransformationNode([AST.TokenNode(p[1]), p[3]])
def p_control_if(p):
''' control : IF condition '{' program '}' '''
p[0] = AST.IfNode([p[2], p[4]])
def p_control_while(p):
''' control : WHILE condition '{' program '}' '''
p[0] = AST.WhileNode([p[2], p[4]])
def p_control_for(p):
''' control : FOR integer_argument ':' integer_argument '{' program '}' '''
p[0] = AST.ForNode([p[2], p[4], p[6]])
def p_apply_body(p):
''' apply_body : IDENTIFIER ';'
'''
p[0] = AST.ApplyBodyNode([AST.TokenNode(p[1])])
def p_apply_body_rec(p):
''' apply_body : IDENTIFIER ';' apply_body
'''
p[0] = AST.ApplyBodyNode([AST.TokenNode(p[1])] + p[3].children)
def p_control_apply(p):
''' control : APPLY IDENTIFIER '{' apply_body '}' '''
p[0] = AST.ApplyNode([AST.TokenNode(p[2]), p[4]])
def p_condition(p):
''' condition : integer_argument COND_OP integer_argument
| string_argument COND_OP string_argument
| boolean_argument
'''
if len(p) == 4:
p[0] = AST.ConditionalNode(p[2], [p[1], p[3]])
else:
p[0] = AST.ConditionalNode(p[1], [])
def p_assign(p):
''' assignation : IDENTIFIER '=' shape ';'
| IDENTIFIER '=' point ';'
| IDENTIFIER '=' color ';'
| IDENTIFIER '=' transformation ';'
| IDENTIFIER '=' integer_argument ';'
| IDENTIFIER '=' string_argument ';'
| IDENTIFIER '=' point_argument ';'
| IDENTIFIER '=' color_argument ';'
| IDENTIFIER '=' boolean_argument ';'
'''
p[0] = AST.AssignNode([AST.TokenNode(p[1]),p[3]])
def p_error(p):
if p:
if p.type in tokens:
print ("Syntax error in line %d, %s token is reserved word" % (p.lineno, p.type))
else:
print ("Syntax error in line %d" % p.lineno)
print (p)
yacc.errok()
else:
print ("Sytax error: unexpected end of file!")
def parse(program):
return yacc.parse(program)#, debug=1
yacc.yacc(outputdir='generated')
if __name__ == "__main__":
import sys
path = 'Tests/'
ext = '.pnp'
files = ['clock', 'comboTest1', 'comboTest2', 'comboTest3', 'customShapeTest', 'helloTest', 'ifTest', 'loopTest', 'loopTest2', 'rotationTest', 'rotationTest2', 'simpleShapesTest', 'someTransforms', 'gradient', 'modulo']
for file in files:
print('--------------------------------------')
print('--------- start ' + file + ' -------------')
print('--------------------------------------')
fullpath = path + file + ext
prog = open(fullpath).read()
result = yacc.parse(prog)
if result:
print(result)
import os
graph = result.makegraphicaltree()
name = os.path.splitext(fullpath)[0]+'-ast.pdf'
graph.write_pdf(name)
print ("wrote ast to", name)
else:
print ("Parsing returned no result!")
exit(-1)