-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartial_pp.py
More file actions
768 lines (578 loc) · 22.1 KB
/
Copy pathpartial_pp.py
File metadata and controls
768 lines (578 loc) · 22.1 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
"""Partially preprocess a C/C++ source file, only acting on certain preprocessor
definitions.
"""
import re
import string
def build_defines_map(defines):
"""Take a list of defines, and build it into a map."""
defines_map = {}
for define in defines:
if isinstance(define, tuple):
(symbol, definition) = define
defines_map[symbol] = definition
else:
assert isinstance(define, str)
defines_map[define] = ''
return defines_map
class Token:
"""A single preprocessor token."""
UNKNOWN = 0
IDENTIFIER = 1
NUMERIC_LITERAL = 2
LPAREN = 3
RPAREN = 4
DEFINED = 5
TRUE = 7
FALSE = 8
BITWISE_AND = 9
LOGICAL_AND = 10
BITWISE_OR = 11
LOGICAL_OR = 12
LOGICAL_NOT = 13
NEQ = 14
EQ = 15
BINARY_OPERATORS = {
BITWISE_AND,
LOGICAL_AND,
BITWISE_OR,
LOGICAL_OR,
NEQ,
EQ,
}
UNARY_OPERATORS = {
LOGICAL_NOT
}
OPERATOR_PRECEDENCES = {
LOGICAL_NOT: 3,
EQ: 9,
NEQ: 9,
BITWISE_AND: 10,
BITWISE_OR: 12,
LOGICAL_AND: 13,
LOGICAL_OR: 14,
}
def __init__(self, kind, val, start, end):
self.kind = kind
self.val = val
self.start = start
self.end = end
def __repr__(self):
return '({}, {})'.format(self.kind, self.val)
def to_tuple(self):
"""Convert to a tuple of (kind, val)"""
return (self.kind, self.val)
def get_leading_whitespace(self):
"""Return the leading whitespace, or an empty string if none."""
m = re.match('^\s+', self.val)
if m is None:
return ''
return m.group(0)
def is_binary_operator(self):
"""Return if this is a binary operator."""
return self.kind in Token.BINARY_OPERATORS
def is_unary_operator(self):
"""Return if this is a unary operator."""
return self.kind in Token.UNARY_OPERATORS
def get_operator_precedence(self):
"""Return this operator's precedence.
If this is not an operator, return None.
"""
if self.kind in Token.OPERATOR_PRECEDENCES:
return Token.OPERATOR_PRECEDENCES[self.kind]
return None
class TokenizerError(Exception):
def __init__(self, msg, pos):
self.msg = msg
self.pos = pos
def __repr__(self):
return 'Syntax error (pos = {}): {}'.format(pos, msg)
class Tokenizer:
"""Tokenize preprocessor expressions."""
def __init__(self, expr):
self.expr = expr
self.length = len(expr)
self.pos = 0
def __iter__(self):
return self
def consume_whitespace(self):
"""Advance pos until it's not pointing at whitespace."""
if self.pos >= self.length:
return
if self.expr[self.pos] not in string.whitespace:
return
begin = self.pos
while self.pos < self.length:
if self.expr[self.pos] not in string.whitespace:
return
self.pos += 1
# These are single-character tokens that are not the prefix of any other
# token.
SINGLE_CHAR_TOKS = {'(': Token.LPAREN,
')': Token.RPAREN,
}
# These are operators that can either be one or two characters.
SINGLE_OR_DOUBLE_CHAR_TOKS = {
'&': (Token.BITWISE_AND, Token.LOGICAL_AND),
'|': (Token.BITWISE_OR, Token.LOGICAL_OR),
}
def try_lex_operator(self):
"""Tokenize operators or parentheses if they're present.
Return the token if present and update pos, otherwise update nothing and
return None.
"""
if self.expr[self.pos] in self.SINGLE_CHAR_TOKS:
tok = self.expr[self.pos]
self.pos += 1
return (self.SINGLE_CHAR_TOKS[tok], tok)
if self.expr[self.pos] in self.SINGLE_OR_DOUBLE_CHAR_TOKS:
tok = self.expr[self.pos]
toklen = 1
self.pos += 1
if self.pos < self.length:
if self.expr[self.pos] == tok:
self.pos += 1
toklen += 1
return (self.SINGLE_OR_DOUBLE_CHAR_TOKS[tok][toklen-1], tok+tok)
if self.expr[self.pos] == '=':
self.pos += 1
if self.pos < self.length:
if self.expr[self.pos] == '=':
self.pos += 1
return (Token.EQ, '==')
raise TokenizerError(self.pos, "Expected '='.")
if self.expr[self.pos] == '!':
self.pos += 1
if self.pos < self.length:
if self.expr[self.pos] == '=':
self.pos += 1
return (Token.NEQ, '!=')
return (Token.LOGICAL_NOT, '!')
return None
def try_lex_needle(self, needle, tok_type):
"""Look for the given string, and tokenize it if present.
If present, return the token and update pos, otherwise update nothing
and return None.
"""
needle_len = len(needle)
if self.pos + needle_len > self.length:
return None
if self.expr[self.pos:self.pos+needle_len] == needle:
self.pos += len(needle)
return (tok_type, needle)
return None
def try_lex_defined(self):
"""Tokenize the 'defined' token if present.
If present, return the token and update pos, otherwise update nothing
and return None.
"""
return self.try_lex_needle('defined', Token.DEFINED)
IDENTIFIER_BEGIN_CHARS = '_' + string.ascii_letters
IDENTIFIER_NEXT_CHARS = '_' + string.ascii_letters + string.digits
def try_lex_identifier(self):
"""Tokenize an identifier if present.
If present, return the token and update pos, otherwise update nothing
and return None.
"""
if self.expr[self.pos] not in self.IDENTIFIER_BEGIN_CHARS:
return None
begin = self.pos
self.pos += 1
while self.pos < self.length:
if self.expr[self.pos] not in self.IDENTIFIER_NEXT_CHARS:
break
self.pos += 1
return (Token.IDENTIFIER, self.expr[begin:self.pos])
def try_lex_numeric_literal(self):
"""Tokenize a numeric literal if present.
If present, return the token and update pos, otherwise update nothing
and return None.
"""
if self.expr[self.pos] not in string.digits:
return None
begin = self.pos
self.pos += 1
while self.pos < self.length:
if self.expr[self.pos] not in string.digits:
break
self.pos += 1
return (Token.NUMERIC_LITERAL, self.expr[begin:self.pos])
def try_lex_true_false(self):
"""Tokenize true/false if present.
If present, return the token and update pos, otherwise update nothing
and return None.
"""
tok = self.try_lex_needle('true', Token.TRUE)
if tok is not None:
return tok
tok = self.try_lex_needle('false', Token.FALSE)
if tok is not None:
return tok
return None
def get_next_internal(self):
"""Return the next token. If at the end of the stream, return None.
Tokens are returned as a pair (type, str) where type is the type of
token (see the constants above), and str is the actual token as a
string.
"""
if self.pos >= self.length:
return None
tok = self.try_lex_operator()
if tok is not None:
return tok
tok = self.try_lex_defined()
if tok is not None:
return tok
# This has to happen before trying to parse an identifier, because
# true/false both could be interpreted as identifiers otherwise.
tok = self.try_lex_true_false()
if tok is not None:
return tok
tok = self.try_lex_identifier()
if tok is not None:
return tok
tok = self.try_lex_numeric_literal()
if tok is not None:
return tok
# If we get here, the token is unknown. Just eat the single character.
tok = (Token.UNKNOWN, self.expr[self.pos])
self.pos += 1
return tok
def get_next(self):
"""Return the next token. If at the end of the stream, return None.
Tokens are returned as Token objects.
"""
# Eat whitespace first. It'll get tacked onto the beginning of
# whatever token is parsed.
start = self.pos
self.consume_whitespace()
tok = self.get_next_internal()
if tok is None:
return None
return Token(tok[0], self.expr[start:self.pos], start, self.pos)
def next(self):
"""Present an iterator interface.
See get_next() for what is returned. The only difference is, this raises
StopIteration instead of returning None when at the end of the stream.
"""
tok = self.get_next()
if tok is None:
raise StopIteration()
return tok
class Simplifier:
"""Simplify preprocessor conditionals as much as possible."""
def __init__(self, defines, undefines):
self.defines = build_defines_map(defines)
self.undefines = set(undefines)
def try_convert_to_boolean(self, expr):
"""Cast a value to a boolean if possible.
If the given expression is not simplified down to a single value, return
the expression itself. If it is, return True or False.
"""
if expr == 'true':
return True
if expr == 'false':
return False
try:
return int(expr) != 0
except ValueError:
return expr
def simplify(self, expr):
"""Simplify the given expression, substituting in all defines/undefines.
If the expression can be shown to be true or false, given the set of
defines and undefines, return True or False. Otherwise, simplify the
expression as much as possible, and return it as a string.
"""
self.tokenizer = Tokenizer(expr)
self.expr = expr
self.cur_tok = self.tokenizer.get_next()
return self.try_convert_to_boolean(self.simplify_expr())
def get_next_tok(self):
"""Consume the current token and return the next one."""
self.cur_tok = self.tokenizer.get_next()
return self.cur_tok
def expect_and_consume(self, kind):
"""Consume and return the given token kind.
If the token kind is not present, raise an error.
"""
tok = self.cur_tok
self.get_next_tok()
if tok.kind != kind:
raise TokenizerError(tok.start,
'Expected token kind {}.'.format(kind))
return tok
def simplify_expr(self):
"""Simplify a full expression using operator precedence parsing.
This being parsing the expression by parsing the leftmost
primary expression.
"""
return self.simplify_expr_op(self.simplify_primary(), 0)
def simplify_binop(self, op, lhs, rhs):
"""Try to simplify the expression lhs op rhs."""
lhs = self.try_convert_to_boolean(lhs)
rhs = self.try_convert_to_boolean(rhs)
lhs_bool = isinstance(lhs, bool)
rhs_bool = isinstance(rhs, bool)
both_bool = lhs_bool and rhs_bool
# If exactly one argument's value is known, make it so that one is lhs,
# to simplify the code
if rhs_bool and not lhs_bool:
(rhs, lhs) = (lhs, rhs)
(rhs_bool, lhs_bool) = (lhs_bool, rhs_bool)
if op.kind == Token.LOGICAL_AND:
if both_bool:
return lhs and rhs
if lhs_bool:
if lhs_val:
return lhs.get_leading_whitespace() + rhs
else:
return op.get_leading_whitespace() + 'false'
if op.kind == Token.LOGICAL_OR:
if both_bool:
return lhs or rhs
if lhs_bool:
if lhs_val:
return op.get_leading_whitespace() + 'true'
else:
return rhs
# We couldn't simplify anything.
return lhs + ' ' + op + ' ' + rhs
def simplify_expr_op(self, lhs, min_precedence):
"""Perform the actual operator precedence parsing and simplification.
"""
while self.cur_tok is not None:
if not self.cur_tok.is_binary_operator():
break;
if self.cur_tok.get_operator_precedence() < min_precedence:
break
op = self.cur_tok
op_prec = op.get_operator_precedence()
self.get_next_tok()
rhs = self.simplify_primary()
while self.cur_tok is not None:
if self.cur_tok.get_operator_precedence() is None:
break
prec = self.cur_tok.get_operator_precedence()
if self.cur_tok.is_binary_operator() and prec <= op_prec:
break
lookahread = self.cur_tok
lookahead_prec = lookahead.get_operator_precedence()
self.get_next_tok()
rhs = self.simplify_expr_op(rhs, lookahead_prec)
# Here we try to simplify LHS op RHS and store it in LHS.
lhs = self.simplify_binop(op, lhs, rhs)
return lhs
ALREADY_SIMPLIFIED_KINDS = [Token.NUMERIC_LITERAL,
Token.TRUE,
Token.FALSE]
def simplify_primary(self):
"""Simplify a primary expression.
primary ::= '(' expression ')'
| IDENTIFIER
| NUMERIC_LITERAL
| 'defined' '(' IDENTIFIER ')'
| 'defined' IDENTIFIER
"""
if self.cur_tok.kind in self.ALREADY_SIMPLIFIED_KINDS:
tok = self.cur_tok
self.get_next_tok()
return tok.val
if self.cur_tok.kind == Token.DEFINED:
first_tok = self.cur_tok
self.get_next_tok()
parens = False
if self.cur_tok.kind == Token.LPAREN:
parens = True
self.get_next_tok()
ident = self.expect_and_consume(Token.IDENTIFIER)
last_tok = ident
if parens:
last_tok = self.expect_and_consume(Token.RPAREN)
value = None
pp_def = ident.val.lstrip()
if pp_def in self.defines:
value = True
elif pp_def in self.undefines:
value = False
if value is None:
return self.expr[first_tok.start:last_tok.end+1]
if value:
return first_tok.get_leading_whitespace() + '1'
else:
return first_tok.get_leading_whitespace() + '0'
if self.cur_tok.kind == Token.IDENTIFIER:
tok = self.cur_tok
self.get_next_tok()
pp_def = tok.val.lstrip()
value = None
if pp_def in self.defines:
return self.defines[pp_def]
return tok.val
tok = self.cur_tok
self.get_next_tok()
raise TokenizerError(tok.start, 'Unexpected token kind {}.'.format(tok.kind))
class Parser:
"""Parse and preprocess source code."""
def __init__(self, source, defines, undefines):
self.source = source
self.lines = source.splitlines()
self.cur_line = 0
self.defines = build_defines_map(defines)
self.undefines = set(undefines)
self.ifdef_matcher = re.compile('^#ifdef (.+)')
self.endif_matcher = re.compile('^#endif')
self.if_matcher = re.compile('^#if (.+)')
self.simplifier = Simplifier(defines, undefines)
def lookup_define(self, sym):
"""If the given preprocessor symbol is defined, return its value.
Symbols that are defined but not given a value will yield the empty
string. Return None for symbols that are not defined.
"""
if sym in self.defines:
return self.defines[sym]
else:
return None
def is_undefined(self, sym):
"""Return whether the given preprocessor symbol is undefined."""
return sym in self.undefines
def is_at_end(self):
"""Return whether all the lines have been parsed."""
return self.cur_line >= len(self.lines)
def consume_cur_line(self):
"""Read a single line of input and move onto the next."""
ret = self.lines[self.cur_line]
self.cur_line += 1
return ret
def get_cur_line(self):
"""Return the current line. Don't move onto the next."""
return self.lines[self.cur_line]
def consume_until_match(self, matcher):
"""Consume lines until the regexp is matched, or eof is hit.
Return the match result if it's eventually hit, or None if eof is hit.
"""
while not self.is_at_end():
m = matcher.match(self.get_cur_line())
if m is not None:
return m
self.consume_cur_line()
return None
def process(self):
"""Process the passed-in source file and return it as a list of lines.
"""
output = []
while not self.is_at_end():
out_lines = self.parse_line()
if out_lines is not None:
output.extend(out_lines)
return output
def parse_line(self):
"""Return parsed output, as a list of lines.
Consumes all the input that is used.
If there is no output, return None.
"""
if self.is_at_end():
return None
m = self.ifdef_matcher.match(self.get_cur_line())
if m is not None:
return self.parse_ifdef(m.group(1))
m = self.if_matcher.match(self.get_cur_line())
if m is not None:
return self.parse_if(m.group(1))
return [self.consume_cur_line()]
def parse_ifdef(self, sym):
"""Parse an #ifdef statement, where sym is the preprocessor symbol.
If it's not the list of defines, the enclosed code is returned. If it
is, None is returned. This must be called with the #ifdef as the current
line, and will parse up to and including the #endif.
"""
# Eat the ifdef
ifdef_line = self.cur_line
self.consume_cur_line()
self.consume_until_match(self.endif_matcher)
assert not self.is_at_end()
# Eat the endif
endif_line = self.cur_line
self.consume_cur_line()
if self.lookup_define(sym) is not None:
# If the symbol is defined, then keep the enclosed lines, but get
# rid of the ifdef.
first_line = ifdef_line + 1
end_line = endif_line
elif self.is_undefined(sym):
# If the symbol is undefined, remove the whole block.
return None
else:
first_line = ifdef_line
end_line = endif_line + 1
return self.lines[first_line:end_line]
def parse_if(self, expr):
"""Parse an #if statement, where expr is the conditional.
Tries to simplify the expression using the defined/undefined symbols. If
the expression can be fully simplified, the #if/#endif will be removed,
and the source will remain only if the condition is true. Otherwise, the
simplified expression will be emitted in place of the original one.
"""
# Eat the if
if_line = self.cur_line
self.consume_cur_line()
self.consume_until_match(self.endif_matcher)
assert not self.is_at_end()
# Eat the endif
endif_line = self.cur_line
self.consume_cur_line()
simplified = self.simplifier.simplify(expr)
# If the condition is known to be true, then keep the enclosed
# lines, but get rid of the if.
if simplified is True:
first_line = if_line + 1
end_line = endif_line
return self.lines[first_line:end_line]
# If the condition is known to be false, remove the whole block.
if simplified is False:
return None
# Otherwise, the condition's value isn't known, but it might have been
# simplified. Replace the expression with the simplified one.
new_line = self.lines[if_line].replace(expr, simplified)
return [new_line] + self.lines[if_line+1:endif_line+1]
def process(source, defines, undefines):
"""Preprocess source, only using definitions from defines.
defines is a list, where each element is either a string, indicating
that preprocessor symbol should be defined, or a tuple of two strings,
indicating that the former is defined to be the latter.
"""
parser = Parser(source, defines, undefines)
output_lines = parser.process()
return '\n'.join(output_lines)
class InvalidCommandLineArgError(Exception):
def __init__(self, arg):
self.arg = arg
def __str__(self):
return self.arg
def parse_args(args):
"""Extract preprocessor definitions from the command line.
Each element of args should either be of the form -DFOO or -DFOO=BAR for
defines, and -UFOO for undefines.
The return value is (defines, undefines).
The defines are a list, where each element os either a string, or a tuple
of two strings. The undefines are a list of strings.
"""
eq_matcher = re.compile('^\-D(.+)$')
defn_matcher = re.compile('^\-D(.+)=(.+)$')
undef_matcher = re.compile('^\-U(.+)$')
defines = []
undefines = []
for arg in args:
m = defn_matcher.match(arg)
if m is not None:
defines.append((m.group(1), m.group(2)))
continue
m = eq_matcher.match(arg)
if m is not None:
defines.append(m.group(1))
continue
m = undef_matcher.match(arg)
if m is not None:
if '=' not in m.group(1):
undefines.append(m.group(1))
continue
raise InvalidCommandLineArgError(arg)
return (defines, undefines)