Debian Codegraph @

clang-tools-extra/pseudo/lib/cxx/cxx.bnf

raw
1 # This is a C++ grammar from the C++ standard [1].
2 #
3 # The grammar is a superset of the true grammar requring semantic constraints to
4 # resolve ambiguities. The grammar is context-free and ambiguous (beyond the
5 # limit of LR(k)). We use general parsing algorithm (e.g GLR) to handle the
6 # grammar and generate a transition table which is used to drive the parsing.
7 #
8 # It aims to align with the ISO C++ grammar as much as possible. We adjust it
9 # to fit the need for the grammar-based parser:
10 # - attributes are omitted, which will be handled as comments;
11 # - we don't allow nullable nonterminal symbols. There are few nullable
12 # nonterminals in the spec grammar, they are adjusted to be non-nullable;
13 # - the file merely describes the core C++ grammar. Preprocessor directives and
14 # lexical conversions are omitted as we reuse clang's lexer and run a fake
15 # preprocessor;
16 # - grammar rules with the >> token are adjusted, the greatergreater token is
17 # split into two > tokens, to make the GLR parser aware of nested templates
18 # and right shift operator;
19 #
20 # Guidelines:
21 # - nonterminals are lower_case; terminals (aka tokens) correspond to
22 # clang::TokenKind, written as "IDENTIFIER", "USING", "::" etc;
23 # - optional symbols are supported, with a _opt suffix;
24 #
25 # [1] https://isocpp.org/files/papers/N4860.pdf
26
27 # _ lists all the start-symbols which we support parsing.
28 #
29 # We list important nonterminals as start symbols, rather than doing it for all
30 # nonterminals by default, this reduces the number of states by 30% and LRTable
31 # actions by 16%.
32 _ := translation-unit EOF
33 _ := statement-seq EOF
34 _ := declaration-seq EOF
35
36 # gram.key
37 #! we don't distinguish between namespaces and namespace aliases, as it's hard
38 #! and uninteresting.
39 namespace-name := IDENTIFIER
40 template-name := IDENTIFIER
41
42 # gram.basic
43 #! Custom modifications to eliminate optional declaration-seq
44 translation-unit := declaration-seq
45 translation-unit := global-module-fragment_opt module-declaration declaration-seq_opt private-module-fragment_opt
46
47 # gram.expr
48 # expr.prim
49 primary-expression := literal
50 primary-expression := THIS
51 primary-expression := ( expression )
52 primary-expression := id-expression
53 primary-expression := lambda-expression
54 primary-expression := fold-expression
55 primary-expression := requires-expression
56 id-expression := unqualified-id
57 id-expression := qualified-id
58 unqualified-id := IDENTIFIER
59 unqualified-id := operator-function-id
60 unqualified-id := conversion-function-id
61 unqualified-id := literal-operator-id
62 unqualified-id := ~ type-name
63 unqualified-id := ~ decltype-specifier
64 unqualified-id := template-id
65 qualified-id := nested-name-specifier TEMPLATE_opt unqualified-id
66 nested-name-specifier := :: [guard]
67 nested-name-specifier := type-name ::
68 nested-name-specifier := namespace-name ::
69 nested-name-specifier := decltype-specifier ::
70 nested-name-specifier := nested-name-specifier IDENTIFIER ::
71 nested-name-specifier := nested-name-specifier TEMPLATE_opt simple-template-id ::
72 lambda-expression := lambda-introducer lambda-declarator_opt compound-statement
73 lambda-expression := lambda-introducer < template-parameter-list > requires-clause_opt lambda-declarator_opt compound-statement
74 #! We allow a capture-default to appear anywhere in a capture-list.
75 # This simplifies the grammar and error recovery.
76 lambda-introducer := [ capture-list_opt ]
77 lambda-declarator := ( parameter-declaration-clause_opt ) decl-specifier-seq_opt noexcept-specifier_opt trailing-return-type_opt requires-clause_opt
78 capture-list := capture
79 capture-list := capture-list , capture
80 capture := capture-default
81 capture := simple-capture
82 capture := init-capture
83 capture-default := &
84 capture-default := =
85 simple-capture := IDENTIFIER ..._opt
86 simple-capture := & IDENTIFIER ..._opt
87 simple-capture := THIS
88 simple-capture := * THIS
89 init-capture := ..._opt IDENTIFIER initializer
90 init-capture := & ..._opt IDENTIFIER initializer
91 fold-expression := ( cast-expression fold-operator ... )
92 fold-expression := ( ... fold-operator cast-expression )
93 fold-expression := ( cast-expression fold-operator ... fold-operator cast-expression )
94 fold-operator := +
95 fold-operator := -
96 fold-operator := *
97 fold-operator := /
98 fold-operator := %
99 fold-operator := ^
100 fold-operator := |
101 fold-operator := <<
102 fold-operator := greatergreater
103 fold-operator := +=
104 fold-operator := -=
105 fold-operator := *=
106 fold-operator := /=
107 fold-operator := %=
108 fold-operator := ^=
109 fold-operator := &=
110 fold-operator := |=
111 fold-operator := <<=
112 fold-operator := >>=
113 fold-operator := =
114 fold-operator := ==
115 fold-operator := !=
116 fold-operator := <
117 fold-operator := >
118 fold-operator := <=
119 fold-operator := >=
120 fold-operator := &&
121 fold-operator := ||
122 fold-operator := ,
123 fold-operator := .*
124 fold-operator := ->*
125 requires-expression := REQUIRES requirement-parameter-list_opt requirement-body
126 requirement-parameter-list := ( parameter-declaration-clause_opt )
127 requirement-body := { requirement-seq }
128 requirement-seq := requirement
129 requirement-seq := requirement-seq requirement
130 requirement := simple-requirement
131 requirement := type-requirement
132 requirement := compound-requirement
133 requirement := nested-requirement
134 simple-requirement := expression ;
135 type-requirement := TYPENAME nested-name-specifier_opt type-name ;
136 compound-requirement := { expression } NOEXCEPT_opt return-type-requirement_opt ;
137 return-type-requirement := -> type-constraint
138 nested-requirement := REQUIRES constraint-expression ;
139 # expr.post
140 postfix-expression := primary-expression
141 postfix-expression := postfix-expression [ expr-or-braced-init-list ]
142 postfix-expression := postfix-expression ( expression-list_opt )
143 postfix-expression := simple-type-specifier ( expression-list_opt )
144 postfix-expression := typename-specifier ( expression-list_opt )
145 postfix-expression := simple-type-specifier braced-init-list
146 postfix-expression := postfix-expression . TEMPLATE_opt id-expression
147 postfix-expression := postfix-expression -> TEMPLATE_opt id-expression
148 postfix-expression := postfix-expression ++
149 postfix-expression := postfix-expression --
150 postfix-expression := DYNAMIC_CAST < type-id > ( expression )
151 postfix-expression := STATIC_CAST < type-id > ( expression )
152 postfix-expression := REINTERPRET_CAST < type-id > ( expression )
153 postfix-expression := CONST_CAST < type-id > ( expression )
154 postfix-expression := TYPEID ( expression )
155 postfix-expression := TYPEID ( type-id )
156 #! Standard defines expression-list in terms of initializer-list, but our
157 # initializer-list allows designators.
158 expression-list := initializer-clause ..._opt
159 expression-list := expression-list , initializer-clause ..._opt
160 # expr.unary
161 unary-expression := postfix-expression
162 unary-expression := unary-operator cast-expression
163 unary-expression := ++ cast-expression
164 unary-expression := -- cast-expression
165 unary-expression := await-expression
166 unary-expression := SIZEOF unary-expression
167 unary-expression := SIZEOF ( type-id )
168 unary-expression := SIZEOF ... ( IDENTIFIER )
169 unary-expression := ALIGNOF ( type-id )
170 unary-expression := noexcept-expression
171 unary-expression := new-expression
172 unary-expression := delete-expression
173 unary-operator := *
174 unary-operator := &
175 unary-operator := +
176 unary-operator := -
177 unary-operator := !
178 unary-operator := ~
179 await-expression := CO_AWAIT cast-expression
180 noexcept-expression := NOEXCEPT ( expression )
181 new-expression := ::_opt NEW new-placement_opt new-type-id new-initializer_opt
182 new-expression := ::_opt NEW new-placement_opt ( type-id ) new-initializer_opt
183 new-placement := ( expression-list )
184 new-type-id := type-specifier-seq new-declarator_opt
185 new-declarator := ptr-operator new-declarator_opt
186 new-declarator := noptr-new-declarator
187 noptr-new-declarator := [ expression_opt ]
188 noptr-new-declarator := noptr-new-declarator [ constant-expression ]
189 new-initializer := ( expression-list_opt )
190 new-initializer := braced-init-list
191 delete-expression := ::_opt DELETE cast-expression
192 delete-expression := ::_opt DELETE [ ] cast-expression
193 cast-expression := unary-expression
194 cast-expression := ( type-id ) cast-expression
195 # expr.mptr.oper
196 pm-expression := cast-expression
197 pm-expression := pm-expression .* cast-expression
198 pm-expression := pm-expression ->* cast-expression
199 # expr.mul
200 multiplicative-expression := pm-expression
201 multiplicative-expression := multiplicative-expression * pm-expression
202 multiplicative-expression := multiplicative-expression / pm-expression
203 multiplicative-expression := multiplicative-expression % pm-expression
204 # expr.add
205 additive-expression := multiplicative-expression
206 additive-expression := additive-expression + multiplicative-expression
207 additive-expression := additive-expression - multiplicative-expression
208 # expr.shift
209 shift-expression := additive-expression
210 shift-expression := shift-expression << additive-expression
211 shift-expression := shift-expression greatergreater additive-expression
212 # expr.spaceship
213 compare-expression := shift-expression
214 compare-expression := compare-expression <=> shift-expression
215 # expr.rel
216 relational-expression := compare-expression
217 relational-expression := relational-expression < compare-expression
218 relational-expression := relational-expression > compare-expression
219 relational-expression := relational-expression <= compare-expression
220 relational-expression := relational-expression >= compare-expression
221 # expr.eq
222 equality-expression := relational-expression
223 equality-expression := equality-expression == relational-expression
224 equality-expression := equality-expression != relational-expression
225 # expr.bit.and
226 and-expression := equality-expression
227 and-expression := and-expression & equality-expression
228 # expr.xor
229 exclusive-or-expression := and-expression
230 exclusive-or-expression := exclusive-or-expression ^ and-expression
231 # expr.or
232 inclusive-or-expression := exclusive-or-expression
233 inclusive-or-expression := inclusive-or-expression | exclusive-or-expression
234 # expr.log.and
235 logical-and-expression := inclusive-or-expression
236 logical-and-expression := logical-and-expression && inclusive-or-expression
237 # expr.log.or
238 logical-or-expression := logical-and-expression
239 logical-or-expression := logical-or-expression || logical-and-expression
240 # expr.cond
241 conditional-expression := logical-or-expression
242 conditional-expression := logical-or-expression ? expression : assignment-expression
243 # expr.ass
244 yield-expression := CO_YIELD assignment-expression
245 yield-expression := CO_YIELD braced-init-list
246 throw-expression := THROW assignment-expression_opt
247 assignment-expression := conditional-expression
248 assignment-expression := yield-expression
249 assignment-expression := throw-expression
250 assignment-expression := logical-or-expression assignment-operator initializer-clause
251 assignment-operator := =
252 assignment-operator := *=
253 assignment-operator := /=
254 assignment-operator := %=
255 assignment-operator := +=
256 assignment-operator := -=
257 assignment-operator := >>=
258 assignment-operator := <<=
259 assignment-operator := &=
260 assignment-operator := ^=
261 assignment-operator := |=
262 # expr.comma
263 expression := assignment-expression
264 expression := expression , assignment-expression
265 # expr.const
266 constant-expression := conditional-expression
267
268 # gram.stmt
269 statement := labeled-statement
270 statement := expression-statement
271 statement := compound-statement
272 statement := selection-statement
273 statement := iteration-statement
274 statement := jump-statement
275 statement := declaration-statement
276 statement := try-block
277 init-statement := expression-statement
278 init-statement := simple-declaration
279 condition := expression
280 condition := decl-specifier-seq declarator brace-or-equal-initializer
281 labeled-statement := IDENTIFIER : statement
282 labeled-statement := CASE constant-expression : statement
283 labeled-statement := DEFAULT : statement
284 expression-statement := expression_opt ;
285 compound-statement := { statement-seq_opt [recover=Brackets] }
286 statement-seq := statement
287 statement-seq := statement-seq statement
288 selection-statement := IF CONSTEXPR_opt ( init-statement_opt condition ) statement [guard]
289 selection-statement := IF CONSTEXPR_opt ( init-statement_opt condition ) statement ELSE statement
290 selection-statement := SWITCH ( init-statement_opt condition ) statement
291 iteration-statement := WHILE ( condition ) statement
292 iteration-statement := DO statement WHILE ( expression ) ;
293 iteration-statement := FOR ( init-statement condition_opt ; expression_opt ) statement
294 iteration-statement := FOR ( init-statement_opt for-range-declaration : for-range-initializer ) statement
295 for-range-declaration := decl-specifier-seq declarator
296 for-range-declaration := decl-specifier-seq ref-qualifier_opt [ identifier-list ]
297 for-range-initializer := expr-or-braced-init-list
298 jump-statement := BREAK ;
299 jump-statement := CONTINUE ;
300 jump-statement := RETURN expr-or-braced-init-list_opt ;
301 jump-statement := coroutine-return-statement
302 jump-statement := GOTO IDENTIFIER ;
303 coroutine-return-statement := CO_RETURN expr-or-braced-init-list_opt ;
304 declaration-statement := block-declaration
305
306 # gram.dcl
307 declaration-seq := declaration
308 declaration-seq := declaration-seq declaration
309 declaration := block-declaration
310 declaration := nodeclspec-function-declaration
311 declaration := function-definition
312 declaration := template-declaration
313 declaration := deduction-guide
314 declaration := explicit-instantiation
315 declaration := explicit-specialization
316 declaration := export-declaration
317 declaration := linkage-specification
318 declaration := namespace-definition
319 declaration := empty-declaration
320 declaration := module-import-declaration
321 block-declaration := simple-declaration
322 block-declaration := asm-declaration
323 block-declaration := namespace-alias-definition
324 block-declaration := using-declaration
325 block-declaration := using-enum-declaration
326 block-declaration := using-directive
327 block-declaration := static_assert-declaration
328 block-declaration := alias-declaration
329 block-declaration := opaque-enum-declaration
330 nodeclspec-function-declaration := function-declarator ;
331 alias-declaration := USING IDENTIFIER = defining-type-id ;
332 simple-declaration := decl-specifier-seq init-declarator-list_opt ;
333 simple-declaration := decl-specifier-seq ref-qualifier_opt [ identifier-list ] initializer ; [guard]
334 static_assert-declaration := STATIC_ASSERT ( constant-expression ) ;
335 static_assert-declaration := STATIC_ASSERT ( constant-expression , string-literal ) ;
336 empty-declaration := ;
337 # dcl.spec
338 decl-specifier := storage-class-specifier
339 decl-specifier := defining-type-specifier
340 decl-specifier := function-specifier
341 decl-specifier := FRIEND
342 decl-specifier := TYPEDEF
343 decl-specifier := CONSTEXPR
344 decl-specifier := CONSTEVAL
345 decl-specifier := CONSTINIT
346 decl-specifier := INLINE
347 decl-specifier-seq := decl-specifier
348 decl-specifier-seq := decl-specifier decl-specifier-seq [guard]
349 storage-class-specifier := STATIC
350 storage-class-specifier := THREAD_LOCAL
351 storage-class-specifier := EXTERN
352 storage-class-specifier := MUTABLE
353 function-specifier := VIRTUAL
354 function-specifier := explicit-specifier
355 explicit-specifier := EXPLICIT ( constant-expression )
356 explicit-specifier := EXPLICIT
357 type-specifier := simple-type-specifier
358 type-specifier := elaborated-type-specifier
359 type-specifier := typename-specifier
360 type-specifier := cv-qualifier
361 type-specifier-seq := type-specifier
362 type-specifier-seq := type-specifier type-specifier-seq [guard]
363 defining-type-specifier := type-specifier
364 defining-type-specifier := class-specifier
365 defining-type-specifier := enum-specifier
366 defining-type-specifier-seq := defining-type-specifier
367 defining-type-specifier-seq := defining-type-specifier defining-type-specifier-seq [guard]
368 simple-type-specifier := nested-name-specifier_opt type-name
369 simple-type-specifier := nested-name-specifier TEMPLATE simple-template-id
370 simple-type-specifier := decltype-specifier
371 simple-type-specifier := placeholder-type-specifier
372 simple-type-specifier := nested-name-specifier_opt template-name
373 simple-type-specifier := SHORT
374 simple-type-specifier := LONG
375 simple-type-specifier := SIGNED
376 simple-type-specifier := UNSIGNED
377 simple-type-specifier := builtin-type
378 #! builtin-type added to aid in classifying which specifiers may combined.
379 builtin-type := CHAR
380 builtin-type := CHAR8_T
381 builtin-type := CHAR16_T
382 builtin-type := CHAR32_T
383 builtin-type := WCHAR_T
384 builtin-type := BOOL
385 builtin-type := INT
386 builtin-type := FLOAT
387 builtin-type := DOUBLE
388 builtin-type := VOID
389 #! Unlike C++ standard grammar, we don't distinguish the underlying type (class,
390 #! enum, typedef) of the IDENTIFIER, as these ambiguities are "local" and don't
391 #! affect the final parse tree. Eliminating them gives a significant performance
392 #! boost to the parser.
393 type-name := IDENTIFIER
394 type-name := simple-template-id
395 elaborated-type-specifier := class-key nested-name-specifier_opt IDENTIFIER
396 elaborated-type-specifier := class-key simple-template-id
397 elaborated-type-specifier := class-key nested-name-specifier TEMPLATE_opt simple-template-id
398 elaborated-type-specifier := elaborated-enum-specifier
399 elaborated-enum-specifier := ENUM nested-name-specifier_opt IDENTIFIER
400 decltype-specifier := DECLTYPE ( expression )
401 placeholder-type-specifier := type-constraint_opt AUTO
402 placeholder-type-specifier := type-constraint_opt DECLTYPE ( AUTO )
403 init-declarator-list := init-declarator
404 init-declarator-list := init-declarator-list , init-declarator
405 #! The standard grammar allows:
406 #! 1) an initializer with any declarator, including a function declarator, this
407 #! creates an ambiguity where a function definition is misparsed as a simple
408 #! declaration;
409 #! 2) an function-body with any declarator, includeing a non-function
410 #! declarator, this creates an ambiguity whwere a simple-declaration is
411 #! misparsed as a function-definition;
412 #! We extend the standard declarator to function-declarator and non-function-declarator
413 #! to eliminate these false parses.
414 init-declarator := non-function-declarator initializer_opt
415 init-declarator := function-declarator requires-clause_opt
416 function-declarator := declarator [guard]
417 non-function-declarator := declarator [guard]
418 declarator := ptr-declarator
419 declarator := noptr-declarator parameters-and-qualifiers trailing-return-type
420 ptr-declarator := noptr-declarator
421 ptr-declarator := ptr-operator ptr-declarator
422 noptr-declarator := declarator-id
423 noptr-declarator := noptr-declarator parameters-and-qualifiers
424 noptr-declarator := noptr-declarator [ constant-expression_opt ]
425 noptr-declarator := ( ptr-declarator )
426 parameters-and-qualifiers := ( parameter-declaration-clause_opt [recover=Brackets] ) cv-qualifier-seq_opt ref-qualifier_opt noexcept-specifier_opt
427 trailing-return-type := -> type-id
428 ptr-operator := * cv-qualifier-seq_opt
429 ptr-operator := &
430 ptr-operator := &&
431 ptr-operator := nested-name-specifier * cv-qualifier-seq_opt
432 cv-qualifier-seq := cv-qualifier cv-qualifier-seq_opt
433 cv-qualifier := CONST
434 cv-qualifier := VOLATILE
435 ref-qualifier := &
436 ref-qualifier := &&
437 declarator-id := ..._opt id-expression
438 type-id := type-specifier-seq abstract-declarator_opt
439 defining-type-id := defining-type-specifier-seq abstract-declarator_opt
440 abstract-declarator := ptr-abstract-declarator
441 abstract-declarator := noptr-abstract-declarator_opt parameters-and-qualifiers trailing-return-type
442 abstract-declarator := abstract-pack-declarator
443 ptr-abstract-declarator := noptr-abstract-declarator
444 ptr-abstract-declarator := ptr-operator ptr-abstract-declarator_opt
445 noptr-abstract-declarator := noptr-abstract-declarator_opt parameters-and-qualifiers
446 noptr-abstract-declarator := noptr-abstract-declarator_opt [ constant-expression_opt ]
447 noptr-abstract-declarator := ( ptr-abstract-declarator )
448 abstract-pack-declarator := noptr-abstract-pack-declarator
449 abstract-pack-declarator := ptr-operator abstract-pack-declarator
450 noptr-abstract-pack-declarator := noptr-abstract-pack-declarator parameters-and-qualifiers
451 noptr-abstract-pack-declarator := noptr-abstract-pack-declarator [ constant-expression_opt ]
452 noptr-abstract-pack-declarator := ...
453 #! Custom modifications to avoid nullable clause.
454 parameter-declaration-clause := parameter-declaration-list
455 parameter-declaration-clause := parameter-declaration-list_opt ...
456 parameter-declaration-clause := parameter-declaration-list , ...
457 parameter-declaration-list := parameter-declaration
458 parameter-declaration-list := parameter-declaration-list , parameter-declaration
459 parameter-declaration := decl-specifier-seq declarator
460 parameter-declaration := decl-specifier-seq declarator = initializer-clause
461 parameter-declaration := decl-specifier-seq abstract-declarator_opt
462 parameter-declaration := decl-specifier-seq abstract-declarator_opt = initializer-clause
463 # dcl.init
464 initializer := brace-or-equal-initializer
465 initializer := ( expression-list )
466 brace-or-equal-initializer := = initializer-clause
467 brace-or-equal-initializer := braced-init-list
468 initializer-clause := assignment-expression
469 initializer-clause := braced-init-list
470 #! Allow mixed designated/non-designated init-list.
471 # This is standard C, and accepted by clang and others as an extension.
472 # FIXME: Decouple recovery from is-there-a-trailing-comma!
473 braced-init-list := { initializer-list [recover=Brackets] }
474 braced-init-list := { initializer-list , }
475 braced-init-list := { }
476 initializer-list := initializer-list-item
477 initializer-list := initializer-list , initializer-list-item
478 initializer-list-item := initializer-clause ..._opt
479 initializer-list-item := designator brace-or-equal-initializer ..._opt
480 designator := . IDENTIFIER
481 #! Array designators are legal in C, and a common extension in C++.
482 designator := [ expression ]
483 expr-or-braced-init-list := expression
484 expr-or-braced-init-list := braced-init-list
485 # dcl.fct
486 function-definition := decl-specifier-seq_opt function-declarator virt-specifier-seq_opt function-body
487 function-definition := decl-specifier-seq_opt function-declarator requires-clause function-body
488 function-body := ctor-initializer_opt compound-statement
489 function-body := function-try-block
490 function-body := = DEFAULT ;
491 function-body := = DELETE ;
492 # dcl.enum
493 enum-specifier := enum-head { enumerator-list_opt }
494 enum-specifier := enum-head { enumerator-list , }
495 enum-head := enum-key enum-head-name_opt enum-base_opt
496 enum-head-name := nested-name-specifier_opt IDENTIFIER
497 opaque-enum-declaration := enum-key enum-head-name enum-base_opt ;
498 enum-key := ENUM
499 enum-key := ENUM CLASS
500 enum-key := ENUM STRUCT
501 enum-base := : type-specifier-seq
502 enumerator-list := enumerator-definition
503 enumerator-list := enumerator-list , enumerator-definition
504 enumerator-definition := enumerator
505 enumerator-definition := enumerator = constant-expression
506 enumerator := IDENTIFIER
507 using-enum-declaration := USING elaborated-enum-specifier ;
508 # basic.namespace
509 namespace-definition := named-namespace-definition
510 namespace-definition := unnamed-namespace-definition
511 namespace-definition := nested-namespace-definition
512 named-namespace-definition := INLINE_opt NAMESPACE IDENTIFIER { namespace-body_opt }
513 unnamed-namespace-definition := INLINE_opt NAMESPACE { namespace-body_opt }
514 nested-namespace-definition := NAMESPACE enclosing-namespace-specifier :: INLINE_opt IDENTIFIER { namespace-body }
515 enclosing-namespace-specifier := IDENTIFIER
516 enclosing-namespace-specifier := enclosing-namespace-specifier :: INLINE_opt IDENTIFIER
517 #! Custom modification to avoid nullable namespace-body.
518 namespace-body := declaration-seq
519 namespace-alias-definition := NAMESPACE IDENTIFIER = qualified-namespace-specifier ;
520 qualified-namespace-specifier := nested-name-specifier_opt namespace-name
521 using-directive := USING NAMESPACE nested-name-specifier_opt namespace-name ;
522 using-declaration := USING using-declarator-list ;
523 using-declarator-list := using-declarator ..._opt
524 using-declarator-list := using-declarator-list , using-declarator ..._opt
525 using-declarator := TYPENAME_opt nested-name-specifier unqualified-id
526 # dcl.asm
527 asm-declaration := ASM ( string-literal ) ;
528 # dcl.link
529 linkage-specification := EXTERN string-literal { declaration-seq_opt }
530 linkage-specification := EXTERN string-literal declaration
531
532 # gram.module
533 module-declaration := export-keyword_opt module-keyword module-name module-partition_opt ;
534 module-name := module-name-qualifier_opt IDENTIFIER
535 module-partition := : module-name-qualifier_opt IDENTIFIER
536 module-name-qualifier := IDENTIFIER .
537 module-name-qualifier := module-name-qualifier IDENTIFIER .
538 export-declaration := EXPORT declaration
539 export-declaration := EXPORT { declaration-seq_opt }
540 export-declaration := export-keyword module-import-declaration
541 module-import-declaration := import-keyword module-name ;
542 module-import-declaration := import-keyword module-partition ;
543 # FIXME: we don't have header-name in the grammar. Handle these in PP?
544 # module-import-declaration := import-keyword header-name ;
545 global-module-fragment := module-keyword ; declaration-seq_opt
546 private-module-fragment := module-keyword : PRIVATE ; declaration-seq_opt
547
548 # gram.class
549 class-specifier := class-head { member-specification_opt [recover=Brackets] }
550 class-head := class-key class-head-name class-virt-specifier_opt base-clause_opt
551 class-head := class-key base-clause_opt
552 class-head-name := nested-name-specifier_opt type-name
553 class-virt-specifier := contextual-final
554 class-key := CLASS
555 class-key := STRUCT
556 class-key := UNION
557 member-specification := member-declaration member-specification_opt
558 member-specification := access-specifier : member-specification_opt
559 member-declaration := decl-specifier-seq member-declarator-list_opt ;
560 member-declaration := member-declarator-list ;
561 member-declaration := function-definition
562 member-declaration := using-declaration
563 member-declaration := using-enum-declaration
564 member-declaration := static_assert-declaration
565 member-declaration := template-declaration
566 member-declaration := explicit-specialization
567 member-declaration := deduction-guide
568 member-declaration := alias-declaration
569 member-declaration := opaque-enum-declaration
570 member-declaration := empty-declaration
571 member-declarator-list := member-declarator
572 member-declarator-list := member-declarator-list , member-declarator
573 member-declarator := function-declarator virt-specifier-seq_opt pure-specifier_opt
574 member-declarator := function-declarator requires-clause
575 member-declarator := non-function-declarator brace-or-equal-initializer_opt
576 member-declarator := IDENTIFIER_opt : constant-expression brace-or-equal-initializer_opt
577 virt-specifier-seq := virt-specifier
578 virt-specifier-seq := virt-specifier-seq virt-specifier
579 virt-specifier := contextual-override
580 virt-specifier := contextual-final
581 pure-specifier := = contextual-zero
582 conversion-function-id := OPERATOR conversion-type-id
583 conversion-type-id := type-specifier-seq conversion-declarator_opt
584 conversion-declarator := ptr-operator conversion-declarator_opt
585 base-clause := : base-specifier-list
586 base-specifier-list := base-specifier ..._opt
587 base-specifier-list := base-specifier-list , base-specifier ..._opt
588 base-specifier := class-or-decltype
589 base-specifier := VIRTUAL access-specifier_opt class-or-decltype
590 base-specifier := access-specifier VIRTUAL_opt class-or-decltype
591 class-or-decltype := nested-name-specifier_opt type-name
592 class-or-decltype := nested-name-specifier TEMPLATE simple-template-id
593 class-or-decltype := decltype-specifier
594 access-specifier := PRIVATE
595 access-specifier := PROTECTED
596 access-specifier := PUBLIC
597 ctor-initializer := : mem-initializer-list
598 mem-initializer-list := mem-initializer ..._opt
599 mem-initializer-list := mem-initializer-list , mem-initializer ..._opt
600 mem-initializer := mem-initializer-id ( expression-list_opt )
601 mem-initializer := mem-initializer-id braced-init-list
602 mem-initializer-id := class-or-decltype
603 mem-initializer-id := IDENTIFIER
604
605 # gram.over
606 operator-function-id := OPERATOR operator-name
607 operator-name := NEW
608 operator-name := DELETE
609 operator-name := NEW [ ]
610 operator-name := DELETE [ ]
611 operator-name := CO_AWAIT
612 operator-name := ( )
613 operator-name := [ ]
614 operator-name := ->
615 operator-name := ->*
616 operator-name := ~
617 operator-name := !
618 operator-name := +
619 operator-name := -
620 operator-name := *
621 operator-name := /
622 operator-name := %
623 operator-name := ^
624 operator-name := &
625 operator-name := |
626 operator-name := =
627 operator-name := +=
628 operator-name := -=
629 operator-name := *=
630 operator-name := /=
631 operator-name := %=
632 operator-name := ^=
633 operator-name := &=
634 operator-name := |=
635 operator-name := ==
636 operator-name := !=
637 operator-name := <
638 operator-name := >
639 operator-name := <=
640 operator-name := >=
641 operator-name := <=>
642 operator-name := ^^
643 operator-name := ||
644 operator-name := <<
645 operator-name := greatergreater
646 operator-name := <<=
647 operator-name := >>=
648 operator-name := ++
649 operator-name := --
650 operator-name := ,
651 literal-operator-id := OPERATOR string-literal IDENTIFIER
652 literal-operator-id := OPERATOR user-defined-string-literal
653
654 # gram.temp
655 template-declaration := template-head declaration
656 template-declaration := template-head concept-definition
657 template-head := TEMPLATE < template-parameter-list > requires-clause_opt
658 template-parameter-list := template-parameter
659 template-parameter-list := template-parameter-list , template-parameter
660 requires-clause := REQUIRES constraint-logical-or-expression
661 constraint-logical-or-expression := constraint-logical-and-expression
662 constraint-logical-or-expression := constraint-logical-or-expression || constraint-logical-and-expression
663 constraint-logical-and-expression := primary-expression
664 constraint-logical-and-expression := constraint-logical-and-expression && primary-expression
665 template-parameter := type-parameter
666 template-parameter := parameter-declaration
667 type-parameter := type-parameter-key ..._opt IDENTIFIER_opt
668 type-parameter := type-parameter-key IDENTIFIER_opt = type-id
669 type-parameter := type-constraint ..._opt IDENTIFIER_opt
670 type-parameter := type-constraint IDENTIFIER_opt = type-id
671 type-parameter := template-head type-parameter-key ..._opt IDENTIFIER_opt
672 type-parameter := template-head type-parameter-key IDENTIFIER_opt = id-expression
673 type-parameter-key := CLASS
674 type-parameter-key := TYPENAME
675 type-constraint := nested-name-specifier_opt concept-name
676 type-constraint := nested-name-specifier_opt concept-name < template-argument-list_opt >
677 simple-template-id := template-name < template-argument-list_opt >
678 template-id := simple-template-id
679 template-id := operator-function-id < template-argument-list_opt >
680 template-id := literal-operator-id < template-argument-list_opt >
681 template-argument-list := template-argument ..._opt
682 template-argument-list := template-argument-list , template-argument ..._opt
683 template-argument := constant-expression
684 template-argument := type-id
685 template-argument := id-expression
686 constraint-expression := logical-or-expression
687 deduction-guide := explicit-specifier_opt template-name ( parameter-declaration-list_opt ) -> simple-template-id ;
688 concept-definition := CONCEPT concept-name = constraint-expression ;
689 concept-name := IDENTIFIER
690 typename-specifier := TYPENAME nested-name-specifier IDENTIFIER
691 typename-specifier := TYPENAME nested-name-specifier TEMPLATE_opt simple-template-id
692 explicit-instantiation := EXTERN_opt TEMPLATE declaration
693 explicit-specialization := TEMPLATE < > declaration
694
695 # gram.except
696 try-block := TRY compound-statement handler-seq
697 function-try-block := TRY ctor-initializer_opt compound-statement handler-seq
698 handler-seq := handler handler-seq_opt
699 handler := CATCH ( exception-declaration ) compound-statement
700 exception-declaration := type-specifier-seq declarator
701 exception-declaration := type-specifier-seq abstract-declarator_opt
702 noexcept-specifier := NOEXCEPT ( constant-expression )
703 noexcept-specifier := NOEXCEPT
704
705 # gram.cpp
706 identifier-list := IDENTIFIER
707 identifier-list := identifier-list , IDENTIFIER
708
709 # gram.lex
710 #! As we use clang lexer, most of lexical symbols are not needed, we only add
711 #! needed literals.
712 literal := integer-literal
713 literal := character-literal
714 literal := floating-point-literal
715 literal := string-literal
716 literal := boolean-literal
717 literal := pointer-literal
718 literal := user-defined-literal
719 integer-literal := NUMERIC_CONSTANT [guard]
720 character-literal := CHAR_CONSTANT [guard]
721 character-literal := WIDE_CHAR_CONSTANT [guard]
722 character-literal := UTF8_CHAR_CONSTANT [guard]
723 character-literal := UTF16_CHAR_CONSTANT [guard]
724 character-literal := UTF32_CHAR_CONSTANT [guard]
725 floating-point-literal := NUMERIC_CONSTANT [guard]
726 string-literal-chunk := STRING_LITERAL [guard]
727 string-literal-chunk := WIDE_STRING_LITERAL [guard]
728 string-literal-chunk := UTF8_STRING_LITERAL [guard]
729 string-literal-chunk := UTF16_STRING_LITERAL [guard]
730 string-literal-chunk := UTF32_STRING_LITERAL [guard]
731 #! Technically, string concatenation happens at phase 6 which is before parsing,
732 #! so it doesn't belong to the grammar. However, we extend the grammar to
733 #! support it, to make the pseudoparser fully functional on practical code.
734 string-literal := string-literal-chunk
735 string-literal := string-literal string-literal-chunk
736 user-defined-literal := user-defined-integer-literal
737 user-defined-literal := user-defined-floating-point-literal
738 user-defined-literal := user-defined-string-literal
739 user-defined-literal := user-defined-character-literal
740 user-defined-integer-literal := NUMERIC_CONSTANT [guard]
741 user-defined-string-literal-chunk := STRING_LITERAL [guard]
742 user-defined-string-literal-chunk := WIDE_STRING_LITERAL [guard]
743 user-defined-string-literal-chunk := UTF8_STRING_LITERAL [guard]
744 user-defined-string-literal-chunk := UTF16_STRING_LITERAL [guard]
745 user-defined-string-literal-chunk := UTF32_STRING_LITERAL [guard]
746 user-defined-string-literal := user-defined-string-literal-chunk
747 user-defined-string-literal := string-literal-chunk user-defined-string-literal
748 user-defined-string-literal := user-defined-string-literal string-literal-chunk
749 user-defined-floating-point-literal := NUMERIC_CONSTANT [guard]
750 user-defined-character-literal := CHAR_CONSTANT [guard]
751 user-defined-character-literal := WIDE_CHAR_CONSTANT [guard]
752 user-defined-character-literal := UTF8_CHAR_CONSTANT [guard]
753 user-defined-character-literal := UTF16_CHAR_CONSTANT [guard]
754 user-defined-character-literal := UTF32_CHAR_CONSTANT [guard]
755 boolean-literal := FALSE
756 boolean-literal := TRUE
757 pointer-literal := NULLPTR
758
759 #! Contextual keywords -- clang lexer always lexes them as identifier tokens.
760 #! Placeholders for literal text in the grammar that lex as other things.
761 contextual-override := IDENTIFIER [guard]
762 contextual-final := IDENTIFIER [guard]
763 contextual-zero := NUMERIC_CONSTANT [guard]
764 module-keyword := IDENTIFIER [guard]
765 import-keyword := IDENTIFIER [guard]
766 export-keyword := IDENTIFIER [guard]
767
768 #! greatergreater token -- clang lexer always lexes it as a single token, we
769 #! split it into two tokens to make the GLR parser aware of the nested-template
770 #! case.
771 greatergreater := > >
772
773 #! C++ predefined identifier, __func__ [dcl.fct.def.general] p8
774 #! FIXME: add other (MSVC, GNU extension) predefined identifiers.
775 primary-expression := predefined-expression
776 predefined-expression := __FUNC__