forked from aosp-mirror/platform_bionic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp.py
More file actions
2190 lines (1846 loc) · 69.8 KB
/
Copy pathcpp.py
File metadata and controls
2190 lines (1846 loc) · 69.8 KB
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# a glorified C pre-processor parser
import sys, re, string
from utils import *
from defaults import *
debugTokens = False
debugDirectiveTokenizer = False
debugLineParsing = False
debugCppExpr = False
debugOptimIf01 = False
#####################################################################################
#####################################################################################
##### #####
##### C P P T O K E N S #####
##### #####
#####################################################################################
#####################################################################################
# the list of supported C-preprocessor tokens
# plus a couple of C tokens as well
tokEOF = "\0"
tokLN = "\n"
tokSTRINGIFY = "#"
tokCONCAT = "##"
tokLOGICAND = "&&"
tokLOGICOR = "||"
tokSHL = "<<"
tokSHR = ">>"
tokEQUAL = "=="
tokNEQUAL = "!="
tokLT = "<"
tokLTE = "<="
tokGT = ">"
tokGTE = ">="
tokELLIPSIS = "..."
tokSPACE = " "
tokDEFINED = "defined"
tokLPAREN = "("
tokRPAREN = ")"
tokNOT = "!"
tokPLUS = "+"
tokMINUS = "-"
tokMULTIPLY = "*"
tokDIVIDE = "/"
tokMODULUS = "%"
tokBINAND = "&"
tokBINOR = "|"
tokBINXOR = "^"
tokCOMMA = ","
tokLBRACE = "{"
tokRBRACE = "}"
tokARROW = "->"
tokINCREMENT = "++"
tokDECREMENT = "--"
tokNUMBER = "<number>"
tokIDENT = "<ident>"
tokSTRING = "<string>"
class Token:
"""a simple class to hold information about a given token.
each token has a position in the source code, as well as
an 'id' and a 'value'. the id is a string that identifies
the token's class, while the value is the string of the
original token itself.
for example, the tokenizer concatenates a series of spaces
and tabs as a single tokSPACE id, whose value if the original
spaces+tabs sequence."""
def __init__(self):
self.id = None
self.value = None
self.lineno = 0
self.colno = 0
def set(self,id,val=None):
self.id = id
if val:
self.value = val
else:
self.value = id
return None
def copyFrom(self,src):
self.id = src.id
self.value = src.value
self.lineno = src.lineno
self.colno = src.colno
def __repr__(self):
if self.id == tokIDENT:
return "(ident %s)" % self.value
if self.id == tokNUMBER:
return "(number %s)" % self.value
if self.id == tokSTRING:
return "(string '%s')" % self.value
if self.id == tokLN:
return "<LN>"
if self.id == tokEOF:
return "<EOF>"
if self.id == tokSPACE and self.value == "\\":
# this corresponds to a trailing \ that was transformed into a tokSPACE
return "<\\>"
return self.id
def __str__(self):
if self.id == tokIDENT:
return self.value
if self.id == tokNUMBER:
return self.value
if self.id == tokSTRING:
return self.value
if self.id == tokEOF:
return "<EOF>"
if self.id == tokSPACE:
if self.value == "\\": # trailing \
return "\\\n"
else:
return self.value
return self.id
class BadExpectedToken(Exception):
def __init__(self,msg):
print msg
#####################################################################################
#####################################################################################
##### #####
##### C P P T O K E N C U R S O R #####
##### #####
#####################################################################################
#####################################################################################
class TokenCursor:
"""a small class to iterate over a list of Token objects"""
def __init__(self,tokens):
self.tokens = tokens
self.n = 0
self.count = len(tokens)
def set(self,n):
"""set the current position"""
if n < 0:
n = 0
if n > self.count:
n = self.count
self.n = n
def peekId(self):
"""retrieve the id of the current token"""
if (self.n >= self.count):
return None
return self.tokens[self.n].id
def peek(self):
"""retrieve the current token. does not change position"""
if (self.n >= self.count):
return None
return self.tokens[self.n]
def skip(self):
"""increase current token position"""
if (self.n < self.count):
self.n += 1
def skipSpaces(self):
"""skip over all space tokens, this includes tokSPACE and tokLN"""
while 1:
tok = self.peekId()
if tok != tokSPACE and tok != tokLN:
break
self.skip()
def skipIfId(self,id):
"""skip an optional token"""
if self.peekId() == id:
self.skip()
def expectId(self,id):
"""raise an exception if the current token hasn't a given id.
otherwise skip over it"""
tok = self.peek()
if tok.id != id:
raise BadExpectedToken, "%d:%d: '%s' expected, received '%s'" % (tok.lineno, tok.colno, id, tok.id)
self.skip()
def remain(self):
"""return the list of remaining tokens"""
return self.tokens[self.n:]
#####################################################################################
#####################################################################################
##### #####
##### C P P T O K E N I Z E R #####
##### #####
#####################################################################################
#####################################################################################
# list of long symbols, i.e. those that take more than one characters
cppLongSymbols = [ tokCONCAT, tokLOGICAND, tokLOGICOR, tokSHL, tokSHR, tokELLIPSIS, tokEQUAL,\
tokNEQUAL, tokLTE, tokGTE, tokARROW, tokINCREMENT, tokDECREMENT ]
class CppTokenizer:
"""an abstract class used to convert some input text into a list
of tokens. real implementations follow and differ in the format
of the input text only"""
def __init__(self):
"""initialize a new CppTokenizer object"""
self.eof = False # end of file reached ?
self.text = None # content of current line, with final \n stripped
self.line = 0 # number of current line
self.pos = 0 # current character position in current line
self.len = 0 # length of current line text
self.held = Token()
def setLineText(self,line):
"""set the content of the (next) current line. should be called
by fillLineText() in derived classes"""
self.text = line
self.len = len(line)
self.pos = 0
def fillLineText(self):
"""refresh the content of 'line' with a new line of input"""
# to be overriden
self.eof = True
def markPos(self,tok):
"""mark the position of the current token in the source file"""
if self.eof or self.pos > self.len:
tok.lineno = self.line + 1
tok.colno = 0
else:
tok.lineno = self.line
tok.colno = self.pos
def peekChar(self):
"""return the current token under the cursor without moving it"""
if self.eof:
return tokEOF
if self.pos > self.len:
self.pos = 0
self.line += 1
self.fillLineText()
if self.eof:
return tokEOF
if self.pos == self.len:
return tokLN
else:
return self.text[self.pos]
def peekNChar(self,n):
"""try to peek the next n chars on the same line"""
if self.pos + n > self.len:
return None
return self.text[self.pos:self.pos+n]
def skipChar(self):
"""increment the token cursor position"""
if not self.eof:
self.pos += 1
def skipNChars(self,n):
if self.pos + n <= self.len:
self.pos += n
else:
while n > 0:
self.skipChar()
n -= 1
def nextChar(self):
"""retrieve the token at the current cursor position, then skip it"""
result = self.peekChar()
self.skipChar()
return result
def getEscape(self):
# try to get all characters after a backslash (\)
result = self.nextChar()
if result == "0":
# octal number ?
num = self.peekNChar(3)
if num != None:
isOctal = True
for d in num:
if not d in "01234567":
isOctal = False
break
if isOctal:
result += num
self.skipNChars(3)
elif result == "x" or result == "X":
# hex number ?
num = self.peekNChar(2)
if num != None:
isHex = True
for d in num:
if not d in "012345678abcdefABCDEF":
isHex = False
break
if isHex:
result += num
self.skipNChars(2)
elif result == "u" or result == "U":
# unicode char ?
num = self.peekNChar(4)
if num != None:
isHex = True
for d in num:
if not d in "012345678abcdefABCDEF":
isHex = False
break
if isHex:
result += num
self.skipNChars(4)
return result
def nextRealToken(self,tok):
"""return next CPP token, used internally by nextToken()"""
c = self.nextChar()
if c == tokEOF or c == tokLN:
return tok.set(c)
if c == '/':
c = self.peekChar()
if c == '/': # C++ comment line
self.skipChar()
while 1:
c = self.nextChar()
if c == tokEOF or c == tokLN:
break
return tok.set(tokLN)
if c == '*': # C comment start
self.skipChar()
value = "/*"
prev_c = None
while 1:
c = self.nextChar()
if c == tokEOF:
#print "## EOF after '%s'" % value
return tok.set(tokEOF,value)
if c == '/' and prev_c == '*':
break
prev_c = c
value += c
value += "/"
#print "## COMMENT: '%s'" % value
return tok.set(tokSPACE,value)
c = '/'
if c.isspace():
while 1:
c2 = self.peekChar()
if c2 == tokLN or not c2.isspace():
break
c += c2
self.skipChar()
return tok.set(tokSPACE,c)
if c == '\\':
if debugTokens:
print "nextRealToken: \\ found, next token is '%s'" % repr(self.peekChar())
if self.peekChar() == tokLN: # trailing \
# eat the tokLN
self.skipChar()
# we replace a trailing \ by a tokSPACE whose value is
# simply "\\". this allows us to detect them later when
# needed.
return tok.set(tokSPACE,"\\")
else:
# treat as a single token here ?
c +=self.getEscape()
return tok.set(c)
if c == "'": # chars
c2 = self.nextChar()
c += c2
if c2 == '\\':
c += self.getEscape()
while 1:
c2 = self.nextChar()
if c2 == tokEOF:
break
c += c2
if c2 == "'":
break
return tok.set(tokSTRING, c)
if c == '"': # strings
quote = 0
while 1:
c2 = self.nextChar()
if c2 == tokEOF:
return tok.set(tokSTRING,c)
c += c2
if not quote:
if c2 == '"':
return tok.set(tokSTRING,c)
if c2 == "\\":
quote = 1
else:
quote = 0
if c >= "0" and c <= "9": # integers ?
while 1:
c2 = self.peekChar()
if c2 == tokLN or (not c2.isalnum() and c2 != "_"):
break
c += c2
self.skipChar()
return tok.set(tokNUMBER,c)
if c.isalnum() or c == "_": # identifiers ?
while 1:
c2 = self.peekChar()
if c2 == tokLN or (not c2.isalnum() and c2 != "_"):
break
c += c2
self.skipChar()
if c == tokDEFINED:
return tok.set(tokDEFINED)
else:
return tok.set(tokIDENT,c)
# check special symbols
for sk in cppLongSymbols:
if c == sk[0]:
sklen = len(sk[1:])
if self.pos + sklen <= self.len and \
self.text[self.pos:self.pos+sklen] == sk[1:]:
self.pos += sklen
return tok.set(sk)
return tok.set(c)
def nextToken(self,tok):
"""return the next token from the input text. this function
really updates 'tok', and does not return a new one"""
self.markPos(tok)
self.nextRealToken(tok)
def getToken(self):
tok = Token()
self.nextToken(tok)
if debugTokens:
print "getTokens: %s" % repr(tok)
return tok
def toTokenList(self):
"""convert the input text of a CppTokenizer into a direct
list of token objects. tokEOF is stripped from the result"""
result = []
while 1:
tok = Token()
self.nextToken(tok)
if tok.id == tokEOF:
break
result.append(tok)
return result
class CppLineTokenizer(CppTokenizer):
"""a CppTokenizer derived class that accepts a single line of text as input"""
def __init__(self,line,lineno=1):
CppTokenizer.__init__(self)
self.line = lineno
self.setLineText(line)
class CppLinesTokenizer(CppTokenizer):
"""a CppTokenizer derived class that accepts a list of texdt lines as input.
the lines must not have a trailing \n"""
def __init__(self,lines=[],lineno=1):
"""initialize a CppLinesTokenizer. you can later add lines using addLines()"""
CppTokenizer.__init__(self)
self.line = lineno
self.lines = lines
self.index = 0
self.count = len(lines)
if self.count > 0:
self.fillLineText()
else:
self.eof = True
def addLine(self,line):
"""add a line to a CppLinesTokenizer. this can be done after tokenization
happens"""
if self.count == 0:
self.setLineText(line)
self.index = 1
self.lines.append(line)
self.count += 1
self.eof = False
def fillLineText(self):
if self.index < self.count:
self.setLineText(self.lines[self.index])
self.index += 1
else:
self.eof = True
class CppFileTokenizer(CppTokenizer):
def __init__(self,file,lineno=1):
CppTokenizer.__init__(self)
self.file = file
self.line = lineno
def fillLineText(self):
line = self.file.readline()
if len(line) > 0:
if line[-1] == '\n':
line = line[:-1]
if len(line) > 0 and line[-1] == "\r":
line = line[:-1]
self.setLineText(line)
else:
self.eof = True
# Unit testing
#
class CppTokenizerTester:
"""a class used to test CppTokenizer classes"""
def __init__(self,tokenizer=None):
self.tokenizer = tokenizer
self.token = Token()
def setTokenizer(self,tokenizer):
self.tokenizer = tokenizer
def expect(self,id):
self.tokenizer.nextToken(self.token)
tokid = self.token.id
if tokid == id:
return
if self.token.value == id and (tokid == tokIDENT or tokid == tokNUMBER):
return
raise BadExpectedToken, "### BAD TOKEN: '%s' expecting '%s'" % (self.token.id,id)
def expectToken(self,id,line,col):
self.expect(id)
if self.token.lineno != line:
raise BadExpectedToken, "### BAD LINENO: token '%s' got '%d' expecting '%d'" % (id,self.token.lineno,line)
if self.token.colno != col:
raise BadExpectedToken, "### BAD COLNO: '%d' expecting '%d'" % (self.token.colno,col)
def expectTokenVal(self,id,value,line,col):
self.expectToken(id,line,col)
if self.token.value != value:
raise BadExpectedToken, "### BAD VALUE: '%s' expecting '%s'" % (self.token.value,value)
def expectList(self,list):
for item in list:
self.expect(item)
def test_CppTokenizer():
print "running CppTokenizer tests"
tester = CppTokenizerTester()
tester.setTokenizer( CppLineTokenizer("#an/example && (01923_xy)") )
tester.expectList( ["#", "an", "/", "example", tokSPACE, tokLOGICAND, tokSPACE, tokLPAREN, "01923_xy", \
tokRPAREN, tokLN, tokEOF] )
tester.setTokenizer( CppLineTokenizer("FOO(BAR) && defined(BAZ)") )
tester.expectList( ["FOO", tokLPAREN, "BAR", tokRPAREN, tokSPACE, tokLOGICAND, tokSPACE,
tokDEFINED, tokLPAREN, "BAZ", tokRPAREN, tokLN, tokEOF] )
tester.setTokenizer( CppLinesTokenizer( ["/*", "#", "*/"] ) )
tester.expectList( [ tokSPACE, tokLN, tokEOF ] )
tester.setTokenizer( CppLinesTokenizer( ["first", "second"] ) )
tester.expectList( [ "first", tokLN, "second", tokLN, tokEOF ] )
tester.setTokenizer( CppLinesTokenizer( ["first second", " third"] ) )
tester.expectToken( "first", 1, 0 )
tester.expectToken( tokSPACE, 1, 5 )
tester.expectToken( "second", 1, 6 )
tester.expectToken( tokLN, 1, 12 )
tester.expectToken( tokSPACE, 2, 0 )
tester.expectToken( "third", 2, 2 )
tester.setTokenizer( CppLinesTokenizer( [ "boo /* what the", "hell */" ] ) )
tester.expectList( [ "boo", tokSPACE ] )
tester.expectTokenVal( tokSPACE, "/* what the\nhell */", 1, 4 )
tester.expectList( [ tokLN, tokEOF ] )
tester.setTokenizer( CppLinesTokenizer( [ "an \\", " example" ] ) )
tester.expectToken( "an", 1, 0 )
tester.expectToken( tokSPACE, 1, 2 )
tester.expectTokenVal( tokSPACE, "\\", 1, 3 )
tester.expectToken( tokSPACE, 2, 0 )
tester.expectToken( "example", 2, 1 )
tester.expectToken( tokLN, 2, 8 )
return True
#####################################################################################
#####################################################################################
##### #####
##### C P P E X P R E S S I O N S #####
##### #####
#####################################################################################
#####################################################################################
# Cpp expressions are modeled by tuples of the form (op,arg) or (op,arg1,arg2), etc..
# op is an "operator" string
class Expr:
"""a class used to model a CPP expression"""
opInteger = "int"
opIdent = "ident"
opCall = "call"
opDefined = "defined"
opTest = "?"
opLogicNot = "!"
opNot = "~"
opNeg = "[-]"
opUnaryPlus = "[+]"
opAdd = "+"
opSub = "-"
opMul = "*"
opDiv = "/"
opMod = "%"
opAnd = "&"
opOr = "|"
opXor = "^"
opLogicAnd = "&&"
opLogicOr = "||"
opEqual = "=="
opNotEqual = "!="
opLess = "<"
opLessEq = "<="
opGreater = ">"
opGreaterEq = ">="
opShl = "<<"
opShr = ">>"
unaries = [ opLogicNot, opNot, opNeg, opUnaryPlus ]
binaries = [ opAdd, opSub, opMul, opDiv, opMod, opAnd, opOr, opXor, opLogicAnd, opLogicOr,
opEqual, opNotEqual, opLess, opLessEq, opGreater, opGreaterEq ]
precedences = {
opTest: 0,
opLogicOr: 1,
opLogicNot: 2,
opOr : 3,
opXor: 4,
opAnd: 5,
opEqual: 6, opNotEqual: 6,
opLess:7, opLessEq:7, opGreater:7, opGreaterEq:7,
opShl:8, opShr:8,
opAdd:9, opSub:9,
opMul:10, opDiv:10, opMod:10,
opLogicNot:11,
opNot: 12,
}
def __init__(self,op):
self.op = op
def __repr__(self):
return "(%s)" % self.op
def __str__(self):
return "operator(%s)" % self.op
def precedence(self):
"""return the precedence of a given operator"""
return Expr.precedences.get(self.op, 1000)
def isUnary(self):
return self.op in Expr.unaries
def isBinary(self):
return self.op in Expr.binaries
def isDefined(self):
return self.op is opDefined
def toInt(self):
"""return the integer value of a given expression. only valid for integer expressions
will return None otherwise"""
return None
class IntExpr(Expr):
def __init__(self,value):
Expr.__init__(self,opInteger)
self.arg = value
def __repr__(self):
return "(int %s)" % self.arg
def __str__(self):
return self.arg
def toInt(self):
s = self.arg # string value
# get rid of U or L suffixes
while len(s) > 0 and s[-1] in "LUlu":
s = s[:-1]
return string.atoi(s)
class IdentExpr(Expr):
def __init__(self,name):
Expr.__init__(self,opIdent)
self.name = name
def __repr__(self):
return "(ident %s)" % self.name
def __str__(self):
return self.name
class CallExpr(Expr):
def __init__(self,funcname,params):
Expr.__init__(self,opCall)
self.funcname = funcname
self.params = params
def __repr__(self):
result = "(call %s [" % self.funcname
comma = ""
for param in self.params:
result += "%s%s" % (comma, repr(param))
comma = ","
result += "])"
return result
def __str__(self):
result = "%s(" % self.funcname
comma = ""
for param in self.params:
result += "%s%s" % (comma, str(param))
comma = ","
result += ")"
return result
class TestExpr(Expr):
def __init__(self,cond,iftrue,iffalse):
Expr.__init__(self,opTest)
self.cond = cond
self.iftrue = iftrue
self.iffalse = iffalse
def __repr__(self):
return "(?: %s %s %s)" % (repr(self.cond),repr(self.iftrue),repr(self.iffalse))
def __str__(self):
return "(%s) ? (%s) : (%s)" % (self.cond, self.iftrue, self.iffalse)
class SingleArgExpr(Expr):
def __init__(self,op,arg):
Expr.__init__(self,op)
self.arg = arg
def __repr__(self):
return "(%s %s)" % (self.op, repr(self.arg))
class DefinedExpr(SingleArgExpr):
def __init__(self,op,macroname):
SingleArgExpr.__init__(self.opDefined,macroname)
def __str__(self):
return "defined(%s)" % self.arg
class UnaryExpr(SingleArgExpr):
def __init__(self,op,arg,opstr=None):
SingleArgExpr.__init__(self,op,arg)
if not opstr:
opstr = op
self.opstr = opstr
def __str__(self):
arg_s = str(self.arg)
arg_prec = self.arg.precedence()
self_prec = self.precedence()
if arg_prec < self_prec:
return "%s(%s)" % (self.opstr,arg_s)
else:
return "%s%s" % (self.opstr, arg_s)
class TwoArgExpr(Expr):
def __init__(self,op,arg1,arg2):
Expr.__init__(self,op)
self.arg1 = arg1
self.arg2 = arg2
def __repr__(self):
return "(%s %s %s)" % (self.op, repr(self.arg1), repr(self.arg2))
class BinaryExpr(TwoArgExpr):
def __init__(self,op,arg1,arg2,opstr=None):
TwoArgExpr.__init__(self,op,arg1,arg2)
if not opstr:
opstr = op
self.opstr = opstr
def __str__(self):
arg1_s = str(self.arg1)
arg2_s = str(self.arg2)
arg1_prec = self.arg1.precedence()
arg2_prec = self.arg2.precedence()
self_prec = self.precedence()
result = ""
if arg1_prec < self_prec:
result += "(%s)" % arg1_s
else:
result += arg1_s
result += " %s " % self.opstr
if arg2_prec < self_prec:
result += "(%s)" % arg2_s
else:
result += arg2_s
return result
#####################################################################################
#####################################################################################
##### #####
##### C P P E X P R E S S I O N P A R S E R #####
##### #####
#####################################################################################
#####################################################################################
class ExprParser:
"""a class used to convert a list of tokens into a cpp Expr object"""
re_octal = re.compile(r"\s*\(0[0-7]+\).*")
re_decimal = re.compile(r"\s*\(\d+[ulUL]*\).*")
re_hexadecimal = re.compile(r"\s*\(0[xX][0-9a-fA-F]*\).*")
def __init__(self,tokens):
self.tok = tokens
self.n = len(self.tok)
self.i = 0
def mark(self):
return self.i
def release(self,pos):
self.i = pos
def peekId(self):
if self.i < self.n:
return self.tok[self.i].id
return None
def peek(self):
if self.i < self.n:
return self.tok[self.i]
return None
def skip(self):
if self.i < self.n:
self.i += 1
def skipOptional(self,id):
if self.i < self.n and self.tok[self.i].id == id:
self.i += 1
def skipSpaces(self):
i = self.i
n = self.n
tok = self.tok
while i < n and (tok[i] == tokSPACE or tok[i] == tokLN):
i += 1
self.i = i
# all the isXXX functions returns a (expr,nextpos) pair if a match is found
# or None if not
def is_integer(self):
id = self.tok[self.i].id
c = id[0]
if c < '0' or c > '9':
return None
m = ExprParser.re_octal.match(id)
if m:
return (IntExpr(id), m.end(1))
m = ExprParser.re_decimal.match(id)
if m:
return (IntExpr(id), m.end(1))
m = ExprParser.re_hexadecimal(id)
if m:
return (IntExpr(id), m.end(1))
return None
def is_defined(self):
id = self.tok[self.i].id
if id != "defined":
return None
pos = self.mark()
use_paren = 0
if self.peekId() == tokLPAREN:
self.skip()
use_paren = 1
if self.peekId() != tokIDENT:
self.throw( BadExpectedToken, "identifier expected")
macroname = self.peek().value
self.skip()
if use_paren:
self.skipSpaces()
if self.peekId() != tokRPAREN:
self.throw( BadExpectedToken, "missing right-paren after 'defined' directive")
self.skip()
i = self.i
return (DefinedExpr(macroname),i+1)
def is_call_or_ident(self):
pass
def parse(self, i):
return None
#####################################################################################
#####################################################################################
##### #####
##### C P P E X P R E S S I O N S #####
##### #####
#####################################################################################
#####################################################################################
class CppInvalidExpression(Exception):
"""an exception raised when an invalid/unsupported cpp expression is detected"""
pass
class CppExpr:
"""a class that models the condition of #if directives into
an expression tree. each node in the tree is of the form (op,arg) or (op,arg1,arg2)
where "op" is a string describing the operation"""
unaries = [ "!", "~" ]
binaries = [ "+", "-", "<", "<=", ">=", ">", "&&", "||", "*", "/", "%", "&", "|", "^", "<<", ">>", "==", "!=" ]
precedences = { "||": 1,
"&&": 2,
"|": 3,
"^": 4,
"&": 5,
"==":6, "!=":6,
"<":7, "<=":7, ">":7, ">=":7,
"<<":8, ">>":8,
"+":9, "-":9,
"*":10, "/":10, "%":10,
"!":11, "~":12
}
def __init__(self, tokens):
"""initialize a CppExpr. 'tokens' must be a CppToken list"""
self.tok = tokens
self.n = len(tokens)
if debugCppExpr:
print "CppExpr: trying to parse %s" % repr(tokens)
expr = self.is_expr(0)
if debugCppExpr:
print "CppExpr: got " + repr(expr)
self.expr = expr[0]
re_cpp_constant = re.compile(r"((\d|\w|_)+)")
def throw(self,exception,i,msg):
if i < self.n:
tok = self.tok[i]
print "%d:%d: %s" % (tok.lineno,tok.colno,msg)
else:
print "EOF: %s" % msg
raise exception
def skip_spaces(self,i):
"""skip spaces in input token list"""
while i < self.n:
t = self.tok[i]