-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcodeDogParser.py
797 lines (735 loc) · 39.1 KB
/
codeDogParser.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# This module parses CodeDog syntax
import re
import progSpec
from progSpec import cdlog, cdErr, logLvl
from pyparsing import *
ParserElement.enablePackrat()
commentsToActivate = {}
def logBSL(s, loc, toks):
cdlog(3,"Parsing Tags...")
def logTags(s, loc, toks):
global commentsToActivate
for tagValue in toks:
for tagList in tagValue:
if tagList[0]=="commentsToActivate":
for tag in tagList[1][0][1]:
tagName = tag[0][0]
commentsToActivate[tagName] = 'active'
def logObj(s, loc, toks):
cdlog(3,"PARSED: {}".format(str(toks[0][0])+' '+toks[0][1][0]))
def logFieldDef(s, loc, toks):
cdlog(4,"Field: {}".format(str(toks)))
# # # # # # # # # # # # # BNF Parser Productions for CodeDog syntax # # # # # # # # # # # # #
ParserElement.enablePackrat()
####################################### T A G S A N D B U I L D - S P E C S
docComment = Group("/*^" + SkipTo("*/") + Suppress("*/") | "//^" + restOfLine)
identifier = Word(alphanums + "_")
tagID = identifier("tagID")
tagDefList = Forward()
tagValue = Forward()
fullFieldDef = Forward()
tagMap = Group('{' + tagDefList + '}')("tagMap")
tagList = Group('[' + Group(Optional(delimitedList(Group(tagValue), ',')))("tagListContents") + ']')
backTickStr = Suppress("`") + SkipTo("`") + Suppress("`")
tagValue <<= Group((Suppress('<') + Group(fullFieldDef)("tagType") + Suppress('>')) | quotedString | backTickStr | Word(alphanums+'-*_./') | tagList | tagMap)("tagValue")
tagDef = Group(tagID + Suppress("=") + tagValue)("tagDef*")
tagDefList <<= Group(ZeroOrMore(tagDef))("tagDefList")
buildID = identifier("buildID")
buildDefList = Group(tagDefList)("buildDefList")
buildSpec = Group(buildID + Suppress(":") + buildDefList + ";")("buildSpec")
buildSpecList = Group(OneOrMore(buildSpec))("buildSpecList")
####################################### B A S I C T Y P E S
expr = Forward()
CID = identifier("CID")
CIDList = Group(delimitedList(CID, ','))("CIDList")
className = CID("className")
classSpec = Forward()
cppType = Keyword("void") | Keyword("bool") | Keyword("int32") | Keyword("int64") | Keyword("double") | Keyword("char") | Keyword("uint32") | Keyword("uint64") | Keyword("string") | Keyword("int")
HexNums = Combine((Literal("0X") | Literal("0x")) + Word(hexnums))
BinNums = Combine((Literal("0B") | Literal("0b")) + Word("01"))
intNum = HexNums | BinNums | Word(nums)
numRange = Group(intNum + ".." + intNum)("numRange")
varType = classSpec | cppType | numRange
boolValue = Keyword("true") | Keyword("false")
floatNum = Combine(intNum + "." + intNum)("floatNum")
value = Forward()
listVal = "[" + delimitedList(expr, ",") + "]"
strMapVal = "{" + delimitedList(quotedString + ":" + expr, ",") + "}"
value <<= boolValue | floatNum | intNum | quotedString | listVal | strMapVal
####################################### E X P R E S S I O N S
parameters = Forward()
owners = Forward()
varSpec = Group(Optional(owners)("owner") + varType("varType") )("varSpec")
varSpecList = Group(Optional(delimitedList(varSpec, ',')))("varSpecList")
typeArgList = Group(Literal("<") + CIDList + Literal(">"))("typeArgList")
reqTagList = Group(Suppress(Literal("<")) + varSpecList + Optional(Literal(":")("optionalTag") + tagDefList) + Suppress(Literal(">")))("reqTagList")
classSpec <<= Group(className + Optional(reqTagList('reqTagList')))("classSpec")
classDefID = Group(className + Optional(typeArgList))("classDefID")
arrayRef = Group('[' + expr('startOffset') + Optional(( ':' + expr('endOffset')) | ('..' + expr('itemLength'))) + ']')
firstRefSeg = NotAny(owners) + Group((CID | arrayRef) + Optional(parameters))
secondRefSeg= Group((Suppress('.') + CID | arrayRef) + Optional(parameters))
varRef = Group(firstRefSeg + ZeroOrMore(secondRefSeg))
lValue = varRef("lValue")
factor = Group( value | ('(' + expr + ')') | ('!' + expr) | ('-' + expr) | varRef("varFuncRef"))
term = Group( factor + Optional(Group(OneOrMore(Group(oneOf('* / %') + factor )))))
plus = Group( term + Optional(Group(OneOrMore(Group(oneOf('+ -') + term )))))
comparison = Group( plus + Optional(Group(OneOrMore(Group(oneOf('< > <= >=') + ~FollowedBy("-") + plus )))))
isEQ = Group( comparison + Optional(Group(OneOrMore(Group(oneOf('== != === !==') + comparison )))))
iOr = Group( isEQ + Optional(Group(OneOrMore(Group('&' + isEQ )))))
xOr = Group( iOr + Optional(Group(OneOrMore(Group('^' + iOr )))))
bar = Group( xOr + Optional(Group(OneOrMore(Group('|' + xOr )))))
logAnd = Group( bar + Optional(Group(OneOrMore(Group(Keyword('and') + bar )))))
logOr = Group( logAnd + Optional(Group(OneOrMore(Group(Keyword('or') + logAnd )))))
expr <<= Group( logOr + Optional(Group(Group(Literal("<-")("assignAsExpr") + logOr ))))("expr")
swap = Group(lValue + Literal("<->")("swapID") + lValue ("RightLValue"))("swap")
rValue = Group(expr)("rValue")
assign = lValue + Combine("<" + (Optional((Word(alphanums + '_') | '+' | ('-' + FollowedBy("-")) | '*' | '/' | '%' | '<<' | '>>' | '&' | '^' | '|')("assignTag"))) + "-")("assignID") + rValue
parameters <<= "(" - Optional(Group(delimitedList(rValue, ','))) + Suppress(")")
initParams = "{" + Optional(Group(delimitedList(rValue, ','))("initParams")) + Suppress("}")
######################################## F U N C T I O N S
verbatim = Group(Literal(r"<%") + SkipTo(r"%>", include=True))
fieldDef = Forward()
commentedFieldDef = Group(Optional(docComment) + fieldDef('fieldDef'))
argList = Group(verbatim | Optional(delimitedList(Group(fieldDef))))("argList")
actionSeq = Forward()
defaultCase = Group(Keyword("default") + Suppress(":") + actionSeq("caseAction"))("defaultCase")
switchCase = Group(Keyword("case") + OneOrMore(rValue + Suppress(":"))("caseValues") - actionSeq("caseAction"))
switchStmt = Group(Keyword("switch")("switchStmt") - "(" - rValue("switchKey") - ")" - "{" - OneOrMore(switchCase)("switchCases") - Optional(defaultCase)("optionalDefaultCase") + "}")
conditionalAction = Forward()
conditionalAction <<= Group(
Group(Keyword("if") - "(" + rValue("ifCondition") + ")" + actionSeq("ifBody"))("ifStatement")
+ Optional(Group((Keyword("else") | Keyword("but")) + Group(actionSeq | conditionalAction)("elseBody"))("optionalElse"))
)("conditionalAction")
protectAction = Group(Keyword("protect")("protectStmt") - "(" + rValue("mutex") + ")" + actionSeq("criticalSection"))("protectAction")
traversalModes = Keyword("Forward") | Keyword("Backward") | Keyword("Preorder") | Keyword("Inorder") | Keyword("Postorder") | Keyword("BreadthFirst") | Keyword("DF_Iterative")
rangeSpec = Group(Keyword("RANGE") - '(' + rValue + ".." + rValue + ')')
whileSpec = Group(Keyword('WHILE') + '(' + expr + ')')
newWhileSpec = Group(Keyword('while') + '(' + expr + ')')
whileAction = Group(newWhileSpec('newWhileSpec') + actionSeq)("whileAction")
fileSpec = Group(Keyword('FILE') + '(' + expr + ')')
keyRange = Group(rValue("repList") + Keyword('from') + rValue('fromPart') + Keyword('to') + rValue('toPart'))
repeatedAction = Group(
Keyword("withEach")("repeatedActionID") - CID("repName") + "in"
+ Optional(traversalModes("traversalMode"))
+ (whileSpec('whileSpec') | rangeSpec('rangeSpec') | keyRange('keyRange') | fileSpec('fileSpec') | rValue("repList"))
+ Optional(":")("optionalColon") # TODO: remove. this is deprecated.
+ Optional(Keyword("where") + "(" + expr("whereExpr") + ")")
+ Optional(Keyword("until") + "(" + expr("untilExpr") + ")")
+ actionSeq
)("repeatedAction")
action = Group((assign("assign") | swap('swap') | varRef("funcCall") | fieldDef('fieldDef'))) + Optional(";").suppress()
actComment = Group(Combine("//:"+ Word(alphanums + r"/")("filterTag")+"::")("actComment") + action)
actionSeq <<= Group(Literal("{")("actSeqID") - (ZeroOrMore(switchStmt | conditionalAction | repeatedAction | whileAction | protectAction | actionSeq | action | actComment))("actionList") + "}")("actionSeq")
rValueVerbatim = Group("<%" + SkipTo("%>", include=True))("rValueVerbatim")
funcBody = Group(actionSeq | rValueVerbatim)("funcBody")
######################################### F I E L D D E S C R I P T I O N S
nameAndVal = Group(
(":" + CID("fieldName") + "(" + argList + Literal(")")('argListTag') + Optional(Literal(":")("optionalTag") + tagDefList) + "<-" - funcBody ) # Function Definition
| (":" + CID("fieldName") + Group(initParams)("parameters"))
| (":" + CID("fieldName") + "<-" - (rValue("givenValue") | rValueVerbatim))
| (":" + "<-" - (rValue("givenValue") | funcBody))
| (":" + CID("fieldName") + Optional("(" + argList + Literal(")")('argListTag')) - ~Word("{"))
| (Literal("::")('allocDoubleColon') + CID("fieldName") + Group(initParams)("parameters"))
| (Literal("::")('allocDoubleColon') + CID("fieldName") + "<-" - (rValue("givenValue")))
| (Literal("::")('deprecateDoubleColon') + CID("fieldName") + Group(parameters)("parameters"))# deprecated
| (Literal("::")('allocDoubleColon') + CID("fieldName"))
)("nameAndVal")
datastructID = Group(Keyword("list") | Keyword("opt") | Keyword("map") | Keyword("multimap") | Keyword("tree") | Keyword("graph") | Keyword("iterableList"))('datastructID')
arraySpec = Group('[' + Optional(owners)('owner') + datastructID + Optional(Group(intNum | Optional(Group(owners)('IDXowner')) + varType('idxBaseType'))('indexType')) + ']')("arraySpec")
meOrMy = Keyword("me") | Keyword("my")
modeSpec = Optional(meOrMy)('owner') + Keyword("mode")("modeIndicator") - "[" - CIDList("modeList") + "]" + nameAndVal
altModeSpec = Keyword("mode")("altModeIndicator") - "[" - Group(delimitedList(CID, ','))("altModeList") + "]"
flagDef = Optional(meOrMy)('owner') + Keyword("flag")("flagIndicator") - nameAndVal
baseType = cppType | numRange
######################################### O B J E C T D E S C R I P T I O N S
fieldDefs = ZeroOrMore(commentedFieldDef)("fieldDefs")
SetFieldStmt = Group(Word(alphanums + "_.") + '=' + Word(alphanums + r"_. */+-(){}[]\|<>,./?`~@#$%^&*=:!'" + '"'))
coFactualEl = Group("(" + Group(fieldDef + "<=>" + Group(OneOrMore(SetFieldStmt + Suppress(';')))) + ")")("coFactualEl")
sequenceEl = "{" - fieldDefs + "}"
alternateEl = "[" - Group(OneOrMore((coFactualEl | fieldDef) + Optional("|").suppress()))("fieldDefs") + "]"
anonModel = sequenceEl("sequenceEl") | alternateEl("alternateEl")
owners <<= Keyword("const") | Keyword("me") | Keyword("my") | Keyword("our") | Keyword("their") | Keyword("we") | Keyword("itr") | Keyword("id_our") | Keyword("id_their")
fullFieldDef <<= Optional('>')('isNext') + Optional(owners)('owner') + Group(baseType | altModeSpec | classSpec | Group(anonModel) | datastructID)('fieldType') + Optional(arraySpec) + Optional(nameAndVal)
fieldDef <<= Group(flagDef('flagDef') | modeSpec('modeDef') | (quotedString('constStr') + Optional("[opt]") + Optional(":"+CID)) | intNum('constNum') | nameAndVal('nameVal') | fullFieldDef('fullFieldDef'))("fieldDef")
modelTypes = (Keyword("model") | Keyword("struct") | Keyword("string") | Keyword("stream"))
classDef = Group(modelTypes + classDefID + Optional(Literal(":")("optionalTag") + tagDefList) + (Keyword('auto') | anonModel))("classDef")
doPattern = Group(Keyword("do") + classSpec + Suppress("(") + CIDList + Suppress(")"))("doPattern")
macroDef = Group(Keyword("#define") + CID('macroName') + Suppress("(") + Optional(CIDList('macroArgs')) + Suppress(")") + Group("<%" + SkipTo("%>", include=True))("macroBody"))
classList = Group(ZeroOrMore(docComment | classDef | doPattern | macroDef))("classList")
classDef.set_parse_action(logObj)
fieldDef.set_parse_action(logFieldDef)
######################################### P A R S E R S T A R T S Y M B O L
progSpecParser = Group(Optional(buildSpecList.set_parse_action(logBSL)) + tagDefList.set_parse_action(logTags) + classList)("progSpecParser")
libTagParser = Group(Optional(buildSpecList.set_parse_action(logBSL)) + tagDefList.set_parse_action(logTags) + (modelTypes|Keyword("do")|Keyword("#define")|StringEnd()))("libTagParser")
# # # # # # # # # # # # # E x t r a c t P a r s e R e s u l t s # # # # # # # # # # # # #
def parseInput(inputStr):
cdlog(2, "Parsing build-specs...")
progSpec.saveTextToErrFile(inputStr)
try:
localResults = progSpecParser.parseString(inputStr, parseAll=True)
except ParseBaseException as pe:
cdErr( "While parsing: {}".format( pe))
return localResults
autoClassNameIdx = 1
def extractTagDefs(tagResults):
global autoClassNameIdx
localTagStore = {}
for tagSpec in tagResults:
tagVal = tagSpec.tagValue[0]
if ((not isinstance(tagVal, str)) and len(tagVal)>=2):
if(tagVal.tagListContents): #tagVal is tagList
tagValues=[]
for each in tagVal.tagListContents:
tagValues.append(each.tagValue[0])
elif(tagVal.tagDefList): #tagVal is tagMap
tagValues=extractTagDefs(tagVal.tagDefList)
elif("tagType" in tagVal):
autoClassName = "autoClass" + str(autoClassNameIdx)
autoClassNameIdx += 1
tagValues=packFieldDef(tagVal, autoClassName, '')
else: #tagVal is an empty parseResult
tagValues = []
tagVal=tagValues
# Remove quotes
elif (len(tagVal)>=2 and (tagVal[0] == '"' or tagVal[0] == "'") and (tagVal[0]==tagVal[-1])):
tagVal = tagVal[1:-1]
#print(tagSpec.tagID, " is ", tagVal)
localTagStore[tagSpec.tagID] = tagVal
return localTagStore
def extractTypeArgList(typeArgList):
localListStore = []
for typeArg in typeArgList[1]:
localListStore.append(typeArg)
return localListStore
nameIDX=1
def packFieldDef(fieldResult, className, indent, comment=None):
global nameIDX
# ['(', [['>', 'me', ['CID'], [':', 'tag']], '<=>', [[[['hasTag']], '=', [[[[[[[['54321'], []], []], []], []], []], []]]]]], ')']
coFactuals=None
if fieldResult[0]=='(': # Reorganize Cofactuals if they are here
coFactuals = fieldResult[1][2]
fieldResult= fieldResult[1][0]
fieldDef={}
argList=[]
paramList=[]
innerDefs=[]
optionalTags=None
isNext=False;
if(fieldResult.isNext): isNext=True
if(fieldResult.owner): owner=fieldResult.owner;
else: owner='me';
isAllocated = False
hasFuncBody = False
packedTArgList = None
if(fieldResult.fieldType):
fieldType=fieldResult.fieldType[0];
if not isinstance(fieldType, str):
if 'reqTagList' in fieldType:
reqTagList = fieldType['reqTagList']
packedTArgList = []
for reqTag in reqTagList[0]:
reqTagVarType = reqTag['varType'][0][0]
reqTagOwner = 'me'
if 'owner' in reqTag: reqTagOwner = reqTag['owner']
packedReqTag={'tArgOwner': reqTagOwner, 'tArgType': reqTagVarType}
packedTArgList.append(packedReqTag)
fieldType=[fieldType[0],packedTArgList]
if(fieldType[0]=='[' or fieldType[0]=='{'):
if fieldType[0]=='{': fieldList=fieldType[1:-1]
elif fieldType[0]=='[': fieldList=fieldType[1]
for innerField in fieldList:
innerFieldDef=packFieldDef(innerField, className, indent+' ')
innerDefs.append(innerFieldDef)
#print("FIELDTYPE is an inline SEQ or ALT:",innerFieldDef)
else: fieldType=None;
isAContainer = False
if(fieldResult.arraySpec):
arraySpec=fieldResult.arraySpec
isAContainer = True
print(" ****Deprecated ArraySpec found: ", arraySpec)
else: arraySpec=None
varOwner = owner
if isAContainer:
if "owner" in arraySpec:
varOwner = arraySpec['owner']
else: varOwner = 'me'
if(fieldResult.nameAndVal):
nameAndVal = fieldResult.nameAndVal
#print("nameAndVal = ", nameAndVal.dump())
if(nameAndVal.fieldName):
fieldName = nameAndVal.fieldName
#print("FIELD NAME", fieldName)
else: fieldName=None;
if(nameAndVal.allocDoubleColon):
if varOwner == 'me' or varOwner == 'we':
print("Error: unable to allocate variable with owner me or we: ", fieldName)
exit(1)
else: isAllocated = True
if(nameAndVal.givenValue):
givenValue = nameAndVal.givenValue
elif(nameAndVal.funcBody):
[funcBodyOut, funcTextVerbatim] = extractFuncBody(fieldName, nameAndVal.funcBody)
givenValue=[funcBodyOut, funcTextVerbatim]
hasFuncBody = True
#print("\n\n[funcBodyOut, funcTextVerbatim] ", givenValue)
elif(nameAndVal.rValueVerbatim):
givenValue = ['', nameAndVal.rValueVerbatim[1]]
else: givenValue = None;
if(nameAndVal.argListTag):
for argSpec in nameAndVal.argList:
argList.append(packFieldDef(argSpec.fieldDef, className, indent+" "))
else: argList=None;
if 'parameters' in nameAndVal:
if('deprecateDoubleColon'in nameAndVal):
print(" ***deprecated doubleColon in nameAndVal at: ", fieldName)
exit(1)
if(str(nameAndVal.parameters)=="['(']"): prmList={}
else: prmList=nameAndVal.parameters[1]
for param in prmList:
paramList.append(param)
if(isAllocated==False): # use a constructor instead of assignment
paramList.append("^&useCtor//8")
else: paramList=None
if(nameAndVal.optionalTag): optionalTags=extractTagDefs(nameAndVal.tagDefList)
else:
givenValue = None;
fieldName=None;
if(fieldResult.flagDef):
cdlog(3,"FLAG: {}".format(fieldResult))
if(arraySpec): cdErr("Lists of flags are not allowed")
fieldDef=progSpec.packField(className, False, owner, 'flag', arraySpec, packedTArgList, fieldName, None, paramList, givenValue, isAllocated, hasFuncBody)
elif(fieldResult.modeDef):
cdlog(3,"MODE: {}".format(fieldResult))
modeList=fieldResult.modeList
if(arraySpec): cdErr("Lists of modes are not allowed")
fieldDef=progSpec.packField(className, False, owner, 'mode', arraySpec, packedTArgList, fieldName, None, paramList, givenValue, isAllocated, hasFuncBody)
fieldDef['typeSpec']['enumList']=modeList
elif(fieldResult.constStr):
if fieldName==None: fieldName="constStr"+str(nameIDX); nameIDX+=1;
if(len(fieldResult)>1 and fieldResult[1]=='[opt]'):
arraySpec={'datastructID': 'opt'};
if(len(fieldResult)>3 and fieldResult[3]!=''):
fieldName=fieldResult[3]
givenValue = fieldResult.constStr[1:-1]
fieldDef=progSpec.packField(className, True, 'const', 'string', arraySpec, packedTArgList, fieldName, None, paramList, givenValue, isAllocated, hasFuncBody)
elif(fieldResult.constNum):
cdlog(3,"CONST Num: {}".format(fieldResult))
if fieldName==None: fieldName="constNum"+str(nameIDX); nameIDX+=1;
fieldDef=progSpec.packField(className, True, 'const', 'int', arraySpec, packedTArgList, fieldName, None, paramList, givenValue, isAllocated, hasFuncBody)
elif(fieldResult.nameVal):
cdlog(3,"NameAndVal: {}".format(fieldResult))
fieldDef=progSpec.packField(className, None, None, None, arraySpec, packedTArgList, fieldName, argList, paramList, givenValue, isAllocated, hasFuncBody)
elif(fieldResult.fullFieldDef):
fieldTypeStr=str(fieldType)[:50]
cdlog(3,"FULL FIELD: {}".format(str([isNext, owner, fieldTypeStr+'... ', arraySpec, packedTArgList, fieldName])))
fieldDef=progSpec.packField(className, isNext, owner, fieldType, arraySpec, packedTArgList, fieldName, argList, paramList, givenValue, isAllocated, hasFuncBody)
else: cdErr("Error in packing FieldDefs: {}".format(fieldResult))
if len(innerDefs)>0: fieldDef['innerDefs'] = innerDefs
if coFactuals!=None: fieldDef['coFactuals'] = coFactuals
if optionalTags!=None: fieldDef['tags'] = optionalTags
if comment!=None: fieldDef['comment'] = comment
return fieldDef
def parseResultsToListOfParseResults(parseSegment):
"""This splits a ParseResults into a list of the top level ParseResults
"""
myList = []
for seg in parseSegment:
myList.append(seg)
return myList
def extractActItem(funcName, actionItem):
global funcsCalled
global commentsToActivate
thisActionItem=None
if actionItem.fieldDef:
thisActionItem = {'typeOfAction':"newVar", 'fieldDef':packFieldDef(actionItem.fieldDef, '', ' LOCAL:')}
elif actionItem.switchStmt:
switchKey = actionItem.switchKey
switchCases = actionItem.switchCases
defaultCaseAction = None
if actionItem.optionalDefaultCase:
defaultCaseAction = extractActSeq(funcName, actionItem.defaultCase.caseAction)
casesList=[]
for sCase in switchCases:
CaseActSeq = extractActSeq(funcName, sCase.caseAction)
casesList.append([sCase.caseValues, CaseActSeq])
thisActionItem = {'typeOfAction':'switchStmt', 'switchKey':switchKey, 'switchCases':casesList, 'defaultCase':defaultCaseAction}
elif actionItem.ifStatement: # Conditional
ifCondition = actionItem.ifStatement.ifCondition
IfBodyIn = actionItem.ifStatement.ifBody
ifBodyOut = extractActSeq(funcName, IfBodyIn)
elseBodyOut = {}
if (actionItem.optionalElse):
elseBodyIn = actionItem.optionalElse.elseBody
if (elseBodyIn.conditionalAction):
elseBodyOut = ['if' , [extractActItem(funcName, elseBodyIn.conditionalAction)] ]
elif (elseBodyIn.actionSeq):
elseBodyOut = ['action', extractActItem(funcName, elseBodyIn.actionSeq)]
thisActionItem = {'typeOfAction':"conditional", 'ifCondition':ifCondition, 'ifBody':ifBodyOut, 'elseBody':elseBodyOut}
# Repeated Action withEach
elif actionItem.repeatedActionID or actionItem.newWhileSpec:
repName = actionItem.repName
repList = actionItem.repList
repBodyIn = actionItem.actionSeq
repBodyOut = extractActSeq(funcName, repBodyIn)
traversalMode=None
if actionItem.optionalColon:
print(" optionalColon in repeatedAction is deprecated.")
if actionItem.traversalMode:
traversalMode = actionItem.traversalMode
whileSpec=None
if actionItem.whileSpec:
whileSpec = actionItem.whileSpec
if actionItem.newWhileSpec:
whileSpec = actionItem.newWhileSpec
rangeSpec=None
if actionItem.rangeSpec:
rangeSpec = actionItem.rangeSpec
keyRange=None
if actionItem.keyRange:
keyRange = actionItem.keyRange
whereExpr = ''
untilExpr = ''
if actionItem.whereExpr:
whereExpr = actionItem.whereExpr
if actionItem.untilExpr:
untilExpr = actionItem.untilExpr
thisActionItem = {'typeOfAction':"repetition" ,'repName':repName, 'whereExpr':whereExpr, 'untilExpr':untilExpr, 'repBody':repBodyOut,
'repList':repList, 'traversalMode':traversalMode, 'rangeSpec':rangeSpec, 'whileSpec':whileSpec, 'keyRange':keyRange}
# Action sequence
elif actionItem.actSeqID:
actionListIn = actionItem
actionListOut = extractActSeq(funcName, actionListIn)
thisActionItem = {'typeOfAction':"actionSeq", 'actionList':actionListOut}
# Assign
elif (actionItem.assign):
RHS = parseResultsToListOfParseResults(actionItem.rValue)
LHS = parseResultsToListOfParseResults(actionItem.lValue)
assignTag = ''
if (actionItem.assignID[0] != '<-'):
if not isinstance(actionItem.assignID, str):
assignTag = actionItem.assignID.assignTag
#print(RHS, LHS)
thisActionItem = {'typeOfAction':"assign", 'LHS':LHS, 'RHS':RHS, 'assignTag':assignTag}
# Swap
elif (actionItem.swap):
print("swap: ", actionItem[0][0][0])
RHS = actionItem[0][2][0]
LHS = actionItem[0][0][0]
thisActionItem = {'typeOfAction':"swap", 'LHS':LHS, 'RHS':RHS}
# Function Call
elif actionItem.funcCall:
calledFunc = actionItem.funcCall
# Verify that calledFunc is a function and error out if not. (The last segment should have '(' as its second item.)
calledFuncLastSegment = calledFunc[-1]
if len(calledFuncLastSegment)<2 or calledFuncLastSegment[1] != '(':
cdErr("Expected a function, not a variable: {}".format(calledFuncLastSegment))
thisActionItem = {'typeOfAction':"funcCall", 'calledFunc':calledFunc}
calledFuncName = calledFuncLastSegment[0]
if(len(calledFuncLastSegment)<=2): calledFuncParams=[]
else:
calledFuncParams = calledFuncLastSegment[2]
progSpec.appendToFuncsCalled(calledFuncName, calledFuncParams)
# Function Call
elif actionItem.protectStmt:
protectStmt = actionItem.protectStmt
mutex = actionItem.mutex
critSectionIn = actionItem.criticalSection
critSectionOut = extractActSeq(funcName, critSectionIn)
thisActionItem = {'typeOfAction':"protect", 'mutex':mutex, 'criticalSection':critSectionOut}
elif actionItem.actComment:
filterTag = actionItem.actComment.filterTag
for tag in commentsToActivate:
if tag[-1]=="/":
filterCpy = filterTag+"/"
if tag == filterCpy[0:len(tag)]:
thisActionItem = extractActItem(funcName, actionItem[1])
break
elif filterTag==tag:
thisActionItem = extractActItem(funcName, actionItem[1])
break
else:
cdErr("problem in extractActItem: actionItem:".format(str(actionItem)))
exit(1)
return thisActionItem
def extractActSeq(funcName, childActSeq):
actionList = childActSeq.actionList
actSeq = []
for actionItem in actionList:
thisActionItem = extractActItem(funcName, actionItem)
if thisActionItem!=None: actSeq.append(thisActionItem)
return actSeq
def extractFuncBody(funcName, funcBodyIn):
'''Extract body of funcName (str) from funcBodyIn (parseResults)
Returns two values: funcBodyOut for CodeDog defined body & funcTextVerbatim for verbatim text.
If body is verbatim: funcBodyOut is an empty string, funcTextVerbatim is a string
If body is CodeDog: funcBodyOut is a list of stuff, funcTextVerbatim is an empty string
'''
if funcBodyIn.rValueVerbatim:
funcBodyOut = ""
funcTextVerbatim = funcBodyIn.rValueVerbatim[1] # opening and closing verbatim symbols are indices 0 and 2
elif funcBodyIn.actionSeq:
funcBodyOut = extractActSeq(funcName, funcBodyIn.actionSeq)
funcTextVerbatim = ""
else:
cdErr("problem in extractFuncBody: funcBodyIn has no rValueVerbatim or actionSeq")
exit(1)
return funcBodyOut, funcTextVerbatim
def extractFieldDefs(ProgSpec, className, stateType, fieldResults):
cdlog(logLvl(), "EXTRACTING {}".format(className))
for fieldResult in fieldResults:
comment = None
if(fieldResult[0][0]=='/*^' or fieldResult[0][0]=='//^' ): comment = fieldResult[0]
fieldDef=packFieldDef(fieldResult.fieldDef, className, '', comment)
progSpec.addField(ProgSpec, className, stateType, fieldDef)
def extractBuildSpecs(buildSpecResults): # buildSpecResults is sometimes a parseResult, often an empty string
resultOfExtractBuildSpecs = []
#print("buildSpecResults: ", buildSpecResults)
if (len(buildSpecResults)==0):
return resultOfExtractBuildSpecs
else:
for each_buildSpec in buildSpecResults:
# If this doesn't loop when expected, it may be a result of a ZeroOrMore/deLimitedList call in parser
# chain of buildSpec without trailing * in name, causing each new builSpec to overwrite previous value.
# Or it may be that another loop is needed over contents of tagDefList
spec = [each_buildSpec.buildID, extractTagDefs(each_buildSpec.buildDefList.tagDefList)]
resultOfExtractBuildSpecs.append(spec)
return resultOfExtractBuildSpecs
def extractObjectSpecs(ProgSpec, classNames, spec, stateType,description, comments):
className=spec.classDefID[0]
configType="unknown"
if(spec.sequenceEl): configType="SEQ"
elif(spec.alternateEl):configType="ALT"
###########Grab optional Object Tags
if 'tagDefList' in spec: #change so it generates an empty one if no field defs
#print("spec.tagDefList = ",spec.tagDefList)
objTags = extractTagDefs(spec.tagDefList)
else: objTags = {}
taggedName = progSpec.addClass(ProgSpec, classNames, className, stateType, configType,description, comments)
progSpec.addObjTags(ProgSpec, className, stateType, objTags)
extractFieldDefs(ProgSpec, className, stateType, spec.fieldDefs)
############Grab optional typeArgList
if 'typeArgList' in spec[1]:
typeArgList = extractTypeArgList(spec[1].typeArgList)
progSpec.addTypeArgList(className, typeArgList)
return taggedName
def extractPatternSpecs(ProgSpec, classNames, spec):
patternName=spec.classSpec[0]
patternArgWords=spec.CIDList
progSpec.addPattern(ProgSpec, classNames, patternName, patternArgWords)
return
def extractMacroSpec(macroDefs, spec, comments):
MacroName=spec.macroName
if 'macroArgs' in spec: MacroArgs=spec.macroArgs
else: MacroArgs=[]
MacroBody=spec.macroBody[1]
macroDefs[MacroName] = {'ArgList':MacroArgs, 'Body':MacroBody}
def extractMacroDefs(macroDefMap, inputString):
macroDefs = re.findall('#define.*%>', inputString)
for macroStr in macroDefs:
try:
localResults = macroDef.parseString(macroStr, parseAll = True)
except ParseException as pe:
cdErr("Error Extracting Macro: {} In: {}".format(pe, macroStr))
exit(1)
extractMacroSpec(macroDefMap, localResults[0], ["//^", ""])
def isCID(ch):
return (ch.isalnum() or ch=='_')
def BlowPOPMacro(replacement):
updatedStr = ""
scanMode='identifier'
for ch in replacement:
if scanMode=='identifier':
if isCID(ch):
updatedStr += ch
else:
updatedStr += ' + "'+ch
scanMode='filler'
elif scanMode=='filler':
if not (ch.isalpha() or ch=='_'):
updatedStr += ch
else:
updatedStr += '" + '+ch
scanMode='identifier'
if scanMode=='filler':
updatedStr+='" '
return updatedStr
def deSlashMacro(replacement):
return replacement.replace('/', '_')
def findMacroEnd(inputString, StartPosOfParens):
nestLvl=0
if(inputString[StartPosOfParens] != '('): cdErr("NO PAREN!"); exit(2);
ISLen=len(inputString)
for pos in range(StartPosOfParens, ISLen):
ch = inputString[pos]
if ch=='(': nestLvl+=1
if ch==')': nestLvl-=1
if nestLvl==0:
#print("MACRO-ARGS:", inputString[StartPosOfParens:pos+1])
return pos+1
return -1
def doMacroSubstitutions(macros, inputString):
macros['BlowPOP'] = {'ArgList':['dummyArg'], 'Body':'dummyArg'}
macros['DESLASH'] = {'ArgList':['dummyArg'], 'Body':'dummyArg'}
subsWereMade=True
while(subsWereMade ==True):
subsWereMade=False
for thisMacro in macros:
macRefPattern=re.compile(r'(?<!#define)([^a-zA-Z0-9_]+)('+thisMacro+')(\\s*)\\(([^)]*)\\)')
#print("MACRO NAME:", thisMacro)
newString=''
currentPos=0
for match in macRefPattern.finditer(inputString):
#print(" %s: %s %s" % (match.start(), match.group(1), match.group(2)))
newText=macros[thisMacro]['Body']
#print(" START TEXT:", newText)
StartPosOfParens = match.start()+len(match.group(1)) + len(match.group(2)) + len(match.group(3))
EndPos=findMacroEnd(inputString, StartPosOfParens)
if EndPos==-1: print("\nERROR: Parentheses problem in macro", thisMacro, "\n"); exit(2);
paramStr=inputString[StartPosOfParens+1 : EndPos-1] #match.group(4)
params=paramStr.split(',')
#print(' PARAMS:', params)
idx=0;
numMacroArgs = len(macros[thisMacro]['ArgList'])
if((numMacroArgs>0 and numMacroArgs != len(params)) or (numMacroArgs==0 and len(params)!=1)):
cdErr("The macro {} has {} parameters, but is called with {}.".format(thisMacro, len(macros[thisMacro]['ArgList']), len(params)))
for arg in macros[thisMacro]['ArgList']:
#print(" SUBS:", arg, ', ', params[idx], ', ', thisMacro)
replacement=params[idx]
if thisMacro=='BlowPOP':
replacement=BlowPOPMacro(replacement)
elif thisMacro=='DESLASH':
replacement=deSlashMacro(replacement)
newText=newText.replace(arg, replacement)
idx+=1
#print(" NEW TEXT:", newText)
newString += inputString[currentPos:match.start()+len(match.group(1))]+ newText
currentPos=EndPos
subsWereMade=True
newString+=inputString[currentPos:]
inputString=newString
#print(" RETURN STRING:[", inputString, ']')
# Last, replace the text into inputString
return inputString
def extractObjectsOrPatterns(ProgSpec, clsNames, macroDefs, objectSpecResults,description):
newClassNames = []
comments = []
for spec in objectSpecResults:
s=spec[0]
if s == "model" or s == "struct" or s == "string" or s == "stream":
newName=extractObjectSpecs(ProgSpec, clsNames, spec, s,description, comments)
if newName!=None: newClassNames.append(newName)
comments = []
elif s == "do":
extractPatternSpecs(ProgSpec, clsNames, spec)
comments = []
elif s == "#define":
extractMacroSpec(macroDefs, spec, comments)
comments = []
elif s == '/*^' or s == '//^':
comments.append(spec)
else:
cdErr("Error in extractObjectsOrPatterns; expected 'object' or 'do' and got '{}'".format(spec[0]))
return newClassNames
# # # # # # # # # # # # # P a r s e r I n t e r f a c e # # # # # # # # # # # # #
def scanQuoteStr(text, idx, endChar):
size = len(text)
quoteStr = ""
while idx < size:
char = text[idx]
quoteStr += char
if char=="\\":
if idx+2 >=size: cdErr("Quote Not Ended at end of file: '"+quoteStr[:30]+" ...'")
nextChar = text[idx+1]
if nextChar==endChar or nextChar=="\\":
idx+=2
continue
elif char==endChar: return idx
if idx+1 >= size: cdErr("Quote Not Ended: '"+quoteStr[:30]+" ...'")
idx += 1
return idx
def comment_remover(textIn):
text = textIn
size = len(text)
commentType = None
idx = 0
while idx < size:
char = text[idx]
if char=='"' or char=="'":
idx = scanQuoteStr(text, idx+1, char)
elif char=="/":
if size <= idx: continue
nextChar = text[idx+1]
commentType = None
if nextChar=="/": commentType = "//"
elif nextChar=="*": commentType = "/*"
if commentType!=None:
keepComments = False
startPos = idx
replaceStr = ""
if idx+2 < size:
nextChar2 = text[idx+2]
if nextChar2==":" or nextChar2=="^":
keepComments = True
if not keepComments: replaceStr += " " #text = text[:idx] + " " + text[idx+2:]
cmtStr = ""
idx += 2
while idx < size:
char = text[idx]
if commentType=="//" and (char=="\n" or idx+1==size): commentType = None; break
elif commentType=="/*":
if idx+1 >= size: break
nextChar = char + text[idx+1]
if nextChar=="*/":
if not keepComments: replaceStr += " "
commentType = None
break
cmtStr += char
if not keepComments:
if text[idx]!="\n": replaceStr += " "
else: replaceStr += "\n"
idx += 1
if not keepComments:
endPos = startPos + len(replaceStr)
text = text[:startPos]+replaceStr+text[endPos:]
if commentType!=None: cdErr("Comment did not end:'"+cmtStr+"'")
idx += 1
return(text)
def parseCodeDogLibTags(inputString):
tmpMacroDefs={}
inputString = comment_remover(inputString)
extractMacroDefs(tmpMacroDefs, inputString)
inputString = doMacroSubstitutions(tmpMacroDefs, inputString)
progSpec.saveTextToErrFile(inputString)
try:
localResults = libTagParser.parseString(inputString, parseAll = False)
except ParseException as pe:
cdErr("While parsing lib tags: {}".format(pe))
tagStore = extractTagDefs(localResults.libTagParser.tagDefList)
return tagStore
def parseCodeDogString(inputString, ProgSpec, clsNames, macroDefs, description):
tmpMacroDefs={}
inputString = comment_remover(inputString)
extractMacroDefs(tmpMacroDefs, inputString)
inputString = doMacroSubstitutions(tmpMacroDefs, inputString)
LogLvl=logLvl()
cdlog(LogLvl, "PARSING: "+description+"...")
results = parseInput(inputString)
cdlog(LogLvl, "EXTRACTING: "+description+"...")
tagStore = extractTagDefs(results.progSpecParser.tagDefList)
buildSpecs = extractBuildSpecs(results.progSpecParser.buildSpecList)
newClassNames = extractObjectsOrPatterns(ProgSpec, clsNames, macroDefs, results.progSpecParser.classList,description)
classes = [ProgSpec, clsNames]
return[tagStore, buildSpecs, classes, newClassNames]
def AddToObjectFromText(ProgSpec, clsNames, inputStr, description):
macroDefs = {} # This var is not used here. If needed, make it an argument.
inputStr = comment_remover(inputStr)
#print('####################\n',inputStr, "\n######################^\n\n\n")
errLevl=logLvl(); cdlog(errLevl, 'Parsing: '+description)
progSpec.saveTextToErrFile(inputStr)
# (map of classes, array of objectNames, string to parse)
try:
results = classList.parseString(inputStr, parseAll = True)
except ParseException as pe:
cdErr("Error parsing generated class {}: {}".format(description, pe))
cdlog(errLevl, 'Completed parsing: '+description)
extractObjectsOrPatterns(ProgSpec, clsNames, macroDefs, results[0], description)