Debian Codegraph @

lezer-cpp/src/cpp.grammar

raw
1 @precedence {
2 capture
3 subscript
4 arguments
5 typeSizeof
6 placeholder
7 baseClass
8 suffix @left
9 prefix @left
10 pointer @left
11 mult @left
12 add @left
13 bitShift @left
14 kwExprExtra
15 kwExpr
16 greaterLess @left
17 equal @left
18 bitAnd @left
19 bitXor @left
20 bitOr @left
21 logicAnd @left
22 logicOr @left
23 concat @left
24 params
25 array
26 specifiers
27 cast
28 more
29 arrowStar
30 subscriptDesignator
31 return
32 typeIdentifier
33 structName
34 enumBase
35 newOpArray
36 namespace
37 else @right
38 caseLabel
39 simpleTypeIdentifier
40 qualifiers
41 statement
42 pack
43 assign @right
44 ternary @right
45 absDecl
46 }
47
48 @top Program {
49 topLevelItem*
50 }
51
52 topLevelItem {
53 FunctionDefinition |
54 LinkageSpecification |
55 Declaration |
56 ExportDeclaration |
57 ImportDeclaration |
58 statement |
59 TypeDefinition |
60 emptyDeclaration |
61 NamespaceDefinition |
62 ConceptDefinition |
63 UsingDeclaration |
64 AliasDeclaration |
65 StaticAssertDeclaration |
66 TemplateDeclaration |
67 TemplateInstantiation |
68 FunctionDefinition { constructorOrDestructorDefinition } |
69 FunctionDefinition { operatorCastDefinition } |
70 Declaration { operatorCastDeclaration } |
71 ModuleDeclaration
72 }
73
74 @skip { ppSpace } {
75 ppEnd { BlockComment* LineComment? "\n" }
76
77 PreprocDirective {
78 pp<"include"> (String | SystemLibString | Identifier ArgumentList?) ppEnd |
79 pp<"define"> Identifier preprocParams? PreprocArg? "\n" |
80 pp<"if"> PreprocArg "\n" |
81 (pp<"ifdef"> | pp<"ifndef">) Identifier ppEnd |
82 pp<"else"> ppEnd |
83 pp<"endif"> ppEnd |
84 pp<"elif"> PreprocArg "\n" |
85 PreprocDirectiveName PreprocArg "\n"
86 }
87 }
88
89 preprocParams { "(" commaSep<Identifier | "..."> ")" }
90
91 FunctionDefinition {
92 Attribute* msCallModifier? declarationSpecifiers declarator (CompoundStatement | TryStatement)
93 }
94
95 Declaration {
96 Attribute* declarationSpecifiers commaSep1<declarator | InitDeclarator> ";"
97 }
98
99 TypeDefinition {
100 kw<"typedef"> typeQualifier* typeSpecifier commaSep1<typeDeclarator> ";"
101 }
102
103 declarationSpecifiers {
104 (storageClassSpecifier | typeQualifier | AttributeSpecifier | MsDeclspecModifier)* ~specifiers typeSpecifier (
105 storageClassSpecifier |
106 typeQualifier |
107 AttributeSpecifier |
108 MsDeclspecModifier
109 )*
110 }
111
112 LinkageSpecification {
113 kw<"extern"> String (FunctionDefinition | Declaration | CompoundStatement)
114 }
115
116 AttributeSpecifier {
117 kw<"__attribute__"> "(" ArgumentList ")"
118 }
119
120 MsDeclspecModifier {
121 kw<"__declspec"> "(" Identifier ")"
122 }
123
124 MsBasedModifier {
125 kw<"__based"> ArgumentList
126 }
127
128 msCallModifier {
129 @specialize[@name=MsCallModifier]<identifier,
130 "__cdecl" | "__clrcall" | "__stdcall" | "__fastcall" | "__thiscall" | "__vectorcall">
131 }
132
133 msPointerModifier {
134 @specialize[@name=MsPointerModifier]<identifier, "__restrict" | "__uptr" | "__sptr" | "_unaligned" | "__unaligned">
135 }
136
137 declarator {
138 PointerDeclarator[@dynamicPrecedence=1] {
139 MsBasedModifier? "*" msPointerModifier* typeQualifier* declarator
140 } |
141 FunctionDeclarator |
142 ArrayDeclarator {
143 declarator !array "[" typeQualifier* (expression | "*")? "]"
144 } |
145 ParenthesizedDeclarator[@dynamicPrecedence=-10] {
146 "(" declarator ")"
147 } |
148 Identifier ~identifier |
149 ReferenceDeclarator[@dynamicPrecedence=1] {
150 ("&" | "&&") declarator
151 } |
152 ScopedIdentifier ~identifier |
153 TemplateFunction ~identifier |
154 OperatorName<kw<"operator">> ~opDecl |
155 DestructorName |
156 StructuredBindingDeclarator[@dynamicPrecedence=-1] {
157 "[" commaSep1<Identifier ~identifier> "]"
158 }
159 }
160
161 functionSpecifiers {
162 (typeQualifier | virtualSpecifier | "&" | "&&" | Noexcept | RequiresClause | ThrowSpecifier)* TrailingReturnType?
163 }
164
165 FunctionDeclarator {
166 declarator !params ParameterList !specifiers functionSpecifiers AttributeSpecifier*
167 (!specifiers )*
168 }
169
170 fieldDeclarator {
171 PointerDeclarator[@dynamicPrecedence=1] {
172 MsBasedModifier? "*" msPointerModifier* typeQualifier* fieldDeclarator
173 } |
174 FunctionDeclarator {
175 fieldDeclarator !params ParameterList functionSpecifiers
176 } |
177 ArrayDeclarator {
178 fieldDeclarator !array "[" typeQualifier* (expression | "*")? "]"
179 } |
180 ParenthesizedDeclarator[@dynamicPrecedence=-10] {
181 "(" fieldDeclarator ")"
182 } |
183 FieldIdentifier |
184 ReferenceDeclarator[@dynamicPrecedence=1] {
185 ("&" | "&&") fieldDeclarator
186 } |
187 TemplateMethod |
188 OperatorName<kw<"operator">> ~opDecl
189 }
190
191 typeDeclarator {
192 PointerDeclarator[@dynamicPrecedence=1] {
193 MsBasedModifier? "*" msPointerModifier* typeQualifier* typeDeclarator
194 } |
195 FunctionDeclarator {
196 typeDeclarator !params ParameterList
197 } |
198 ArrayDeclarator {
199 typeDeclarator !array "[" typeQualifier* (expression | "*")? "]"
200 } |
201 ParenthesizedDeclarator[@dynamicPrecedence=-10] {
202 "(" typeDeclarator ")"
203 } |
204 TypeIdentifier
205 }
206
207 abstractDeclarator {
208 AbstractPointerDeclarator[@dynamicPrecedence=1] {
209 "*" (!qualifiers typeQualifier)* (!absDecl abstractDeclarator)?
210 } |
211 AbstractFunctionDeclarator |
212 AbstractArrayDeclarator {
213 abstractDeclarator? !array "[" typeQualifier* (expression | "*")? "]"
214 } |
215 AbstractParenthesizedDeclarator {
216 "(" abstractDeclarator ")"
217 } |
218 AbstractReferenceDeclarator {
219 ("&" | "&&") (!absDecl abstractDeclarator)?
220 }
221 }
222
223 AbstractFunctionDeclarator {
224 abstractDeclarator? !params ParameterList (typeQualifier | "&" | "&&" | Noexcept | ThrowSpecifier)* !return TrailingReturnType?
225 }
226
227 InitDeclarator {
228 declarator "=" (InitializerList | expression) |
229 declarator (!arguments ArgumentList | InitializerList)
230 }
231
232 CompoundStatement {
233 !statement "{" topLevelItem* !statement "}"
234 }
235
236 storageClassSpecifier {
237 kw<"extern"> | kw<"static"> | kw<"register"> | kw<"inline"> | kw<"thread_local">
238 }
239
240 typeQualifier {
241 kw<"const"> | kw<"volatile"> | kw<"restrict"> | kw<"_Atomic"> | kw<"mutable"> | kw<"constexpr"> | kw<"constinit"> | kw<"consteval">
242 }
243
244 typeSpecifier[@isGroup=Type] {
245 StructSpecifier { kw<"struct"> classSpecifier } |
246 UnionSpecifier { kw<"union"> classSpecifier } |
247 ClassSpecifier { kw<"class"> classSpecifier } |
248 EnumSpecifier {
249 kw<"enum"> (kw<"class"> | kw<"struct">)?
250 (className (!enumBase enumBaseClause)? (!structName EnumeratorList)? | EnumeratorList)
251 } |
252 DependentType[@dynamicPrecedence=-1] {
253 kw<"typename"> typeSpecifier
254 } |
255 Decltype |
256 SizedTypeSpecifier |
257 primitiveType ~afterPrimitive |
258 kw<"auto"> |
259 ScopedTypeIdentifier !simpleTypeIdentifier |
260 TypeIdentifier !simpleTypeIdentifier |
261 TemplateType |
262 PlaceholderTypeSpecifier { typeSpecifier !placeholder (kw<"auto"> | Decltype) }
263 }
264
265 classSpecifier {
266 MsDeclspecModifier? Attribute*
267 (className | className? !structName virtualSpecifier? BaseClassClause? FieldDeclarationList)
268 }
269
270 Decltype { kw<"decltype"> "(" (expression | kw<"auto">) ")" }
271
272 // FIXME this used to allow arbitrary TypeNames as well as primitive
273 // types, which I guess might be useful with preprocessor-defined
274 // types, but requires even more GLR magic.
275 SizedTypeSpecifier {
276 @specialize[@name=TypeSize]<identifier, "signed" | "unsigned" | "long" | "short">+ primitiveType?
277 }
278
279 EnumeratorList {
280 "{" commaSepTrailing<Enumerator> "}"
281 }
282
283 FieldDeclarationList {
284 "{" fieldDeclarationListItem* "}"
285 }
286
287 fieldDeclarationListItem {
288 FieldDeclaration {
289 Attribute* kw<"virtual">? declarationSpecifiers (fieldDeclarator ("," fieldDeclarator)*)?
290 (BitfieldClause | InitializerList | "=" (expression | InitializerList))? ";"
291 } |
292 TemplateDeclaration |
293 FunctionDefinition {
294 Attribute* kw<"virtual">? declarationSpecifiers
295 fieldDeclarator (CompoundStatement | TryStatement | !more DefaultMethodClause | DeleteMethodClause)
296 } |
297 FunctionDefinition { constructorOrDestructorDefinition } |
298 Declaration {
299 (kw<"virtual"> | ExplicitFunctionSpecifier)? FunctionDeclarator ";"
300 } |
301 FunctionDefinition { operatorCastDefinition } |
302 Declaration { operatorCastDeclaration } |
303 FriendDeclaration |
304 AccessSpecifier |
305 AliasDeclaration |
306 UsingDeclaration |
307 TypeDefinition |
308 StaticAssertDeclaration
309 }
310
311 BitfieldClause {
312 ":" expression
313 }
314
315 Enumerator {
316 Identifier ("=" expression)?
317 }
318
319 ParameterList {
320 "(" ~argOrParam commaSep<(ParameterDeclaration | OptionalParameterDeclaration | VariadicParameterDeclaration | "..." ~argOrParam)> ")"
321 }
322
323 ParameterDeclaration {
324 Attribute* declarationSpecifiers (declarator | abstractDeclarator)?
325 }
326
327 statement[@isGroup=Statement] {
328 CaseStatement |
329 nonCaseStatement |
330 AttributeStatement |
331 ForRangeLoop |
332 TryStatement |
333 ThrowStatement
334 }
335
336 nonCaseStatement {
337 LabeledStatement |
338 CompoundStatement |
339 ExpressionStatement |
340 IfStatement |
341 SwitchStatement |
342 DoStatement |
343 WhileStatement |
344 ForStatement |
345 ReturnStatement |
346 BreakStatement |
347 ContinueStatement |
348 GotoStatement |
349 CoReturnStatement |
350 CoYieldStatement
351 }
352
353 AttributeStatement { Attribute+ (CaseStatement | nonCaseStatement | ForRangeLoop | TryStatement) }
354
355 LabeledStatement {
356 StatementIdentifier ":" statement
357 }
358
359 ExpressionStatement {
360 (expression | CommaExpression)? ";"
361 }
362
363 IfStatement {
364 kw<"if"> kw<"constexpr">? ConditionClause statement (!else kw<"else"> statement)?
365 }
366
367 SwitchStatement {
368 kw<"switch"> ConditionClause CompoundStatement
369 }
370
371 CaseStatement {
372 (kw<"case"> expression | kw<"default">) ":" (!caseLabel (nonCaseStatement | AttributeStatement | Declaration | TypeDefinition))*
373 }
374
375 WhileStatement {
376 kw<"while"> ConditionClause statement
377 }
378
379 DoStatement {
380 kw<"do"> statement kw<"while"> ParenthesizedExpression ";"
381 }
382
383 ForStatement {
384 kw<"for"> "(" (Declaration | (expression | CommaExpression)? ";") expression? ";" (expression | CommaExpression)? ")" statement
385 }
386
387 ReturnStatement {
388 kw<"return"> (expression | CommaExpression)? ";" |
389 kw<"return"> InitializerList ";"
390 }
391
392 BreakStatement {
393 kw<"break"> ";"
394 }
395
396 ContinueStatement {
397 kw<"continue"> ";"
398 }
399
400 GotoStatement {
401 kw<"goto"> StatementIdentifier ";"
402 }
403
404 CoReturnStatement {
405 kw<"co_return"> expression? ";"
406 }
407
408 CoYieldStatement {
409 kw<"co_yield"> expression ";"
410 }
411
412 expression[@isGroup=Expression] {
413 ConditionalExpression |
414 AssignmentExpression |
415 BinaryExpression |
416 UnaryExpression |
417 UpdateExpression |
418 CastExpression |
419 PointerExpression |
420 SizeofExpression |
421 CoAwaitExpression |
422 RequiresExpression |
423 SubscriptExpression |
424 CallExpression |
425 FieldExpression |
426 CompoundLiteralExpression |
427 Identifier ~identifier |
428 Number |
429 String |
430 True |
431 False |
432 kw<"NULL"> |
433 ConcatenatedString |
434 CharLiteral |
435 ParenthesizedExpression |
436 TemplateFunction ~identifier |
437 ScopedIdentifier ~identifier |
438 OperatorName<kw<"operator">> ~opDecl |
439 NewExpression |
440 DeleteExpression |
441 LambdaExpression |
442 ParameterPackExpansion |
443 kw<"nullptr"> |
444 kw<"this"> |
445 RawString |
446 UserDefinedLiteral |
447 ParamPack { "..." ~argOrParam }
448 }
449
450 CommaExpression {
451 expression !more "," (expression | CommaExpression)
452 }
453
454 ConditionalExpression {
455 expression !ternary "?" expression ":" expression
456 }
457
458 AssignmentExpression {
459 (Identifier |
460 CallExpression |
461 FieldExpression |
462 PointerExpression |
463 SubscriptExpression |
464 ParenthesizedExpression |
465 ScopedNamespaceIdentifier) !assign (UpdateOp | "=") (expression | InitializerList)
466 }
467
468 PointerExpression {
469 ("*" | "&") !pointer expression
470 }
471
472 UnaryExpression {
473 (LogicOp<"!"> | ArithOp<"+" | "-"> | BitOp<"~">) !suffix expression
474 }
475
476 // Note: operators that conflict with other tokens are defined using
477 // inline rules (so that the tokens can be shared), others directly using
478 // ArithOp-style token helpers.
479 BinaryExpression {
480 expression !add ArithOp<"+" | "-"> expression |
481 expression !mult (ArithOp { "*" } | ArithOp<"/" | "%">) expression |
482 expression !logicAnd LogicOp { "&&" } expression |
483 expression !logicOr LogicOp { "||" } expression |
484 expression !bitOr BitOp<"|"> expression |
485 expression !bitXor BitOp<"^"> expression |
486 expression !bitAnd BitOp { "&" } expression |
487 expression !equal CompareOp<"==" | "!="> expression |
488 expression !greaterLess
489 (CompareOp<"<=" | ">=" | "<=>"> | @extend[@name=CompareOp]<"<"> | @extend[@name=CompareOp]<templateArgsEnd, ">">) expression |
490 expression !bitShift BitOp<"<<" | ">>"> expression |
491 expression !concat incDec expression
492 }
493
494 UpdateExpression {
495 incDec !prefix expression | expression !suffix incDec
496 }
497
498 CastExpression {
499 "(" TypeDescriptor ")" expression
500 }
501
502 TypeDescriptor {
503 typeQualifier* typeSpecifier typeQualifier* abstractDeclarator?
504 }
505
506 SizeofExpression {
507 kw<"sizeof"> !kwExpr (expression | [@dynamicPrecedence=1] { "(" !typeSizeof TypeDescriptor ")" }) |
508 kw<"sizeof"> "..." !kwExprExtra "(" Identifier ")"
509 }
510
511 CoAwaitExpression {
512 kw<"co_await"> !kwExpr expression
513 }
514
515 RequiresExpression {
516 kw<"requires"> !kwExpr ParameterList? RequirementList { "{" requirement* "}" }
517 }
518
519 requirement {
520 SimpleRequirement { (expression | CommaExpression)? ";" } |
521 TypeRequirement { kw<"typename"> className ";" } |
522 CompoundRequirement { "{" expression "}" kw<"noexcept">? ReturnTypeRequirement? ";" } |
523 ReturnTypeRequirement ";" |
524 RequiresClause ";"
525 }
526
527 ReturnTypeRequirement { "->" expression }
528
529 SubscriptExpression {
530 expression !subscript "[" expression "]"
531 }
532
533 CallExpression {
534 expression !arguments ArgumentList |
535 primitiveType ~afterPrimitive ArgumentList
536 }
537
538 ArgumentList {
539 "(" ~argOrParam commaSep<(expression | InitializerList)> ")"
540 }
541
542 FieldExpression {
543 expression !subscript ("." | "->") FieldIdentifier |
544 expression !subscript ("." | "->") (DestructorName | TemplateMethod)
545 }
546
547 CompoundLiteralExpression {
548 "(" TypeDescriptor ")" InitializerList |
549 (TypeIdentifier | TemplateType | ScopedTypeIdentifier) InitializerList
550 }
551
552 ParenthesizedExpression {
553 "(" (expression | CommaExpression) ")"
554 }
555
556 InitializerList {
557 "{" commaSepTrailing<InitializerPair | expression | InitializerList> "}"
558 }
559
560 InitializerPair {
561 (SubscriptDesignator | FieldDesignator)+ "=" (expression | InitializerList)
562 }
563
564 SubscriptDesignator {
565 "[" expression !subscriptDesignator "]"
566 }
567
568 FieldDesignator {
569 "." FieldIdentifier
570 }
571
572 @skip {} {
573 CharLiteral {
574 charLiteralStart (EscapeSequence | charLiteralContent) charLiteralEnd
575 }
576
577 String[isolate] {
578 stringLiteralStart (stringLiteralContent | EscapeSequence)* stringLiteralEnd
579 }
580
581 Macro[@dynamicPrecedence=-10] { MacroName (whitespace? ArgumentList?) }
582 }
583
584 ConcatenatedString {
585 (RawString | String) (RawString | String)+
586 }
587
588 UserDefinedLiteral {
589 (Number | CharLiteral | String | RawString | ConcatenatedString) Identifier
590 }
591
592 TypeIdentifier {
593 identifier
594 }
595
596 FieldIdentifier {
597 identifier
598 }
599
600 StatementIdentifier {
601 identifier
602 }
603
604 Identifier {
605 identifier
606 }
607
608 emptyDeclaration {
609 Attribute* typeSpecifier ";"
610 }
611
612 className {
613 (TypeIdentifier | ScopedTypeIdentifier | TemplateType)
614 }
615
616 virtualSpecifier {
617 @specialize[@name=VirtualSpecifier]<identifier, "final" | "override">
618 }
619
620 ExplicitFunctionSpecifier {
621 kw<"explicit"> |
622 kw<"explicit"> !arguments "(" expression ")"
623 }
624
625 BaseClassClause {
626 ":" Attribute* commaSep1<(access | kw<"virtual">)* className "..."?>
627 }
628
629 enumBaseClause {
630 ":" (ScopedTypeIdentifier | TypeIdentifier | SizedTypeSpecifier | primitiveType)
631 }
632
633 TemplateDeclaration {
634 kw<"template"> TemplateParameterList RequiresClause? (
635 emptyDeclaration |
636 AliasDeclaration |
637 Declaration |
638 TemplateDeclaration |
639 FriendDeclaration |
640 FunctionDefinition |
641 ConceptDefinition |
642 FunctionDefinition { constructorOrDestructorDefinition } |
643 FunctionDefinition { operatorCastDefinition }
644 )
645 }
646
647 RequiresClause {
648 kw<"requires"> constraint
649 }
650
651 constraint {
652 True |
653 False |
654 className |
655 ParenthesizedExpression |
656 LambdaExpression |
657 RequiresExpression |
658 ConstraintConjuction { constraint !logicAnd LogicOp { "&&" } constraint } |
659 ConstraintDisjunction { constraint !logicOr LogicOp { "||" } constraint }
660 }
661
662 TemplateInstantiation {
663 kw<"template"> declarationSpecifiers? declarator ";"
664 }
665
666 TemplateParameterList {
667 "<" commaSep<templateParameter> (templateArgsEnd | templateArgsEndFallback)
668 }
669
670 templateParameter {
671 ParameterDeclaration |
672 OptionalParameterDeclaration |
673 TypeParameterDeclaration |
674 VariadicParameterDeclaration |
675 OptionalTypeParameterDeclaration |
676 VariadicTypeParameterDeclaration |
677 TemplateTemplateParameterDeclaration {
678 kw<"template"> TemplateParameterList
679 (TypeParameterDeclaration | VariadicTypeParameterDeclaration | OptionalTypeParameterDeclaration)
680 }
681 }
682
683 TypeParameterDeclaration {
684 (kw<"typename"> | kw<"class">) !typeIdentifier TypeIdentifier? !typeIdentifier
685 }
686
687 OptionalTypeParameterDeclaration {
688 (kw<"typename"> | kw<"class">) TypeIdentifier? !typeIdentifier "=" typeSpecifier
689 }
690
691 VariadicTypeParameterDeclaration {
692 (kw<"typename"> | kw<"class">) "..." !typeIdentifier TypeIdentifier? !typeIdentifier
693 }
694
695 OptionalParameterDeclaration {
696 declarationSpecifiers declarator? "=" expression
697 }
698
699 VariadicParameterDeclaration {
700 declarationSpecifiers (
701 VariadicDeclarator |
702 ReferenceDeclarator { ("&&" | "&") VariadicDeclarator }
703 )
704 }
705
706 VariadicDeclarator {
707 "..." Identifier?
708 }
709
710 OperatorCast {
711 ((NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? "::" skw<"operator"> | kw<"operator">)
712 declarationSpecifiers !cast abstractDeclarator
713 }
714
715 FieldInitializerList {
716 ":" commaSep1<FieldInitializer>
717 }
718
719 FieldInitializer {
720 (FieldIdentifier | ScopedFieldIdentifier) !arguments (InitializerList | ArgumentList) "..."?
721 }
722
723 operatorCastDefinition {
724 (storageClassSpecifier | typeQualifier | AttributeSpecifier)*
725 ~specifiers (kw<"virtual"> | ExplicitFunctionSpecifier)?
726 OperatorCast (CompoundStatement | TryStatement | DefaultMethodClause | DeleteMethodClause)
727 }
728
729 operatorCastDeclaration {
730 (kw<"virtual"> | ExplicitFunctionSpecifier)? OperatorCast ("=" expression)? ";"
731 }
732
733 constructorOrDestructorDefinition {
734 (storageClassSpecifier | typeQualifier | AttributeSpecifier)*
735 ~specifiers
736 (kw<"virtual"> | ExplicitFunctionSpecifier)?
737 FunctionDeclarator
738 FieldInitializerList?
739 (CompoundStatement | TryStatement | DefaultMethodClause | DeleteMethodClause)
740 }
741
742 DefaultMethodClause {
743 "=" kw<"default"> ";"
744 }
745
746 DeleteMethodClause {
747 "=" kw<"delete"> ";"
748 }
749
750 FriendDeclaration {
751 kw<"friend"> (Declaration | FunctionDefinition | (kw<"class"> | kw<"struct"> | kw<"union">)? className ";")
752 }
753
754 AccessSpecifier {
755 access ":"
756 }
757
758 TrailingReturnType {
759 "->" typeQualifier? typeSpecifier (!absDecl abstractDeclarator)?
760 }
761
762 Noexcept {
763 kw<"noexcept"> (!arguments "(" expression? ")")?
764 }
765
766 ThrowSpecifier {
767 kw<"throw"> "(" commaSep<TypeDescriptor> ")"
768 }
769
770 TemplateType {
771 (TypeIdentifier | ScopedTypeIdentifier) ~identifier TemplateArgumentList
772 }
773
774 TemplateMethod {
775 (FieldIdentifier | ScopedFieldIdentifier) ~identifier TemplateArgumentList
776 }
777
778 TemplateFunction {
779 (Identifier | ScopedIdentifier) ~identifier TemplateArgumentList
780 }
781
782 TemplateArgumentList {
783 "<" commaSep<
784 [@dynamicPrecedence=3] { TypeDescriptor } |
785 ParameterPackExpansion[@dynamicPrecedence=2] { TypeDescriptor "..." } |
786 [@dynamicPrecedence=1] { expression }
787 > (templateArgsEnd | templateArgsEndFallback)
788 }
789
790 ConceptDefinition {
791 kw<"concept"> Identifier "=" expression ";"
792 }
793
794 NamespaceDefinition {
795 kw<"inline">? kw<"namespace"> (Identifier ("::" kw<"inline">? Identifier)*)?
796 (CompoundStatement | "=" (Identifier | ScopedIdentifier) ";")
797 }
798
799 UsingDeclaration {
800 kw<"using"> (kw<"namespace"> | kw<"enum">)? (Identifier | ScopedIdentifier) ";"
801 }
802
803 AliasDeclaration {
804 kw<"using"> TypeIdentifier Attribute* "=" TypeDescriptor ";"
805 }
806
807 StaticAssertDeclaration {
808 kw<"static_assert"> "(" expression ("," (String | RawString | ConcatenatedString))? ")" ";"
809 }
810
811 ModuleDeclaration {
812 kw<"module"> (ModuleName Attribute* | ":" access)? ";"
813 }
814
815 ExportDeclaration {
816 kw<"export"> topLevelItem
817 }
818
819 ImportDeclaration {
820 kw<"import"> (ModuleName | HeaderName) Attribute* ";"
821 }
822
823 ModuleName {
824 Identifier ("." Identifier)* modulePartition? |
825 modulePartition
826 }
827
828 modulePartition { ":" PartitionName { identifier } }
829
830 ConditionClause {
831 "(" (
832 (Declaration | ExpressionStatement)? (expression | CommaExpression) |
833 Declaration { declarationSpecifiers declarator ("=" expression | InitializerList) }
834 ) ")"
835 }
836
837 ForRangeLoop {
838 kw<"for"> "("
839 (AliasDeclaration | TypeDefinition | Declaration | (expression | CommaExpression)? ";")?
840 declarationSpecifiers declarator ":" (expression | InitializerList) ")"
841 statement
842 }
843
844 ThrowStatement {
845 kw<"throw"> expression? ";"
846 }
847
848 TryStatement {
849 kw<"try"> CompoundStatement CatchClause+
850 }
851
852 CatchClause {
853 kw<"catch"> ParameterList CompoundStatement
854 }
855
856 Attribute {
857 "[[" commaSep<AttributeName AttributeArgs { "(" balancedTokens* ")" }?> "..."? "]]"
858 }
859
860 AttributeName { attributeIdentifier ("::" attributeIdentifier)* }
861
862 balancedTokens {
863 "{" balancedTokens* "}" |
864 "[" balancedTokens* "]" |
865 "(" balancedTokens* ")" |
866 attributeIdentifier |
867 incDec | ArithOp<"+" | "-"> | ArithOp { "*" } | ArithOp<"/" | "%"> | LogicOp { "||" | "&&" } |
868 BitOp<"|"> | BitOp<"^"> | BitOp { "&" } | CompareOp<"==" | "!="> | CompareOp<"<=" | ">=" | "<=>"> |
869 @extend[@name=CompareOp]<"<"> | @extend[@name=CompareOp]<templateArgsEnd, ">"> | BitOp<"<<" | ">>"> |
870 UpdateOp |
871 ";" | ":" | "," | "..." | "::" | "." | "->" |
872 Number | String | RawString | CharLiteral
873 }
874
875 NewExpression {
876 ("::" skw<"new"> | kw<"new">) ArgumentList? typeSpecifier (!more NewDeclarator)? (!arguments (ArgumentList | InitializerList))?
877 }
878
879 NewDeclarator {
880 "[" expression "]" (!more NewDeclarator)?
881 }
882
883 DeleteExpression {
884 ("::" skw<"delete"> | kw<"delete">) (!more "[" "]")? expression
885 }
886
887 LambdaExpression {
888 LambdaCaptureSpecifier (TemplateParameterList RequiresClause)? AbstractFunctionDeclarator? CompoundStatement
889 }
890
891 LambdaCaptureSpecifier {
892 "[" (
893 ("=" | "&") ("," commaSep1<expression>)? |
894 commaSep1<expression> |
895 ""
896 ) "]" !capture
897 }
898
899 ParameterPackExpansion {
900 expression !pack "..."
901 }
902
903 ScopedFieldIdentifier {
904 (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier | Decltype)? !namespace
905 "::" (FieldIdentifier { scopedIdentifier } | OperatorName<skw<"operator">> | DestructorName)
906 }
907
908 ScopedIdentifier {
909 (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? !namespace
910 "::" (Identifier { scopedIdentifier } | OperatorName<skw<"operator">> | DestructorName)
911 }
912
913 ScopedTypeIdentifier {
914 (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? !namespace
915 "::" TypeIdentifier { scopedIdentifier }
916 }
917
918 ScopedNamespaceIdentifier {
919 (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? !namespace
920 "::" NamespaceIdentifier { scopedIdentifier !namespace }
921 }
922
923 NamespaceIdentifier {
924 identifier !namespace
925 }
926
927 access { @specialize[@name=Access]<identifier, "public" | "private" | "protected"> }
928
929 True { @specialize<identifier, "TRUE" | "true"> }
930
931 False { @specialize<identifier, "FALSE" | "false"> }
932
933 primitiveType {
934 @specialize[@name=PrimitiveType]<identifier,
935 "bool" | "char" | "int" | "float" | "double" | "void" | "size_t" | "ssize_t" |
936 "intptr_t" | "uintptr_t" | "charptr_t" | "int8_t" | "int16_t" | "int32_t" |
937 "int64_t" | "uint8_t" | "uint16_t" | "uint32_t" | "uint64_t" | "char8_t" |
938 "char16_t" | "char32_t" | "char64_t">
939 }
940
941 kw<term> { @specialize[@name={term}]<identifier, term> }
942
943 skw<term> { @specialize[@name={term}]<scopedIdentifier, term> }
944
945 commaSep<term> { "" | term ("," term)* }
946
947 commaSep1<term> { term ("," term)* }
948
949 commaSepTrailing<term> { "" | term ("," term?)* }
950
951 @skip { whitespace | LineComment | BlockComment | PreprocDirective | Macro }
952
953 @external tokens rawString from "./tokens" { RawString }
954
955 @external tokens fallback from "./tokens" { templateArgsEndFallback[@name=">"], MacroName }
956
957 OperatorName<opKw> {
958 opKw (
959 UpdateOp | incDec | "=" |
960 ArithOp<$[*/%+\-]> |
961 BitOp<$[^~&|] | "<<" | ">>"> |
962 CompareOp<"<" ("=" ">"?)? | ">" "="? | $[!=] "="> |
963 LogicOp<"!" | "&&" | "||"> |
964 "->" (!arrowStar "*")? | "(" ")" | "[" "]" |
965 (kw<"new"> | kw<"delete">) (!newOpArray "[" "]")? |
966 kw<"co_await"> |
967 '""' Identifier
968 )
969 }
970
971 @tokens {
972 whitespace {
973 ($[ \t\r\n] | "\\" "\r"? "\n")+
974 }
975
976 pp[@name="#" {name}]<name> { "#" $[ \t]* name }
977
978 PreprocDirectiveName {
979 "#" $[ \t]* $[a-zA-Z] $[a-zA-Z0-9_]*
980 }
981
982 @precedence { pp, PreprocDirectiveName }
983
984 PreprocArg {
985 (![\n] | "\\" "\r"? "\n")+
986 }
987
988 ppSpace { $[ \t]+ }
989
990 @precedence { "(", PreprocArg, ppSpace }
991
992 Number {
993 ("0x" | "0b")?
994 (($[0-9]+ ("'" $[0-9]+)* |
995 "0b" $[0-9]+ ("'" $[0-9]+)* |
996 "0x" $[0-9a-fA-F]+ ("'" $[0-9a-fA-F]+)*)
997 ("." ($[0-9a-fA-F]+ ("'" $[0-9a-fA-F]+)*)?)? |
998 "." $[0-9]+ ("'" $[0-9]+)*)
999 ($[eEpP] ($[-\+]? $[0-9a-fA-F]+ ("'" $[0-9a-fA-F]+)*)?)? ("u" | "l" | "U" | "L" | "f" | "F")*
1000 }
1001
1002 @precedence { Number, "." }
1003
1004 EscapeSequence {
1005 "\\" (
1006 ![xuU] |
1007 $[0-9] $[0-9] $[0-9]? |
1008 "x" $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F]* |
1009 "u" $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] |
1010 "U" $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F]
1011 )
1012 }
1013
1014 SystemLibString[isolate] {
1015 "<" (![>\n\\] | "\\" _)* ">"
1016 }
1017
1018 identifier {
1019 $[a-zA-Z_] $[a-zA-Z0-9_]*
1020 }
1021
1022 scopedIdentifier { identifier }
1023
1024 attributeIdentifier[@name=Identifier] { identifier }
1025
1026 DestructorName {
1027 "~" $[ \t]* identifier
1028 }
1029
1030 @precedence { DestructorName, BitOp }
1031
1032 HeaderName { "<" ![>]+ ">" }
1033
1034 LineComment[isolate] { "//" ("\\" (![\n] | "\\r"? "\\n") | ![\n])* }
1035
1036 BlockComment[isolate] { "/*" blockCommentRest }
1037
1038 blockCommentRest { ![*] blockCommentRest | "*" blockCommentAfterStar }
1039
1040 blockCommentAfterStar { "/" | "*" blockCommentAfterStar | ![/*] blockCommentRest }
1041
1042 @precedence { LineComment, BlockComment, ArithOp }
1043
1044 ArithOp<expr> { expr }
1045
1046 BitOp<expr> { expr }
1047
1048 CompareOp<expr> { expr }
1049
1050 LogicOp<expr> { expr }
1051
1052 UpdateOp {
1053 ($[*/%+\-&^|] | "<<" | ">>") "="
1054 }
1055
1056 incDec[@name=UpdateOp] { "++" | "--" }
1057
1058 charLiteralStart { "L'" | "u'" | "U'" | "u8'" | "'" }
1059
1060 charLiteralEnd { "'" }
1061
1062 charLiteralContent { ![\\\n']+ }
1063
1064 stringLiteralStart { "L\"" | "u\"" | "U\"" | "u8\"" | "\"" }
1065
1066 stringLiteralEnd { "\"" }
1067
1068 stringLiteralContent { ![\\\n"]+ }
1069
1070 templateArgsEnd[@name=">"] { ">" }
1071
1072 "(" ")" "{" "}" "[" "]"
1073 "," "<" "::"
1074
1075 @precedence { stringLiteralStart, charLiteralStart, identifier }
1076 @precedence { stringLiteralStart, charLiteralStart, attributeIdentifier }
1077 }
1078
1079 @external propSource cppHighlighting from "./highlight"
1080
1081 @detectDelim