-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_lexer.py
More file actions
803 lines (599 loc) · 21 KB
/
cpp_lexer.py
File metadata and controls
803 lines (599 loc) · 21 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
from __future__ import annotations
from collections.abc import Iterable
from dataclasses import dataclass
from cpp_index_model import Token
from cpp_index_utils import normalize_signature_spacing
# ---------------------------------------------------------------------------
# Stream helpers
# ---------------------------------------------------------------------------
def _is_identifier_start(ch: str) -> bool:
return ch == "_" or ch.isalpha()
def _is_identifier_continue(ch: str) -> bool:
return ch == "_" or ch.isalnum()
def _try_raw_string_start(line: str, index: int) -> tuple[str, int] | None:
# C++ raw string literal:
# R"delimiter(raw text)delimiter"
#
# Supported encoding prefixes:
# R"..."
# LR"..."
# u8R"..."
# uR"..."
# UR"..."
#
# delimiter is at most 16 chars and cannot contain whitespace, backslash,
# parentheses, or quotes. This function returns (terminator, after_open).
prefixes = ("u8R", "LR", "uR", "UR", "R")
matched_prefix = None
for prefix in prefixes:
if line.startswith(prefix + '"', index):
matched_prefix = prefix
break
if matched_prefix is None:
return None
j = index + len(matched_prefix) + 1
delimiter_chars: list[str] = []
while j < len(line):
ch = line[j]
if ch == "(":
delimiter = "".join(delimiter_chars)
return f"){delimiter}\"", j + 1
if ch.isspace() or ch in {"\\", ")", '"'}:
return None
delimiter_chars.append(ch)
if len(delimiter_chars) > 16:
return None
j += 1
return None
def _find_raw_terminator(line: str, start: int, terminator: str) -> int | None:
pos = line.find(terminator, start)
if pos < 0:
return None
return pos + len(terminator)
def _preprocessor_directive(line: str) -> str | None:
stripped = line.lstrip()
if not stripped.startswith("#"):
return None
text = stripped[1:].lstrip()
if not text:
return ""
return text.split(None, 1)[0]
def _preprocessor_expression_is_zero(line: str) -> bool:
stripped = line.lstrip()
if not stripped.startswith("#"):
return False
text = stripped[1:].strip()
return text == "if 0" or text.startswith("if 0 ")
def _current_preprocessor_active(stack: list[PreprocessorFrame]) -> bool:
return all(frame.this_branch_active for frame in stack)
# ---------------------------------------------------------------------------
# Comment blanking
# ---------------------------------------------------------------------------
def blank_comments_preserve_lines(lines: list[str]) -> list[str]:
"""Return source lines with comments blanked, preserving line count.
This is intentionally stream/state based, not regex based. Block comments and
line comments are replaced by spaces so later token column positions remain
stable. String, char, and raw string literals are preserved.
"""
result: list[str] = []
in_block_comment = False
raw_terminator: str | None = None
def preserve_trailing_continuation_backslashes(original: str, blanked: str) -> str:
stripped = original.rstrip()
if not stripped.endswith("\\"):
return blanked
chars = list(blanked)
index = len(stripped) - 1
while index >= 0 and original[index] == "\\":
if index >= len(chars):
chars.extend(" " for _ in range(index - len(chars) + 1))
chars[index] = "\\"
index -= 1
return "".join(chars)
for line in lines:
out: list[str] = []
index = 0
in_string = False
in_char = False
escape = False
while index < len(line):
ch = line[index]
next_ch = line[index + 1] if index + 1 < len(line) else ""
if raw_terminator is not None:
end = _find_raw_terminator(line, index, raw_terminator)
if end is None:
out.append(line[index:])
index = len(line)
continue
out.append(line[index:end])
index = end
raw_terminator = None
continue
if in_block_comment:
if ch == "*" and next_ch == "/":
out.append(" ")
out.append(" ")
index += 2
in_block_comment = False
continue
out.append("\t" if ch == "\t" else " ")
index += 1
continue
raw_start = None
if not in_string and not in_char:
raw_start = _try_raw_string_start(line, index)
if raw_start is not None:
terminator, after_open = raw_start
raw_terminator = terminator
out.append(line[index:after_open])
index = after_open
continue
if escape:
out.append(ch)
escape = False
index += 1
continue
if ch == "\\" and (in_string or in_char):
out.append(ch)
escape = True
index += 1
continue
if in_string:
out.append(ch)
if ch == '"':
in_string = False
index += 1
continue
if in_char:
out.append(ch)
if ch == "'":
in_char = False
index += 1
continue
if ch == '"':
out.append(ch)
in_string = True
index += 1
continue
if ch == "'":
out.append(ch)
in_char = True
index += 1
continue
if ch == "/" and next_ch == "/":
# Preserve columns by blanking the rest of the physical line.
out.append(" " * (len(line) - index))
index = len(line)
continue
if ch == "/" and next_ch == "*":
out.append(" ")
out.append(" ")
index += 2
in_block_comment = True
continue
out.append(ch)
index += 1
result.append(preserve_trailing_continuation_backslashes(line, "".join(out)))
return result
# ---------------------------------------------------------------------------
# Tokenization
# ---------------------------------------------------------------------------
_MULTI_CHAR_SYMBOLS_3 = {
"<=>",
"...",
}
_MULTI_CHAR_SYMBOLS_2 = {
"::",
"->",
".*",
"->*",
"==",
"!=",
"<=",
">=",
"&&",
"||",
"++",
"--",
"+=",
"-=",
"*=",
"/=",
"%=",
"&=",
"|=",
"^=",
"<<",
">>",
}
_SINGLE_CHAR_SYMBOLS = set("{}()[];,:<>*=~&|+-/!?.#%^")
def is_preprocessor_continuation_line(line: str) -> bool:
stripped = line.rstrip()
# Count trailing backslashes. An odd number means line continuation.
count = 0
for ch in reversed(stripped):
if ch == "\\":
count += 1
else:
break
return (count % 2) == 1
@dataclass(slots=True)
class PreprocessorFrame:
parent_active: bool
this_branch_active: bool
any_branch_taken: bool
def tokenize_lines(lines: list[str]) -> list[Token]:
"""Tokenize C++ source with a small stream scanner.
The scanner skips comment/string/char/raw-string contents. It is not a full
C++ lexer; it only emits tokens needed by the routing indexer.
"""
pp_stack: list[PreprocessorFrame] = []
in_preprocessor = False
tokens: list[Token] = []
raw_terminator: str | None = None
in_block_comment = False
for line_no, line in enumerate(lines, start=1):
if in_preprocessor:
in_preprocessor = is_preprocessor_continuation_line(line)
continue
directive = _preprocessor_directive(line)
if directive in {"if", "ifdef", "ifndef"}:
parent_active = _current_preprocessor_active(pp_stack)
if directive == "if" and _preprocessor_expression_is_zero(line):
branch_active = False
else:
# V1: assume unknown #if/#ifdef/#ifndef branch is active.
# This avoids hiding real source unless it is explicit #if 0.
branch_active = True
pp_stack.append(
PreprocessorFrame(
parent_active=parent_active,
this_branch_active=parent_active and branch_active,
any_branch_taken=parent_active and branch_active,
)
)
continue
if directive in {"else", "elif"}:
if pp_stack:
frame = pp_stack[-1]
# V1 rule:
# Unknown #if/#ifdef/#ifndef conditions are not evaluated.
# Therefore do not hide #else/#elif branches for unknown conditions.
# Only explicit #if 0 suppresses its first branch; after #else/#elif
# the branch becomes visible again.
frame.this_branch_active = frame.parent_active
frame.any_branch_taken = True
continue
if directive == "endif":
if pp_stack:
pp_stack.pop()
continue
if not _current_preprocessor_active(pp_stack):
continue
if directive is not None:
in_preprocessor = is_preprocessor_continuation_line(line)
continue
index = 0
in_string = False
in_char = False
escape = False
while index < len(line):
ch = line[index]
next_ch = line[index + 1] if index + 1 < len(line) else ""
if raw_terminator is not None:
end = _find_raw_terminator(line, index, raw_terminator)
if end is None:
break
index = end
raw_terminator = None
continue
if in_block_comment:
if ch == "*" and next_ch == "/":
index += 2
in_block_comment = False
continue
index += 1
continue
raw_start = None
if not in_string and not in_char:
raw_start = _try_raw_string_start(line, index)
if raw_start is not None:
terminator, after_open = raw_start
raw_terminator = terminator
index = after_open
continue
if escape:
escape = False
index += 1
continue
if ch == "\\" and (in_string or in_char):
escape = True
index += 1
continue
if in_string:
if ch == '"':
in_string = False
index += 1
continue
if in_char:
if ch == "'":
in_char = False
index += 1
continue
if ch == '"':
in_string = True
index += 1
continue
if ch == "'":
in_char = True
index += 1
continue
if ch == "/" and next_ch == "/":
break
if ch == "/" and next_ch == "*":
index += 2
in_block_comment = True
continue
if ch.isspace():
index += 1
continue
if _is_identifier_start(ch):
start = index
index += 1
while index < len(line) and _is_identifier_continue(line[index]):
index += 1
tokens.append(
Token(
value=line[start:index],
kind="identifier",
line=line_no,
col0=start,
)
)
continue
if ch.isdigit():
start = index
index += 1
while index < len(line):
current = line[index]
if current.isalnum() or current in {"_", ".", "'"}:
index += 1
continue
break
tokens.append(
Token(
value=line[start:index],
kind="number",
line=line_no,
col0=start,
)
)
continue
three = line[index : index + 3]
two = line[index : index + 2]
if three in _MULTI_CHAR_SYMBOLS_3:
tokens.append(Token(value=three, kind="symbol", line=line_no, col0=index))
index += 3
continue
if two in _MULTI_CHAR_SYMBOLS_2:
tokens.append(Token(value=two, kind="symbol", line=line_no, col0=index))
index += 2
continue
if ch in _SINGLE_CHAR_SYMBOLS:
tokens.append(Token(value=ch, kind="symbol", line=line_no, col0=index))
index += 1
continue
tokens.append(Token(value=ch, kind="symbol", line=line_no, col0=index))
index += 1
return tokens
# ---------------------------------------------------------------------------
# Token formatting helpers
# ---------------------------------------------------------------------------
def token_values(tokens: Iterable[Token]) -> list[str]:
return [token.value for token in tokens]
def tokens_to_text(tokens: list[Token]) -> str:
if not tokens:
return ""
parts: list[str] = []
previous = ""
no_space_before = {
",",
";",
")",
"]",
">",
"::",
".",
"->",
}
no_space_after = {
"(",
"[",
"<",
"::",
".",
"->",
"~",
}
for token in tokens:
value = token.value
if not parts:
parts.append(value)
elif value in no_space_before or previous in no_space_after:
parts.append(value)
else:
parts.append(" ")
parts.append(value)
previous = value
return normalize_signature_spacing("".join(parts))
def first_identifier(tokens: list[Token]) -> Token | None:
for token in tokens:
if token.kind == "identifier":
return token
return None
def previous_token(tokens: list[Token], before_index: int) -> Token | None:
index = before_index - 1
if index < 0:
return None
return tokens[index]
def find_matching_token(
tokens: list[Token],
open_index: int,
open_value: str,
close_value: str,
) -> int | None:
depth = 0
for index in range(open_index, len(tokens)):
value = tokens[index].value
if value == open_value:
depth += 1
elif value == close_value:
depth -= 1
if depth == 0:
return index
return None
def update_angle_depth(value: str, angle_depth: int) -> int:
if value == "<":
return angle_depth + 1
if value == ">":
return max(0, angle_depth - 1)
if value == ">>":
return max(0, angle_depth - 2)
return angle_depth
def split_top_level_commas(tokens: list[Token]) -> list[list[Token]]:
parts: list[list[Token]] = []
current: list[Token] = []
angle_depth = 0
paren_depth = 0
bracket_depth = 0
for token in tokens:
value = token.value
angle_depth = update_angle_depth(value, angle_depth)
if value == "(":
paren_depth += 1
elif value == ")":
paren_depth = max(0, paren_depth - 1)
elif value == "[":
bracket_depth += 1
elif value == "]":
bracket_depth = max(0, bracket_depth - 1)
if (
value == ","
and angle_depth == 0
and paren_depth == 0
and bracket_depth == 0
):
if current:
parts.append(current)
current = []
continue
current.append(token)
if current:
parts.append(current)
return parts
def iter_code_chars(lines: list[str]):
"""Yield code characters outside comments/string/char/raw-string regions.
Useful for lightweight depth scans. Returns tuples:
(line_no, col0, ch)
"""
raw_terminator: str | None = None
in_block_comment = False
pp_stack: list[PreprocessorFrame] = []
in_preprocessor = False
for line_no, line in enumerate(lines, start=1):
if in_preprocessor:
in_preprocessor = is_preprocessor_continuation_line(line)
continue
directive = _preprocessor_directive(line)
if directive in {"if", "ifdef", "ifndef"}:
parent_active = _current_preprocessor_active(pp_stack)
if directive == "if" and _preprocessor_expression_is_zero(line):
branch_active = False
else:
branch_active = True
pp_stack.append(
PreprocessorFrame(
parent_active=parent_active,
this_branch_active=parent_active and branch_active,
any_branch_taken=parent_active and branch_active,
)
)
continue
if directive in {"else", "elif"}:
if pp_stack:
frame = pp_stack[-1]
frame.this_branch_active = frame.parent_active
frame.any_branch_taken = True
continue
if directive == "endif":
if pp_stack:
pp_stack.pop()
continue
if not _current_preprocessor_active(pp_stack):
continue
if directive is not None:
in_preprocessor = is_preprocessor_continuation_line(line)
continue
index = 0
in_string = False
in_char = False
escape = False
while index < len(line):
ch = line[index]
next_ch = line[index + 1] if index + 1 < len(line) else ""
if raw_terminator is not None:
end = _find_raw_terminator(line, index, raw_terminator)
if end is None:
break
index = end
raw_terminator = None
continue
if in_block_comment:
if ch == "*" and next_ch == "/":
index += 2
in_block_comment = False
continue
index += 1
continue
raw_start = None
if not in_string and not in_char:
raw_start = _try_raw_string_start(line, index)
if raw_start is not None:
terminator, after_open = raw_start
raw_terminator = terminator
index = after_open
continue
if escape:
escape = False
index += 1
continue
if ch == "\\" and (in_string or in_char):
escape = True
index += 1
continue
if in_string:
if ch == '"':
in_string = False
index += 1
continue
if in_char:
if ch == "'":
in_char = False
index += 1
continue
if ch == '"':
in_string = True
index += 1
continue
if ch == "'":
in_char = True
index += 1
continue
if ch == "/" and next_ch == "/":
break
if ch == "/" and next_ch == "*":
index += 2
in_block_comment = True
continue
yield line_no, index, ch
index += 1