-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonCoder.py
626 lines (531 loc) · 22.8 KB
/
PythonCoder.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import re
from Dictionaries import Dictionaries
class PythonCoder():
def __init__(self):
self.dictionary = Dictionaries()
self.code = ""
self.blockCommaStack = []
self.elementCommaStack = []
self.attribCommaStack = []
self.alterCommaStack = []
self.condCommaStack = []
self.indents = 0
self.smoothContext = False
self.alternativesContext = False
self.conditionContext = False
self.conditionalContext = False
self.spec_condition = []
def getCode(self):
return self.code
def indent(self):
return (" "*4*self.indents)
def write(self,text):
self.code += text
def literalize(self, text, useUnderscore):
if text[0] == '"':
return text
if useUnderscore:
literalized = re.sub('("[ _]+")', ' ', re.sub('("")', '"', re.sub('([a-zA-Z]+)', '"\\1"', text) ) )
else:
literalized = re.sub('([a-zA-Z_]+[a-zA-Z0-9_]*)', '"\\1"', text)
return literalized
def toCamelCase(self, text):
return ''.join([ x.capitalize() for x in text.split('_') ])
def replaceHexColorCode(self, match):
value = match.group(1)
if len(value) == 3: # short group
value = [str(round(int(c + c, 16)/255.0,3)) for c in value]
elif len(value) == 6:
value = [str(round(int(c1 + c2, 16)/255.0,3)) for c1, c2 in zip(value[::2], value[1::2])]
else:
raise Exception('Invalid hex number: #' + value)
return '({}, 1.0)'.format(', '.join(value))
def replaceRGBColorCode(self, match):
rgb = match.group(0)
values = rgb[4:-1].split(',')
values.append('255')
return str( tuple( round(c/255.,3) for c in values ) )
def replaceRGBAColorCode(self, match):
rgba = match.group(0)
values = rgba[5:-1].split(',')
values[3] = str( 255.*float(values[3]) )
return str( tuple( round(c/255.,3) for c in values ) )
def replaceColorsInText(self, text):
# _hex_colour = re.compile(r'#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b')
_hex_colour = re.compile(r'#([0-9a-fA-F]+|[0-9a-fA-F]+)\b')
text = _hex_colour.sub(self.replaceHexColorCode, text)
_rgb_colour = re.compile(r'rgb\(\s*(?:(\d{1,3})\s*,?){3}\)')
text = _rgb_colour.sub(self.replaceRGBColorCode, text)
_rgba_colour = re.compile(r'rgba\(\s*(?:(\d{1,3})\s*,?){3},\d+\.\d+\)')
text = _rgba_colour.sub(self.replaceRGBAColorCode, text)
for word, initial in self.dictionary.colors.items():
text = re.sub(r'\b'+word+r'\b',str(initial),text)
return text
# ------------------------------------------------------------
# styles
# : named_block+ EOF #NAMED
# | elements EOF #UNNAMED
# ;
# ------------------------------------------------------------
def enterNAMED(self):
self.indents = 1
self.blockCommaStack.append('')
self.write('styles = {\n' )
def exitNAMED(self):
self.blockCommaStack.pop()
self.write('\n}')
def enterUNNAMED(self):
self.indents = 1
self.write('styles = [' )
def exitUNNAMED(self):
self.write(']')
# ------------------------------------------------------------
# named_block
# : ('@name' STRING_LITERAL SEMI elements)
# ;
# ------------------------------------------------------------
def enterNamed_block(self, name):
self.write(self.blockCommaStack[-1])
self.write(self.indent()+name + ' : [')
self.blockCommaStack[-1] = ',\n'
self.indents += 1
def exitNamed_block(self):
self.write(self.indent()+']')
# ------------------------------------------------------------
# elements
# : element ( element )*
# ;
# ------------------------------------------------------------
def enterElements(self):
self.elementCommaStack.append('\n')
self.attribCommaStack.append("\n")
def exitElements(self):
self.write("\n")
self.indents -= 1
self.elementCommaStack.pop()
# ------------------------------------------------------------
# element
# : '@'? 'level' (STRUDEL def_name)? spec_conditions* (LPAREN condition RPAREN)? LCURLY attributes RCURLY
# | '@'? element_name (STRUDEL def_name)? (LPAREN condition RPAREN)? LCURLY attributes RCURLY
# ;
# ------------------------------------------------------------
def enterElement_name(self, name):
txt = self.toCamelCase(name)
self.write(self.indent()+txt+'(')
self.indents += 1
def enterElement(self, txt):
self.write(self.elementCommaStack[-1])
self.attribCommaStack.append("\n") # maybe we have a condition first
if 'level' in txt[:15]:
self.write(self.indent()+'Level(')
self.indents += 1
def exitElement(self):
self.indents -=1
self.write(self.indent()+')')
self.elementCommaStack[-1] = ",\n"
def enterCondition(self):
self.conditionContext = True
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'condition = lambda item : ' )
self.attribCommaStack[-1] = ',\n'
def exitCondition(self):
self.conditionContext = False
self.attribCommaStack[-1] = ',\n'
# ------------------------------------------------------------
# attributes
# : attribute*
# ;
# ------------------------------------------------------------
def enterAttributes(self):
pass
# self.attribCommaStack.append("\n")
def exitAttributes(self):
self.write("\n")
self.attribCommaStack.pop()
# ------------------------------------------------------------
# attribute
# : 'symmetry' COLON sym_expression SEMI
# | 'use' COLON use_expression SEMI
# | ('faces' | 'sharpEdges') COLON smooth_expression SEMI
# | attr_name COLON expression SEMI
# | attr_name COLON markup_block // markup
# ;
# sym_expression
# : IDENTIFIER
# ;
# use_expression
# : IDENTIFIER (COMMA IDENTIFIER)*
# ;
# smooth_expression
# : expression
# ;
# markup_block
# : LBRACK elements RBRACK
# ;
# ------------------------------------------------------------
def enterSym_expression(self, sym):
self.write(self.attribCommaStack[-1])
symmetry = self.toCamelCase(sym)
self.write(self.indent()+'symmetry = symmetry.'+symmetry )
self.attribCommaStack[-1] = ",\n"
def enterUse_expression(self, enterSimple_expr):
self.write(self.attribCommaStack[-1])
expression = self.literalize(enterSimple_expr,False)
self.write(self.indent()+'use = (' + expression + ',)' )
self.attribCommaStack[-1] = ",\n"
def enterSmooth_expression(self, name):
self.smoothContext = True
self.write(self.attribCommaStack[-1])
self.write(self.indent() + name + ' = ' )
self.attribCommaStack[-1] = ",\n"
def exitSmooth_expression(self):
self.smoothContext = False
def enterMarkup_block(self):
self.write(' [')
self.indents += 1
def exitMarkup_block(self):
self.write(self.indent()+']')
def enterAttr_name(self,name):
if name == 'class': # avoid conflict with Python keyword
name = 'cl'
self.write(self.attribCommaStack[-1])
self.write(self.indent()+name+' = ')
self.attribCommaStack[-1] = ",\n"
# ------------------------------------------------------------
# expression
# : simple_expr | function | alternatives
# ;
# alternatives
# : function (PIPE function)+
# ;
# ------------------------------------------------------------
def enterAlternatives(self):
self.alternativesContext = True
# self.inFunctionStack.append('inFunction')
self.functionContext = True
self.write(" Value(Alternatives(\n")
self.alterCommaStack.append("")
self.indents +=1
def exitAlternatives(self):
self.alternativesContext = False
self.alterCommaStack.pop()
self.indents -=1
self.write('\n'+self.indent()+'))')
# ------------------------------------------------------------
# function
# : 'attr' LPAREN string_literal RPAREN #ATTR
# | 'bldgAttr' LPAREN string_literal RPAREN #BUILDATTR
# | 'random_normal' LPAREN NUMBER RPAREN #RANDN
# | 'random_weighted' nested_list #RANDW
# | 'if' LPAREN conditional RPAREN function #COND
# | 'use_from' LPAREN IDENTIFIER RPAREN #USEFROM
# | 'per_building' LPAREN function RPAREN #PERBUILD
# | 'rgb' LPAREN NUMBER COMMA NUMBER COMMA NUMBER RPAREN #RGB
# | 'rgba' LPAREN NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER RPAREN #RGBA
# | constant #CONST
# | nested_list #NESTED
# | arith_atom #ARITH
# ;
# ------------------------------------------------------------
def enterATTR(self, attribute):
types = self.dictionary.getAttributeTypes(attribute)
if self.alternativesContext:
self.write(self.alterCommaStack[-1])
if len(types) == 1:
self.write( self.indent()+"FromAttr(" + attribute + ", " + types[0] + ')' )
else:
self.write( self.indent()+"FromAttr(" + attribute + ", " + types[0] + '),\n' )
self.write( self.indent()+"FromAttr(" + attribute + ", " + types[1] + ')' )
self.alterCommaStack[-1] = ",\n"
else:
if len(types) == 1:
self.write( "Value(FromAttr(" + attribute + ", " + types[0] + '))' )
else:
self.write('Value(Alternatives(\n')
self.indents += 1
self.write( self.indent()+"FromAttr(" + attribute + ", " + types[0] + '),\n' )
self.write( self.indent()+"FromAttr(" + attribute + ", " + types[1] + ')\n' )
self.indents -= 1
self.write(self.indent()+'))')
def enterBUILDATTR(self, attribute):
types = self.dictionary.getAttributeTypes(attribute)
if self.alternativesContext:
self.write(self.alterCommaStack[-1])
if len(types) == 1:
self.write( self.indent()+"FromBldgAttr(" + attribute + ", " + types[0] + ')' )
else:
self.write( self.indent()+"FromBldgAttr(" + attribute + ", " + types[0] + '),\n' )
self.write( self.indent()+"FromBldgAttr(" + attribute + ", " + types[1] + ')' )
self.alterCommaStack[-1] = ",\n"
else:
if len(types) == 1:
self.write( "Value(FromBldgAttr(" + attribute + ", " + types[0] + '))' )
else:
self.write('Value(Alternatives(\n')
self.indents += 1
self.write( self.indent()+"FromBldgAttr(" + attribute + ", " + types[0] + '),\n' )
self.write( self.indent()+"FromBldgAttr(" + attribute + ", " + types[1] + ')\n' )
self.indents -= 1
self.write(self.indent()+'))')
# ------------------------------------------------------------
# function
# : ...
# | 'random_normal' LPAREN NUMBER RPAREN #RANDN
# | 'random_weighted' nested_list #RANDW
# | ...
# ------------------------------------------------------------
def enterRANDN(self, value):
if self.alternativesContext or self.conditionContext:
self.write(self.alterCommaStack[-1])
self.write(self.indent()+'RandomNormal( ' + value + ' )')
self.alterCommaStack[-1] = ",\n"
else:
self.write('Value(RandomNormal( ' + value + ' ))')
def enterRANDW(self, li):
li = self.replaceColorsInText(li)
list = self.literalize(li,False)
if self.alternativesContext or self.conditionContext:
self.write(self.alterCommaStack[-1])
self.write(self.indent()+'RandomWeighted( ' + list + ' )')
self.alterCommaStack[-1] = ",\n"
else:
self.write('Value(RandomWeighted( ' + list + ' ))')
# ------------------------------------------------------------
# function
# : ...
# | 'if' LPAREN conditional RPAREN function #COND
# | ...
# ------------------------------------------------------------
def enterCOND(self, condition, result):
self.conditionContext = True
self.conditionalContext = True
if self.alternativesContext:
self.write(self.alterCommaStack[-1])
self.write( self.indent()+"Conditional(\n" )
self.indents += 1
self.write(self.indent()+'lambda item: ' )
self.alterCommaStack.append(',\n')
else:
self.write( "Value(Conditional(\n" )
self.indents += 1
self.write(self.indent()+'lambda item: ' )
def exitCOND(self):
self.indents -= 1
self.write("\n")
if self.conditionContext:
if self.alternativesContext:
self.write(self.indent()+")" )
else:
self.write(self.indent()+"))" )
else:
self.write(self.indent()+")" )
self.conditionContext = False
self.conditionalContext = False
# ------------------------------------------------------------
# function
# : ...
# | 'use_from' LPAREN IDENTIFIER RPAREN #USEFROM
# | 'per_building' LPAREN function RPAREN #PERBUILD
# | ...
# ------------------------------------------------------------
def enterUSEFROM(self, ident):
self.write('useFrom("' + ident + '")')
def enterPERBUILD(self):
if self.alternativesContext:
self.write(self.alterCommaStack[-1])
self.alterCommaStack[-1] = ''
self.write(self.indent()+'PerBuilding(\n')
self.indents += 1
else:
self.write('PerBuilding(')
def exitPERBUILD(self):
if self.alternativesContext:
self.indents -= 1
self.alterCommaStack[-1] = ',\n'
self.write('\n'+self.indent()+')')
else:
self.write(')')
# ------------------------------------------------------------
# function
# : ...
# | 'rgb' LPAREN NUMBER COMMA NUMBER COMMA NUMBER RPAREN #RGB
# | 'rgba' LPAREN NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER RPAREN #RGBA
# | ...
# ------------------------------------------------------------
def enterRGB(self, rgb):
expr = self.replaceColorsInText(rgb)
if self.alternativesContext or self.conditionalContext:
self.write(self.alterCommaStack[-1])
self.write( self.indent()+"Constant(" + expr + ')' )
self.alterCommaStack[-1] = ',\n'
else:
self.write('Value(Constant(' + expr + ')' )
def enterRGBA(self, rgba):
expr = self.replaceColorsInText(rgba)
if self.alternativesContext or self.conditionalContext:
self.write(self.alterCommaStack[-1])
self.write( self.indent()+"Constant(" + expr + ')' )
self.alterCommaStack[-1] = ',\n'
else:
self.write('Value(Constant(' + expr + ')' )
# ------------------------------------------------------------
# function
# : ...
# | constant #CONST
# | nested_list #NESTED
# | ...
# ------------------------------------------------------------
def enterCONST(self, text):
text = self.replaceColorsInText(text)
if self.conditionalContext:
self.write(',\n')
if self.smoothContext and text in ('smooth','flat','horizontal','side','all'):
expr = self.toCamelCase(text)
self.write( self.indent()+"Constant(smoothness." + expr + ')' )
else:
expr = self.literalize(text,False)
self.write( self.indent()+"Constant(" + expr + ')' )
elif self.alternativesContext:
self.write(self.alterCommaStack[-1])
expr = self.literalize(text,False)
self.write( self.indent()+"Constant(" + expr + ')' )
self.alterCommaStack[-1] = ',\n'
else:
expr = self.literalize(text,False)
self.write(expr)
def enterNESTED(self, li):
li = self.replaceColorsInText(li)
list = self.literalize(li,False)
if self.alternativesContext:
self.write(self.alterCommaStack[-1])
self.write( self.indent()+list )
self.alterCommaStack[-1] = ',\n'
else:
self.write( list )
def exitINNESTED(self, li):
list = self.literalize(li, True)
self.write( list )
# ------------------------------------------------------------
# spec_conditions
# : LBRACK spec_condition RBRACK #SPEC_LEVEL
# ;
# spec_condition
# : '@roof' #SPEC_ROOF
# | '@all' #SPEC_ALL
# | NUMBER COLON NUMBER #SPEC_FULL_INDX
# | NUMBER COLON #SPEC_LEFT_INDX
# | COLON NUMBER #SPEC_RIGHT_INDX
# ;
# ------------------------------------------------------------
def enterSPEC_LEVEL(self):
self.spec_condition = []
def exitSPEC_LEVEL(self):
roof_val = 'True' if '@roof' in self.spec_condition else 'False'
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'roofLevels = '+roof_val )
self.attribCommaStack[-1] = ",\n"
all_val = 'True' if '@all' in self.spec_condition else 'False'
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'allLevels = '+all_val )
self.attribCommaStack[-1] = ",\n"
self.spec_condition = []
def enterSPEC_ROOF(self, cond):
self.spec_condition.append(cond)
def enterSPEC_FULL_INDX(self, index_text):
indices = index_text.split(':')
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'indices = ('+indices[0]+','+indices[1]+')' )
self.attribCommaStack[-1] = ",\n"
def enterSPEC_SINGLE(self, index_text):
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'indices = ('+index_text+','+index_text+')' )
self.attribCommaStack[-1] = ",\n"
def enterSPEC_LEFT_INDX(self, index_text):
indices = index_text.split(':')
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'indices = ('+indices[0]+',-1)' )
self.attribCommaStack[-1] = ",\n"
def enterSPEC_RIGHT_INDX(self, index_text):
indices = index_text.split(':')
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'indices = (0,'+indices[1]+')' )
self.attribCommaStack[-1] = ",\n"
# ------------------------------------------------------------
# arith_atom
# : 'item' '.' IDENTIFIER # ATOM_SINGLE
# | 'item' '.' IDENTIFIER '.' IDENTIFIER # ATOM_SINGLE
# | 'item' '.' IDENTIFIER LBRACK STRING_LITERAL RBRACK # ATOM_FROMATTR
# | 'item' LBRACK STRING_LITERAL RBRACK # ATOM_FROMATTR_SHORT
#| | 'style' '.' IDENTIFIER # ATOM_STYLE
# | identifier # ATOM_IDENT
# | NUMBER # ATOM_IDENT
# | STRING_LITERAL # ATOM_IDENT
# ------------------------------------------------------------
def enterATOM_SINGLE(self, atom):
self.write(atom)
def enterATOM_FROMATTR(self ,ident, literal):
if self.conditionContext or self.conditionalContext:
self.write( 'item.' + ident +'.getStyleBlockAttr(' + literal + ')' )
else:
# self.write(self.alterCommaStack[-1])
identifier = ident.capitalize()
self.write("FromStyleBlockAttr("+literal+",FromStyleBlockAttr."+identifier+")")
# self.alterCommaStack[-1] = ",\n"
def enterATOM_FROMATTR_SHORT(self, literal):
if self.conditionContext:
self.write( 'item.getStyleBlockAttr(' + literal + ')' )
else:
self.write(self.alterCommaStack[-1])
self.write(self.indent()+"FromStyleBlockAttr("+literal+")")
self.alterCommaStack[-1] = ",\n"
def enterATOM_STYLE(self, identifier):
if self.conditionContext:
self.write( 'item.styleBlock.' + identifier )
else:
self.write(self.alterCommaStack[-1])
self.write(self.indent()+'item.styleBlock.' + identifier)
self.alterCommaStack[-1] = ",\n"
def enterATOM_IDENT(self, ident):
self.write(ident)
def enterConst_atom(self, atom):
const = self.literalize(atom,True)
self.write( ',\n'+self.indent()+"Constant(" + const + ')' )
# ------------------------------------------------------------
# ... and all the remaining details
# ------------------------------------------------------------
def enterDef_name(self, definition):
self.write(self.attribCommaStack[-1])
self.write(self.indent()+'defName = "' + definition + '"' )
self.attribCommaStack[-1] = ",\n"
def enterConstant(self, text):
self.enterCONST(text)
def enterSimple_expr(self, text):
if self.smoothContext:
return
if text in ('true','false'):
expr = text.capitalize()
else:
expr = self.replaceColorsInText(text)
expr = self.literalize(expr,False)
if self.alternativesContext or self.conditionalContext: # ???or self.context in ( "conditional" ):
# self.write(self.alterCommaStack[-1])
self.write( self.indent()+"Constant(" + expr + ')' )
else:
self.write(expr)
def enterAri_lparen(self):
self.write( ' (' )
def enterAri_rparen(self):
self.write( ') ' )
def enterIdentifier(self, ident):
if self.smoothContext and ident in ('smooth','flat','horizontal','side','all') :
self.write('smoothness.' + ident )
def enterInop(self, op):
self.write( ' '+op+' ' )
def enterRelop(self, op):
self.write( ' '+op+' ' )
def enterLogicop(self, op):
self.write( ' '+op+' ' )
def enterNotop(self, op):
self.write( ' '+op+' ' )
def enterArith_op(self, op):
self.write( ' '+op+' ' )