Debian Codegraph @

Tools/c-analyzer/c_parser/parser/_regexes.py

raw
1 # Regular expression patterns for C syntax.
2 #
3 # None of these patterns has any capturing. However, a number of them
4 # have capturing markers compatible with utils.set_capture_groups().
5
6 import textwrap
7
8
9 def _ind(text, level=1, edges='both'):
10 indent = ' ' * level
11 text = textwrap.indent(text, indent)
12 if edges == 'pre' or edges == 'both':
13 text = '\n' + indent + text.lstrip()
14 if edges == 'post' or edges == 'both':
15 text = text.rstrip() + '\n' + ' ' * (level - 1)
16 return text
17
18
19 #######################################
20 # general
21
22 HEX = r'(?: [0-9a-zA-Z] )'
23
24 STRING_LITERAL = textwrap.dedent(rf'''
25 (?:
26 # character literal
27 (?:
28 ['] [^'] [']
29 |
30 ['] \\ . [']
31 |
32 ['] \\x{HEX}{HEX} [']
33 |
34 ['] \\0\d\d [']
35 |
36 (?:
37 ['] \\o[01]\d\d [']
38 |
39 ['] \\o2[0-4]\d [']
40 |
41 ['] \\o25[0-5] [']
42 )
43 )
44 |
45 # string literal
46 (?:
47 ["] (?: [^"\\]* \\ . )* [^"\\]* ["]
48 )
49 # end string literal
50 )
51 ''')
52
53 _KEYWORD = textwrap.dedent(r'''
54 (?:
55 \b
56 (?:
57 auto |
58 extern |
59 register |
60 static |
61 _Thread_local |
62 typedef |
63
64 const |
65 volatile |
66
67 signed |
68 unsigned |
69 char |
70 short |
71 int |
72 long |
73 float |
74 double |
75 _Complex |
76 void |
77
78 struct |
79 union |
80 enum |
81
82 goto |
83 return |
84 sizeof |
85 break |
86 continue |
87 if |
88 else |
89 for |
90 do |
91 while |
92 switch |
93 case |
94 default |
95 entry
96 )
97 \b
98 )
99 ''')
100 KEYWORD = rf'''
101 # keyword
102 {_KEYWORD}
103 # end keyword
104 '''
105 _KEYWORD = ''.join(_KEYWORD.split())
106
107 IDENTIFIER = r'(?: [a-zA-Z_][a-zA-Z0-9_]* )'
108 # We use a negative lookahead to filter out keywords.
109 STRICT_IDENTIFIER = rf'(?: (?! {_KEYWORD} ) \b {IDENTIFIER} \b )'
110 ANON_IDENTIFIER = rf'(?: (?! {_KEYWORD} ) \b {IDENTIFIER} (?: - \d+ )? \b )'
111
112
113 #######################################
114 # types
115
116 SIMPLE_TYPE = textwrap.dedent(rf'''
117 # simple type
118 (?:
119 \b
120 (?:
121 void
122 |
123 (?: signed | unsigned ) # implies int
124 |
125 (?:
126 (?: (?: float | double | long\s+double ) \s+ )?
127 _Complex
128 )
129 |
130 (?:
131 _Complex
132 (?: \s+ (?: float | double | long\s+double ) )?
133 )
134 |
135 (?:
136 (?: (?: signed | unsigned ) \s+ )?
137 (?: (?: long | short ) \s+ )?
138 (?: char | short | int | long | float | double )
139 )
140 )
141 \b
142 )
143 # end simple type
144 ''')
145
146 COMPOUND_TYPE_KIND = r'(?: \b (?: struct | union | enum ) \b )'
147
148
149 #######################################
150 # variable declarations
151
152 _STORAGE = 'auto register static extern _Thread_local'.split()
153 STORAGE_CLASS = rf'(?: \b (?: {" | ".join(_STORAGE)} ) \b )'
154 TYPE_QUALIFIER = r'(?: \b (?: const | volatile ) \b )'
155 PTR_QUALIFIER = rf'(?: [*] (?: \s* {TYPE_QUALIFIER} )? )'
156
157 TYPE_SPEC = textwrap.dedent(rf'''
158 # type spec
159 (?:
160 {_ind(SIMPLE_TYPE, 2)}
161 |
162 (?:
163 [_]*typeof[_]*
164 \s* [(]
165 (?: \s* [*&] )*
166 \s* {STRICT_IDENTIFIER}
167 \s* [)]
168 )
169 |
170 # reference to a compound type
171 (?:
172 {COMPOUND_TYPE_KIND}
173 (?: \s* {ANON_IDENTIFIER} )?
174 )
175 |
176 # reference to a typedef
177 {STRICT_IDENTIFIER}
178 )
179 # end type spec
180 ''')
181
182 DECLARATOR = textwrap.dedent(rf'''
183 # declarator (possibly abstract)
184 (?:
185 (?: {PTR_QUALIFIER} \s* )*
186 (?:
187 (?:
188 (?: # <IDENTIFIER>
189 {STRICT_IDENTIFIER}
190 )
191 # Inside the brackets is actually a "constant expression".
192 (?: \s* \[ (?: \s* [^\]]+ \s* )? [\]] )* # arrays
193 )
194 |
195 (?:
196 [(] \s*
197 (?: # <WRAPPED_IDENTIFIER>
198 {STRICT_IDENTIFIER}
199 )
200 # Inside the brackets is actually a "constant expression".
201 (?: \s* \[ (?: \s* [^\]]+ \s* )? [\]] )* # arrays
202 \s* [)]
203 )
204 |
205 # func ptr
206 (?:
207 [(] (?: \s* {PTR_QUALIFIER} )? \s*
208 (?: # <FUNC_IDENTIFIER>
209 {STRICT_IDENTIFIER}
210 )
211 # Inside the brackets is actually a "constant expression".
212 (?: \s* \[ (?: \s* [^\]]+ \s* )? [\]] )* # arrays
213 \s* [)]
214 # We allow for a single level of paren nesting in parameters.
215 \s* [(] (?: [^()]* [(] [^)]* [)] )* [^)]* [)]
216 )
217 )
218 )
219 # end declarator
220 ''')
221
222 VAR_DECL = textwrap.dedent(rf'''
223 # var decl (and typedef and func return type)
224 (?:
225 (?:
226 (?: # <STORAGE>
227 {STORAGE_CLASS}
228 )
229 \s*
230 )?
231 (?:
232 (?: # <TYPE_QUAL>
233 {TYPE_QUALIFIER}
234 )
235 \s*
236 )?
237 (?:
238 (?: # <TYPE_SPEC>
239 {_ind(TYPE_SPEC, 4)}
240 )
241 )
242 \s*
243 (?:
244 (?: # <DECLARATOR>
245 {_ind(DECLARATOR, 4)}
246 )
247 )
248 )
249 # end var decl
250 ''')
251
252 INITIALIZER = textwrap.dedent(rf'''
253 # initializer
254 (?:
255 (?:
256 [(]
257 # no nested parens (e.g. func ptr)
258 [^)]*
259 [)]
260 \s*
261 )?
262 (?:
263 # a string literal
264 (?:
265 (?: {_ind(STRING_LITERAL, 4)} \s* )*
266 {_ind(STRING_LITERAL, 4)}
267 )
268 |
269
270 # a simple initializer
271 (?:
272 (?:
273 [^'",;{{]*
274 {_ind(STRING_LITERAL, 4)}
275 )*
276 [^'",;{{]*
277 )
278 |
279
280 # a struct/array literal
281 (?:
282 # We only expect compound initializers with
283 # single-variable declarations.
284 {{
285 (?:
286 [^'";]*?
287 {_ind(STRING_LITERAL, 5)}
288 )*
289 [^'";]*?
290 }}
291 (?= \s* ; ) # Note this lookahead.
292 )
293 )
294 )
295 # end initializer
296 ''')
297
298
299 #######################################
300 # compound type declarations
301
302 STRUCT_MEMBER_DECL = textwrap.dedent(rf'''
303 (?:
304 # inline compound type decl
305 (?:
306 (?: # <COMPOUND_TYPE_KIND>
307 {COMPOUND_TYPE_KIND}
308 )
309 (?:
310 \s+
311 (?: # <COMPOUND_TYPE_NAME>
312 {STRICT_IDENTIFIER}
313 )
314 )?
315 \s* {{
316 )
317 |
318 (?:
319 # typed member
320 (?:
321 # Technically it doesn't have to have a type...
322 (?: # <SPECIFIER_QUALIFIER>
323 (?: {TYPE_QUALIFIER} \s* )?
324 {_ind(TYPE_SPEC, 5)}
325 )
326 (?:
327 # If it doesn't have a declarator then it will have
328 # a size and vice versa.
329 \s*
330 (?: # <DECLARATOR>
331 {_ind(DECLARATOR, 6)}
332 )
333 )?
334 )
335
336 # sized member
337 (?:
338 \s* [:] \s*
339 (?: # <SIZE>
340 # This is actually a "constant expression".
341 \d+
342 |
343 [^'",}}]+
344 )
345 )?
346 \s*
347 (?: # <ENDING>
348 [,;]
349 )
350 )
351 |
352 (?:
353 \s*
354 (?: # <CLOSE>
355 }}
356 )
357 )
358 )
359 ''')
360
361 ENUM_MEMBER_DECL = textwrap.dedent(rf'''
362 (?:
363 (?:
364 \s*
365 (?: # <CLOSE>
366 }}
367 )
368 )
369 |
370 (?:
371 \s*
372 (?: # <NAME>
373 {IDENTIFIER}
374 )
375 (?:
376 \s* = \s*
377 (?: # <INIT>
378 # This is actually a "constant expression".
379 {_ind(STRING_LITERAL, 4)}
380 |
381 [^'",}}]+
382 )
383 )?
384 \s*
385 (?: # <ENDING>
386 , | }}
387 )
388 )
389 )
390 ''')
391
392
393 #######################################
394 # statements
395
396 SIMPLE_STMT_BODY = textwrap.dedent(rf'''
397 # simple statement body
398 (?:
399 (?:
400 [^'"{{}};]*
401 {_ind(STRING_LITERAL, 3)}
402 )*
403 [^'"{{}};]*
404 #(?= [;{{] ) # Note this lookahead.
405 )
406 # end simple statement body
407 ''')
408 SIMPLE_STMT = textwrap.dedent(rf'''
409 # simple statement
410 (?:
411 (?: # <SIMPLE_STMT>
412 # stmt-inline "initializer"
413 (?:
414 return \b
415 (?:
416 \s*
417 {_ind(INITIALIZER, 5)}
418 )?
419 )
420 |
421 # variable assignment
422 (?:
423 (?: [*] \s* )?
424 (?:
425 {STRICT_IDENTIFIER} \s*
426 (?: . | -> ) \s*
427 )*
428 {STRICT_IDENTIFIER}
429 (?: \s* \[ \s* \d+ \s* \] )?
430 \s* = \s*
431 {_ind(INITIALIZER, 4)}
432 )
433 |
434 # catchall return statement
435 (?:
436 return \b
437 (?:
438 (?:
439 [^'";]*
440 {_ind(STRING_LITERAL, 6)}
441 )*
442 \s* [^'";]*
443 )?
444 )
445 |
446 # simple statement
447 (?:
448 {_ind(SIMPLE_STMT_BODY, 4)}
449 )
450 )
451 \s*
452 (?: # <SIMPLE_ENDING>
453 ;
454 )
455 )
456 # end simple statement
457 ''')
458 COMPOUND_STMT = textwrap.dedent(rf'''
459 # compound statement
460 (?:
461 \b
462 (?:
463 (?:
464 (?: # <COMPOUND_BARE>
465 else | do
466 )
467 \b
468 )
469 |
470 (?:
471 (?: # <COMPOUND_LABELED>
472 (?:
473 case \b
474 (?:
475 [^'":]*
476 {_ind(STRING_LITERAL, 7)}
477 )*
478 \s* [^'":]*
479 )
480 |
481 default
482 |
483 {STRICT_IDENTIFIER}
484 )
485 \s* [:]
486 )
487 |
488 (?:
489 (?: # <COMPOUND_PAREN>
490 for | while | if | switch
491 )
492 \s* (?= [(] ) # Note this lookahead.
493 )
494 )
495 \s*
496 )
497 # end compound statement
498 ''')
499
500
501 #######################################
502 # function bodies
503
504 LOCAL = textwrap.dedent(rf'''
505 (?:
506 # an empty statement
507 (?: # <EMPTY>
508 ;
509 )
510 |
511 # inline type decl
512 (?:
513 (?:
514 (?: # <INLINE_LEADING>
515 [^;{{}}]+?
516 )
517 \s*
518 )?
519 (?: # <INLINE_PRE>
520 (?: {STORAGE_CLASS} \s* )?
521 (?: {TYPE_QUALIFIER} \s* )?
522 )? # </INLINE_PRE>
523 (?: # <INLINE_KIND>
524 {COMPOUND_TYPE_KIND}
525 )
526 (?:
527 \s+
528 (?: # <INLINE_NAME>
529 {STRICT_IDENTIFIER}
530 )
531 )?
532 \s* {{
533 )
534 |
535 # var decl
536 (?:
537 (?: # <STORAGE>
538 {STORAGE_CLASS}
539 )? # </STORAGE>
540 (?:
541 \s*
542 (?: # <VAR_DECL>
543 {_ind(VAR_DECL, 5)}
544 )
545 )
546 (?:
547 (?:
548 # initializer
549 # We expect only basic initializers.
550 \s* = \s*
551 (?: # <VAR_INIT>
552 {_ind(INITIALIZER, 6)}
553 )
554 )?
555 (?:
556 \s*
557 (?: # <VAR_ENDING>
558 [,;]
559 )
560 )
561 )
562 )
563 |
564 {_ind(COMPOUND_STMT, 2)}
565 |
566 # start-of-block
567 (?:
568 (?: # <BLOCK_LEADING>
569 (?:
570 [^'"{{}};]*
571 {_ind(STRING_LITERAL, 5)}
572 )*
573 [^'"{{}};]*
574 # Presumably we will not see "== {{".
575 [^\s='"{{}});]
576 \s*
577 )? # </BLOCK_LEADING>
578 (?: # <BLOCK_OPEN>
579 {{
580 )
581 )
582 |
583 {_ind(SIMPLE_STMT, 2)}
584 |
585 # end-of-block
586 (?: # <BLOCK_CLOSE>
587 }}
588 )
589 )
590 ''')
591
592 LOCAL_STATICS = textwrap.dedent(rf'''
593 (?:
594 # inline type decl
595 (?:
596 (?:
597 (?: # <INLINE_LEADING>
598 [^;{{}}]+?
599 )
600 \s*
601 )?
602 (?: # <INLINE_PRE>
603 (?: {STORAGE_CLASS} \s* )?
604 (?: {TYPE_QUALIFIER} \s* )?
605 )?
606 (?: # <INLINE_KIND>
607 {COMPOUND_TYPE_KIND}
608 )
609 (?:
610 \s+
611 (?: # <INLINE_NAME>
612 {STRICT_IDENTIFIER}
613 )
614 )?
615 \s* {{
616 )
617 |
618 # var decl
619 (?:
620 # We only look for static variables.
621 (?: # <STATIC_DECL>
622 static \b
623 (?: \s* {TYPE_QUALIFIER} )?
624 \s* {_ind(TYPE_SPEC, 4)}
625 \s* {_ind(DECLARATOR, 4)}
626 )
627 \s*
628 (?:
629 (?: # <STATIC_INIT>
630 = \s*
631 {_ind(INITIALIZER, 4)}
632 \s*
633 [,;{{]
634 )
635 |
636 (?: # <STATIC_ENDING>
637 [,;]
638 )
639 )
640 )
641 |
642 # everything else
643 (?:
644 (?: # <DELIM_LEADING>
645 (?:
646 [^'"{{}};]*
647 {_ind(STRING_LITERAL, 4)}
648 )*
649 \s* [^'"{{}};]*
650 )
651 (?:
652 (?: # <BLOCK_OPEN>
653 {{
654 )
655 |
656 (?: # <BLOCK_CLOSE>
657 }}
658 )
659 |
660 (?: # <STMT_END>
661 ;
662 )
663 )
664 )
665 )
666 ''')
667
668
669 #######################################
670 # global declarations
671
672 GLOBAL = textwrap.dedent(rf'''
673 (?:
674 # an empty statement
675 (?: # <EMPTY>
676 ;
677 )
678 |
679
680 # compound type decl (maybe inline)
681 (?:
682 (?:
683 (?: # <COMPOUND_LEADING>
684 [^;{{}}]+?
685 )
686 \s*
687 )?
688 (?: # <COMPOUND_KIND>
689 {COMPOUND_TYPE_KIND}
690 )
691 (?:
692 \s+
693 (?: # <COMPOUND_NAME>
694 {STRICT_IDENTIFIER}
695 )
696 )?
697 \s* {{
698 )
699 |
700 # bogus inline decl artifact
701 # This simplifies resolving the relative syntactic ambiguity of
702 # inline structs.
703 (?:
704 (?: # <FORWARD_KIND>
705 {COMPOUND_TYPE_KIND}
706 )
707 \s*
708 (?: # <FORWARD_NAME>
709 {ANON_IDENTIFIER}
710 )
711 (?: # <MAYBE_INLINE_ACTUAL>
712 [^=,;({{[*\]]*
713 [=,;({{]
714 )
715 )
716 |
717
718 # typedef
719 (?:
720 \b typedef \b \s*
721 (?: # <TYPEDEF_DECL>
722 {_ind(VAR_DECL, 4)}
723 )
724 (?:
725 # We expect no inline type definitions in the parameters.
726 \s* [(] \s*
727 (?: # <TYPEDEF_FUNC_PARAMS>
728 [^{{;]*
729 )
730 \s* [)]
731 )?
732 \s* ;
733 )
734 |
735
736 # func decl/definition & var decls
737 # XXX dedicated pattern for funcs (more restricted)?
738 (?:
739 (?:
740 (?: # <VAR_STORAGE>
741 {STORAGE_CLASS}
742 )
743 \s*
744 )?
745 (?:
746 (?: # <FUNC_INLINE>
747 \b inline \b
748 )
749 \s*
750 )?
751 (?: # <VAR_DECL>
752 {_ind(VAR_DECL, 4)}
753 )
754 (?:
755 # func decl / definition
756 (?:
757 (?:
758 # We expect no inline type definitions in the parameters.
759 \s* [(] \s*
760 (?: # <FUNC_PARAMS>
761 [^{{;]*
762 )
763 \s* [)] \s*
764 (?: # <FUNC_DELIM>
765 [{{;]
766 )
767 )
768 |
769 (?:
770 # This is some old-school syntax!
771 \s* [(] \s*
772 # We throw away the bare names:
773 {STRICT_IDENTIFIER}
774 (?: \s* , \s* {STRICT_IDENTIFIER} )*
775 \s* [)] \s*
776
777 # We keep the trailing param declarations:
778 (?: # <FUNC_LEGACY_PARAMS>
779 # There's at least one!
780 (?: {TYPE_QUALIFIER} \s* )?
781 {_ind(TYPE_SPEC, 7)}
782 \s*
783 {_ind(DECLARATOR, 7)}
784 \s* ;
785 (?:
786 \s*
787 (?: {TYPE_QUALIFIER} \s* )?
788 {_ind(TYPE_SPEC, 8)}
789 \s*
790 {_ind(DECLARATOR, 8)}
791 \s* ;
792 )*
793 )
794 \s* {{
795 )
796 )
797 |
798 # var / typedef
799 (?:
800 (?:
801 # initializer
802 # We expect only basic initializers.
803 \s* = \s*
804 (?: # <VAR_INIT>
805 {_ind(INITIALIZER, 6)}
806 )
807 )?
808 \s*
809 (?: # <VAR_ENDING>
810 [,;]
811 )
812 )
813 )
814 )
815 )
816 ''')