pax_global_header00006660000000000000000000000064145433273220014517gustar00rootroot0000000000000052 comment=8ec722cb66b81bfa754bdbbf906ba9065d62b69c cpp-1.1.2/000077500000000000000000000000001454332732200123025ustar00rootroot00000000000000cpp-1.1.2/.gitignore000066400000000000000000000000531454332732200142700ustar00rootroot00000000000000/node_modules/ /src/parser.* .tern-* /dist cpp-1.1.2/.npmignore000066400000000000000000000000161454332732200142760ustar00rootroot00000000000000/node_modules cpp-1.1.2/CHANGELOG.md000066400000000000000000000036651454332732200141250ustar00rootroot00000000000000## 1.1.2 (2023-12-28) ### Bug fixes Tag comments and strings as isolating for the purpose of bidirectional text. ## 1.1.1 (2023-07-03) ### Bug fixes Make the package work with new TS resolution styles. ## 1.1.0 (2023-02-08) ### New features Add support for a number of C++20 constructs. ## 1.0.0 (2022-06-06) ### New features First stable version. ## 0.16.0 (2022-04-20) ### Breaking changes Move to 0.16 serialized parser format. ## 0.15.3 (2022-03-28) ### Bug fixes Properly parse braced initializer lists in assignment expressions. ### New features The parser now includes syntax highlighting information in its node types. >>>>>>> 9081cc7 (Mark version 0.16.0) ## 0.15.2 (2022-01-24) ### Bug fixes Allow more explicit operator names, stop treating the 'operator' keyword as part of the operator name token. Allow comment after simple preprocessor directives. Support friend declarations in template declarations. Improve the parsing of some ambiguous cases in struct/enum/class specifiers. ## 0.15.1 (2022-01-21) ### Bug fixes Regenerate with \@lezer/generator 0.15.3 to fix a problem with parsing macros. ## 0.15.0 (2021-08-11) ### Breaking changes The module's name changed from `lezer-cpp` to `@lezer/cpp`. Upgrade to the 0.15.0 lezer interfaces. ## 0.13.2 (2021-03-23) ### Bug fixes Fix a crash that sometimes occurred during error-recovery around strings. ## 0.13.1 (2020-12-04) ### Bug fixes Fix versions of lezer packages depended on. ## 0.13.0 (2020-12-04) ## 0.12.0 (2020-10-23) ### Breaking changes Adjust to changed serialized parser format. ## 0.11.1 (2020-09-26) ### Bug fixes Fix lezer depencency versions ## 0.11.0 (2020-09-26) ### Breaking changes Follow change in serialized parser format. ## 0.10.1 (2020-09-10) ### Bug fixes Fix meaningless names for preprocessor directive tokens. Make `++`/`--` operators `UpdateOp` tokens. ## 0.10.0 (2020-09-02) ### Breaking changes First numbered release. cpp-1.1.2/LICENSE000066400000000000000000000021311454332732200133040ustar00rootroot00000000000000MIT License Copyright (C) 2020 by Marijn Haverbeke and others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cpp-1.1.2/README.md000066400000000000000000000010601454332732200135560ustar00rootroot00000000000000# @lezer/cpp This is a C++ grammar for the [Lezer](https://lezer.codemirror.net/) parser system. The grammar used is based in a large part on the corresponding [tree-sitter grammar](https://github.com/tree-sitter/tree-sitter-cpp). It should be noted that really parsing C++ without a symbol table and a preprocessor is not really something that is _possible_. The language is very, very ambiguous when parsed like that. This grammar tries to pick a likely parse, but is entirely capable of picking the wrong one. The code is licensed under an MIT license. cpp-1.1.2/dist/000077500000000000000000000000001454332732200132455ustar00rootroot00000000000000cpp-1.1.2/dist/index.d.cts000066400000000000000000000001021454332732200153020ustar00rootroot00000000000000import {LRParser} from "@lezer/lr" export const parser: LRParser cpp-1.1.2/dist/index.d.ts000066400000000000000000000001021454332732200151370ustar00rootroot00000000000000import {LRParser} from "@lezer/lr" export const parser: LRParser cpp-1.1.2/package.json000066400000000000000000000017301454332732200145710ustar00rootroot00000000000000{ "name": "@lezer/cpp", "version": "1.1.2", "description": "lezer-based C++ grammar", "main": "dist/index.cjs", "type": "module", "exports": { "import": "./dist/index.js", "require": "./dist/index.cjs" }, "module": "dist/index.js", "types": "dist/index.d.ts", "author": "Marijn Haverbeke ", "license": "MIT", "devDependencies": { "@lezer/generator": "^1.0.0", "mocha": "^10.2.0", "rollup": "^2.52.2", "@rollup/plugin-node-resolve": "^9.0.0" }, "dependencies": { "@lezer/common": "^1.2.0", "@lezer/highlight": "^1.0.0", "@lezer/lr": "^1.0.0" }, "repository": { "type" : "git", "url" : "https://github.com/lezer-parser/cpp.git" }, "scripts": { "build": "lezer-generator src/cpp.grammar -o src/parser && rollup -c", "build-debug": "lezer-generator src/cpp.grammar --names -o src/parser && rollup -c", "prepare": "npm run build", "test": "mocha test/test-*.js" } } cpp-1.1.2/rollup.config.js000066400000000000000000000004621454332732200154230ustar00rootroot00000000000000import {nodeResolve} from "@rollup/plugin-node-resolve" export default { input: "./src/parser.js", output: [{ format: "cjs", file: "./dist/index.cjs" }, { format: "es", file: "./dist/index.js" }], external(id) { return !/^[\.\/]/.test(id) }, plugins: [ nodeResolve() ] } cpp-1.1.2/src/000077500000000000000000000000001454332732200130715ustar00rootroot00000000000000cpp-1.1.2/src/cpp.grammar000066400000000000000000000633421454332732200152330ustar00rootroot00000000000000@precedence { capture subscript arguments typeSizeof placeholder baseClass suffix @left prefix @left pointer @left mult @left add @left bitShift @left kwExprExtra kwExpr greaterLess @left equal @left bitAnd @left bitXor @left bitOr @left logicAnd @left logicOr @left concat @left params array specifiers cast more arrowStar subscriptDesignator return typeIdentifier structName enumBase newOpArray namespace else @right caseLabel simpleTypeIdentifier qualifiers statement pack assign @right ternary @right absDecl } @top Program { topLevelItem* } topLevelItem { FunctionDefinition | LinkageSpecification | Declaration | ExportDeclaration | ImportDeclaration | statement | TypeDefinition | emptyDeclaration | NamespaceDefinition | ConceptDefinition | UsingDeclaration | AliasDeclaration | StaticAssertDeclaration | TemplateDeclaration | TemplateInstantiation | FunctionDefinition { constructorOrDestructorDefinition } | FunctionDefinition { operatorCastDefinition } | Declaration { operatorCastDeclaration } | ModuleDeclaration } @skip { ppSpace } { ppEnd { BlockComment* LineComment? "\n" } PreprocDirective { pp<"include"> (String | SystemLibString | Identifier ArgumentList?) ppEnd | pp<"define"> Identifier preprocParams? PreprocArg? "\n" | pp<"if"> PreprocArg "\n" | (pp<"ifdef"> | pp<"ifndef">) Identifier ppEnd | pp<"else"> ppEnd | pp<"endif"> ppEnd | pp<"elif"> PreprocArg "\n" | PreprocDirectiveName PreprocArg "\n" } } preprocParams { "(" commaSep ")" } FunctionDefinition { Attribute* msCallModifier? declarationSpecifiers declarator (CompoundStatement | TryStatement) } Declaration { Attribute* declarationSpecifiers commaSep1 ";" } TypeDefinition { kw<"typedef"> typeQualifier* typeSpecifier commaSep1 ";" } declarationSpecifiers { (storageClassSpecifier | typeQualifier | AttributeSpecifier | MsDeclspecModifier)* ~specifiers typeSpecifier ( storageClassSpecifier | typeQualifier | AttributeSpecifier | MsDeclspecModifier )* } LinkageSpecification { kw<"extern"> String (FunctionDefinition | Declaration | CompoundStatement) } AttributeSpecifier { kw<"__attribute__"> "(" ArgumentList ")" } MsDeclspecModifier { kw<"__declspec"> "(" Identifier ")" } MsBasedModifier { kw<"__based"> ArgumentList } msCallModifier { @specialize[@name=MsCallModifier] } msPointerModifier { @specialize[@name=MsPointerModifier] } declarator { PointerDeclarator[@dynamicPrecedence=1] { MsBasedModifier? "*" msPointerModifier* typeQualifier* declarator } | FunctionDeclarator | ArrayDeclarator { declarator !array "[" typeQualifier* (expression | "*")? "]" } | ParenthesizedDeclarator[@dynamicPrecedence=-10] { "(" declarator ")" } | Identifier ~identifier | ReferenceDeclarator[@dynamicPrecedence=1] { ("&" | "&&") declarator } | ScopedIdentifier ~identifier | TemplateFunction ~identifier | OperatorName> ~opDecl | DestructorName | StructuredBindingDeclarator[@dynamicPrecedence=-1] { "[" commaSep1 "]" } } functionSpecifiers { (typeQualifier | virtualSpecifier | "&" | "&&" | Noexcept | RequiresClause | ThrowSpecifier)* TrailingReturnType? } FunctionDeclarator { declarator !params ParameterList !specifiers functionSpecifiers AttributeSpecifier* (!specifiers )* } fieldDeclarator { PointerDeclarator[@dynamicPrecedence=1] { MsBasedModifier? "*" msPointerModifier* typeQualifier* fieldDeclarator } | FunctionDeclarator { fieldDeclarator !params ParameterList functionSpecifiers } | ArrayDeclarator { fieldDeclarator !array "[" typeQualifier* (expression | "*")? "]" } | ParenthesizedDeclarator[@dynamicPrecedence=-10] { "(" fieldDeclarator ")" } | FieldIdentifier | ReferenceDeclarator[@dynamicPrecedence=1] { ("&" | "&&") fieldDeclarator } | TemplateMethod | OperatorName> ~opDecl } typeDeclarator { PointerDeclarator[@dynamicPrecedence=1] { MsBasedModifier? "*" msPointerModifier* typeQualifier* typeDeclarator } | FunctionDeclarator { typeDeclarator !params ParameterList } | ArrayDeclarator { typeDeclarator !array "[" typeQualifier* (expression | "*")? "]" } | ParenthesizedDeclarator[@dynamicPrecedence=-10] { "(" typeDeclarator ")" } | TypeIdentifier } abstractDeclarator { AbstractPointerDeclarator[@dynamicPrecedence=1] { "*" (!qualifiers typeQualifier)* (!absDecl abstractDeclarator)? } | AbstractFunctionDeclarator | AbstractArrayDeclarator { abstractDeclarator? !array "[" typeQualifier* (expression | "*")? "]" } | AbstractParenthesizedDeclarator { "(" abstractDeclarator ")" } | AbstractReferenceDeclarator { ("&" | "&&") (!absDecl abstractDeclarator)? } } AbstractFunctionDeclarator { abstractDeclarator? !params ParameterList (typeQualifier | "&" | "&&" | Noexcept | ThrowSpecifier)* !return TrailingReturnType? } InitDeclarator { declarator "=" (InitializerList | expression) | declarator (!arguments ArgumentList | InitializerList) } CompoundStatement { !statement "{" topLevelItem* !statement "}" } storageClassSpecifier { kw<"extern"> | kw<"static"> | kw<"register"> | kw<"inline"> | kw<"thread_local"> } typeQualifier { kw<"const"> | kw<"volatile"> | kw<"restrict"> | kw<"_Atomic"> | kw<"mutable"> | kw<"constexpr"> | kw<"constinit"> | kw<"consteval"> } typeSpecifier[@isGroup=Type] { StructSpecifier { kw<"struct"> classSpecifier } | UnionSpecifier { kw<"union"> classSpecifier } | ClassSpecifier { kw<"class"> classSpecifier } | EnumSpecifier { kw<"enum"> (kw<"class"> | kw<"struct">)? (className (!enumBase enumBaseClause)? (!structName EnumeratorList)? | EnumeratorList) } | DependentType[@dynamicPrecedence=-1] { kw<"typename"> typeSpecifier } | Decltype | SizedTypeSpecifier | primitiveType ~afterPrimitive | kw<"auto"> | ScopedTypeIdentifier !simpleTypeIdentifier | TypeIdentifier !simpleTypeIdentifier | TemplateType | PlaceholderTypeSpecifier { typeSpecifier !placeholder (kw<"auto"> | Decltype) } } classSpecifier { MsDeclspecModifier? Attribute* (className | className? !structName virtualSpecifier? BaseClassClause? FieldDeclarationList) } Decltype { kw<"decltype"> "(" (expression | kw<"auto">) ")" } // FIXME this used to allow arbitrary TypeNames as well as primitive // types, which I guess might be useful with preprocessor-defined // types, but requires even more GLR magic. SizedTypeSpecifier { @specialize[@name=TypeSize]+ primitiveType? } EnumeratorList { "{" commaSepTrailing "}" } FieldDeclarationList { "{" fieldDeclarationListItem* "}" } fieldDeclarationListItem { FieldDeclaration { Attribute* kw<"virtual">? declarationSpecifiers (fieldDeclarator ("," fieldDeclarator)*)? (BitfieldClause | InitializerList | "=" (expression | InitializerList))? ";" } | TemplateDeclaration | FunctionDefinition { Attribute* kw<"virtual">? declarationSpecifiers fieldDeclarator (CompoundStatement | TryStatement | !more DefaultMethodClause | DeleteMethodClause) } | FunctionDefinition { constructorOrDestructorDefinition } | Declaration { (kw<"virtual"> | ExplicitFunctionSpecifier)? FunctionDeclarator ";" } | FunctionDefinition { operatorCastDefinition } | Declaration { operatorCastDeclaration } | FriendDeclaration | AccessSpecifier | AliasDeclaration | UsingDeclaration | TypeDefinition | StaticAssertDeclaration } BitfieldClause { ":" expression } Enumerator { Identifier ("=" expression)? } ParameterList { "(" ~argOrParam commaSep<(ParameterDeclaration | OptionalParameterDeclaration | VariadicParameterDeclaration | "..." ~argOrParam)> ")" } ParameterDeclaration { Attribute* declarationSpecifiers (declarator | abstractDeclarator)? } statement[@isGroup=Statement] { CaseStatement | nonCaseStatement | AttributeStatement | ForRangeLoop | TryStatement | ThrowStatement } nonCaseStatement { LabeledStatement | CompoundStatement | ExpressionStatement | IfStatement | SwitchStatement | DoStatement | WhileStatement | ForStatement | ReturnStatement | BreakStatement | ContinueStatement | GotoStatement | CoReturnStatement | CoYieldStatement } AttributeStatement { Attribute+ (CaseStatement | nonCaseStatement | ForRangeLoop | TryStatement) } LabeledStatement { StatementIdentifier ":" statement } ExpressionStatement { (expression | CommaExpression)? ";" } IfStatement { kw<"if"> kw<"constexpr">? ConditionClause statement (!else kw<"else"> statement)? } SwitchStatement { kw<"switch"> ConditionClause CompoundStatement } CaseStatement { (kw<"case"> expression | kw<"default">) ":" (!caseLabel (nonCaseStatement | AttributeStatement | Declaration | TypeDefinition))* } WhileStatement { kw<"while"> ConditionClause statement } DoStatement { kw<"do"> statement kw<"while"> ParenthesizedExpression ";" } ForStatement { kw<"for"> "(" (Declaration | (expression | CommaExpression)? ";") expression? ";" (expression | CommaExpression)? ")" statement } ReturnStatement { kw<"return"> (expression | CommaExpression)? ";" | kw<"return"> InitializerList ";" } BreakStatement { kw<"break"> ";" } ContinueStatement { kw<"continue"> ";" } GotoStatement { kw<"goto"> StatementIdentifier ";" } CoReturnStatement { kw<"co_return"> expression? ";" } CoYieldStatement { kw<"co_yield"> expression ";" } expression[@isGroup=Expression] { ConditionalExpression | AssignmentExpression | BinaryExpression | UnaryExpression | UpdateExpression | CastExpression | PointerExpression | SizeofExpression | CoAwaitExpression | RequiresExpression | SubscriptExpression | CallExpression | FieldExpression | CompoundLiteralExpression | Identifier ~identifier | Number | String | True | False | kw<"NULL"> | ConcatenatedString | CharLiteral | ParenthesizedExpression | TemplateFunction ~identifier | ScopedIdentifier ~identifier | OperatorName> ~opDecl | NewExpression | DeleteExpression | LambdaExpression | ParameterPackExpansion | kw<"nullptr"> | kw<"this"> | RawString | UserDefinedLiteral | ParamPack { "..." ~argOrParam } } CommaExpression { expression !more "," (expression | CommaExpression) } ConditionalExpression { expression !ternary "?" expression ":" expression } AssignmentExpression { (Identifier | CallExpression | FieldExpression | PointerExpression | SubscriptExpression | ParenthesizedExpression | ScopedNamespaceIdentifier) !assign (UpdateOp | "=") (expression | InitializerList) } PointerExpression { ("*" | "&") !pointer expression } UnaryExpression { (LogicOp<"!"> | ArithOp<"+" | "-"> | BitOp<"~">) !suffix expression } // Note: operators that conflict with other tokens are defined using // inline rules (so that the tokens can be shared), others directly using // ArithOp-style token helpers. BinaryExpression { expression !add ArithOp<"+" | "-"> expression | expression !mult (ArithOp { "*" } | ArithOp<"/" | "%">) expression | expression !logicAnd LogicOp { "&&" } expression | expression !logicOr LogicOp { "||" } expression | expression !bitOr BitOp<"|"> expression | expression !bitXor BitOp<"^"> expression | expression !bitAnd BitOp { "&" } expression | expression !equal CompareOp<"==" | "!="> expression | expression !greaterLess (CompareOp<"<=" | ">=" | "<=>"> | @extend[@name=CompareOp]<"<"> | @extend[@name=CompareOp]">) expression | expression !bitShift BitOp<"<<" | ">>"> expression | expression !concat incDec expression } UpdateExpression { incDec !prefix expression | expression !suffix incDec } CastExpression { "(" TypeDescriptor ")" expression } TypeDescriptor { typeQualifier* typeSpecifier typeQualifier* abstractDeclarator? } SizeofExpression { kw<"sizeof"> !kwExpr (expression | [@dynamicPrecedence=1] { "(" !typeSizeof TypeDescriptor ")" }) | kw<"sizeof"> "..." !kwExprExtra "(" Identifier ")" } CoAwaitExpression { kw<"co_await"> !kwExpr expression } RequiresExpression { kw<"requires"> !kwExpr ParameterList? RequirementList { "{" requirement* "}" } } requirement { SimpleRequirement { (expression | CommaExpression)? ";" } | TypeRequirement { kw<"typename"> className ";" } | CompoundRequirement { "{" expression "}" kw<"noexcept">? ReturnTypeRequirement? ";" } | ReturnTypeRequirement ";" | RequiresClause ";" } ReturnTypeRequirement { "->" expression } SubscriptExpression { expression !subscript "[" expression "]" } CallExpression { expression !arguments ArgumentList | primitiveType ~afterPrimitive ArgumentList } ArgumentList { "(" ~argOrParam commaSep<(expression | InitializerList)> ")" } FieldExpression { expression !subscript ("." | "->") FieldIdentifier | expression !subscript ("." | "->") (DestructorName | TemplateMethod) } CompoundLiteralExpression { "(" TypeDescriptor ")" InitializerList | (TypeIdentifier | TemplateType | ScopedTypeIdentifier) InitializerList } ParenthesizedExpression { "(" (expression | CommaExpression) ")" } InitializerList { "{" commaSepTrailing "}" } InitializerPair { (SubscriptDesignator | FieldDesignator)+ "=" (expression | InitializerList) } SubscriptDesignator { "[" expression !subscriptDesignator "]" } FieldDesignator { "." FieldIdentifier } @skip {} { CharLiteral { charLiteralStart (EscapeSequence | charLiteralContent) charLiteralEnd } String[isolate] { stringLiteralStart (stringLiteralContent | EscapeSequence)* stringLiteralEnd } Macro[@dynamicPrecedence=-10] { MacroName (whitespace? ArgumentList?) } } ConcatenatedString { (RawString | String) (RawString | String)+ } UserDefinedLiteral { (Number | CharLiteral | String | RawString | ConcatenatedString) Identifier } TypeIdentifier { identifier } FieldIdentifier { identifier } StatementIdentifier { identifier } Identifier { identifier } emptyDeclaration { Attribute* typeSpecifier ";" } className { (TypeIdentifier | ScopedTypeIdentifier | TemplateType) } virtualSpecifier { @specialize[@name=VirtualSpecifier] } ExplicitFunctionSpecifier { kw<"explicit"> | kw<"explicit"> !arguments "(" expression ")" } BaseClassClause { ":" Attribute* commaSep1<(access | kw<"virtual">)* className "..."?> } enumBaseClause { ":" (ScopedTypeIdentifier | TypeIdentifier | SizedTypeSpecifier | primitiveType) } TemplateDeclaration { kw<"template"> TemplateParameterList RequiresClause? ( emptyDeclaration | AliasDeclaration | Declaration | TemplateDeclaration | FriendDeclaration | FunctionDefinition | ConceptDefinition | FunctionDefinition { constructorOrDestructorDefinition } | FunctionDefinition { operatorCastDefinition } ) } RequiresClause { kw<"requires"> constraint } constraint { True | False | className | ParenthesizedExpression | LambdaExpression | RequiresExpression | ConstraintConjuction { constraint !logicAnd LogicOp { "&&" } constraint } | ConstraintDisjunction { constraint !logicOr LogicOp { "||" } constraint } } TemplateInstantiation { kw<"template"> declarationSpecifiers? declarator ";" } TemplateParameterList { "<" commaSep (templateArgsEnd | templateArgsEndFallback) } templateParameter { ParameterDeclaration | OptionalParameterDeclaration | TypeParameterDeclaration | VariadicParameterDeclaration | OptionalTypeParameterDeclaration | VariadicTypeParameterDeclaration | TemplateTemplateParameterDeclaration { kw<"template"> TemplateParameterList (TypeParameterDeclaration | VariadicTypeParameterDeclaration | OptionalTypeParameterDeclaration) } } TypeParameterDeclaration { (kw<"typename"> | kw<"class">) !typeIdentifier TypeIdentifier? !typeIdentifier } OptionalTypeParameterDeclaration { (kw<"typename"> | kw<"class">) TypeIdentifier? !typeIdentifier "=" typeSpecifier } VariadicTypeParameterDeclaration { (kw<"typename"> | kw<"class">) "..." !typeIdentifier TypeIdentifier? !typeIdentifier } OptionalParameterDeclaration { declarationSpecifiers declarator? "=" expression } VariadicParameterDeclaration { declarationSpecifiers ( VariadicDeclarator | ReferenceDeclarator { ("&&" | "&") VariadicDeclarator } ) } VariadicDeclarator { "..." Identifier? } OperatorCast { ((NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? "::" skw<"operator"> | kw<"operator">) declarationSpecifiers !cast abstractDeclarator } FieldInitializerList { ":" commaSep1 } FieldInitializer { (FieldIdentifier | ScopedFieldIdentifier) !arguments (InitializerList | ArgumentList) "..."? } operatorCastDefinition { (storageClassSpecifier | typeQualifier | AttributeSpecifier)* ~specifiers (kw<"virtual"> | ExplicitFunctionSpecifier)? OperatorCast (CompoundStatement | TryStatement | DefaultMethodClause | DeleteMethodClause) } operatorCastDeclaration { (kw<"virtual"> | ExplicitFunctionSpecifier)? OperatorCast ("=" expression)? ";" } constructorOrDestructorDefinition { (storageClassSpecifier | typeQualifier | AttributeSpecifier)* ~specifiers (kw<"virtual"> | ExplicitFunctionSpecifier)? FunctionDeclarator FieldInitializerList? (CompoundStatement | TryStatement | DefaultMethodClause | DeleteMethodClause) } DefaultMethodClause { "=" kw<"default"> ";" } DeleteMethodClause { "=" kw<"delete"> ";" } FriendDeclaration { kw<"friend"> (Declaration | FunctionDefinition | (kw<"class"> | kw<"struct"> | kw<"union">)? className ";") } AccessSpecifier { access ":" } TrailingReturnType { "->" typeQualifier? typeSpecifier (!absDecl abstractDeclarator)? } Noexcept { kw<"noexcept"> (!arguments "(" expression? ")")? } ThrowSpecifier { kw<"throw"> "(" commaSep ")" } TemplateType { (TypeIdentifier | ScopedTypeIdentifier) ~identifier TemplateArgumentList } TemplateMethod { (FieldIdentifier | ScopedFieldIdentifier) ~identifier TemplateArgumentList } TemplateFunction { (Identifier | ScopedIdentifier) ~identifier TemplateArgumentList } TemplateArgumentList { "<" commaSep< [@dynamicPrecedence=3] { TypeDescriptor } | ParameterPackExpansion[@dynamicPrecedence=2] { TypeDescriptor "..." } | [@dynamicPrecedence=1] { expression } > (templateArgsEnd | templateArgsEndFallback) } ConceptDefinition { kw<"concept"> Identifier "=" expression ";" } NamespaceDefinition { kw<"inline">? kw<"namespace"> (Identifier ("::" kw<"inline">? Identifier)*)? (CompoundStatement | "=" (Identifier | ScopedIdentifier) ";") } UsingDeclaration { kw<"using"> (kw<"namespace"> | kw<"enum">)? (Identifier | ScopedIdentifier) ";" } AliasDeclaration { kw<"using"> TypeIdentifier Attribute* "=" TypeDescriptor ";" } StaticAssertDeclaration { kw<"static_assert"> "(" expression ("," (String | RawString | ConcatenatedString))? ")" ";" } ModuleDeclaration { kw<"module"> (ModuleName Attribute* | ":" access)? ";" } ExportDeclaration { kw<"export"> topLevelItem } ImportDeclaration { kw<"import"> (ModuleName | HeaderName) Attribute* ";" } ModuleName { Identifier ("." Identifier)* modulePartition? | modulePartition } modulePartition { ":" PartitionName { identifier } } ConditionClause { "(" ( (Declaration | ExpressionStatement)? (expression | CommaExpression) | Declaration { declarationSpecifiers declarator ("=" expression | InitializerList) } ) ")" } ForRangeLoop { kw<"for"> "(" (AliasDeclaration | TypeDefinition | Declaration | (expression | CommaExpression)? ";")? declarationSpecifiers declarator ":" (expression | InitializerList) ")" statement } ThrowStatement { kw<"throw"> expression? ";" } TryStatement { kw<"try"> CompoundStatement CatchClause+ } CatchClause { kw<"catch"> ParameterList CompoundStatement } Attribute { "[[" commaSep "..."? "]]" } AttributeName { attributeIdentifier ("::" attributeIdentifier)* } balancedTokens { "{" balancedTokens* "}" | "[" balancedTokens* "]" | "(" balancedTokens* ")" | attributeIdentifier | incDec | ArithOp<"+" | "-"> | ArithOp { "*" } | ArithOp<"/" | "%"> | LogicOp { "||" | "&&" } | BitOp<"|"> | BitOp<"^"> | BitOp { "&" } | CompareOp<"==" | "!="> | CompareOp<"<=" | ">=" | "<=>"> | @extend[@name=CompareOp]<"<"> | @extend[@name=CompareOp]"> | BitOp<"<<" | ">>"> | UpdateOp | ";" | ":" | "," | "..." | "::" | "." | "->" | Number | String | RawString | CharLiteral } NewExpression { ("::" skw<"new"> | kw<"new">) ArgumentList? typeSpecifier (!more NewDeclarator)? (!arguments (ArgumentList | InitializerList))? } NewDeclarator { "[" expression "]" (!more NewDeclarator)? } DeleteExpression { ("::" skw<"delete"> | kw<"delete">) (!more "[" "]")? expression } LambdaExpression { LambdaCaptureSpecifier (TemplateParameterList RequiresClause)? AbstractFunctionDeclarator? CompoundStatement } LambdaCaptureSpecifier { "[" ( ("=" | "&") ("," commaSep1)? | commaSep1 | "" ) "]" !capture } ParameterPackExpansion { expression !pack "..." } ScopedFieldIdentifier { (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier | Decltype)? !namespace "::" (FieldIdentifier { scopedIdentifier } | OperatorName> | DestructorName) } ScopedIdentifier { (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? !namespace "::" (Identifier { scopedIdentifier } | OperatorName> | DestructorName) } ScopedTypeIdentifier { (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? !namespace "::" TypeIdentifier { scopedIdentifier } } ScopedNamespaceIdentifier { (NamespaceIdentifier | TemplateType | ScopedNamespaceIdentifier)? !namespace "::" NamespaceIdentifier { scopedIdentifier !namespace } } NamespaceIdentifier { identifier !namespace } access { @specialize[@name=Access] } True { @specialize } False { @specialize } primitiveType { @specialize[@name=PrimitiveType] } kw { @specialize[@name={term}] } skw { @specialize[@name={term}] } commaSep { "" | term ("," term)* } commaSep1 { term ("," term)* } commaSepTrailing { "" | term ("," term?)* } @skip { whitespace | LineComment | BlockComment | PreprocDirective | Macro } @external tokens rawString from "./tokens" { RawString } @external tokens fallback from "./tokens" { templateArgsEndFallback[@name=">"], MacroName } OperatorName { opKw ( UpdateOp | incDec | "=" | ArithOp<$[*/%+\-]> | BitOp<$[^~&|] | "<<" | ">>"> | CompareOp<"<" ("=" ">"?)? | ">" "="? | $[!=] "="> | LogicOp<"!" | "&&" | "||"> | "->" (!arrowStar "*")? | "(" ")" | "[" "]" | (kw<"new"> | kw<"delete">) (!newOpArray "[" "]")? | kw<"co_await"> | '""' Identifier ) } @tokens { whitespace { ($[ \t\r\n] | "\\" "\r"? "\n")+ } pp[@name="#" {name}] { "#" $[ \t]* name } PreprocDirectiveName { "#" $[ \t]* $[a-zA-Z] $[a-zA-Z0-9_]* } @precedence { pp, PreprocDirectiveName } PreprocArg { (![\n] | "\\" "\r"? "\n")+ } ppSpace { $[ \t]+ } @precedence { "(", PreprocArg, ppSpace } Number { ("0x" | "0b")? (($[0-9]+ ("'" $[0-9]+)* | "0b" $[0-9]+ ("'" $[0-9]+)* | "0x" $[0-9a-fA-F]+ ("'" $[0-9a-fA-F]+)*) ("." ($[0-9a-fA-F]+ ("'" $[0-9a-fA-F]+)*)?)? | "." $[0-9]+ ("'" $[0-9]+)*) ($[eEpP] ($[-\+]? $[0-9a-fA-F]+ ("'" $[0-9a-fA-F]+)*)?)? ("u" | "l" | "U" | "L" | "f" | "F")* } @precedence { Number, "." } EscapeSequence { "\\" ( ![xuU] | $[0-9] $[0-9] $[0-9]? | "x" $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F]* | "u" $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] $[0-9a-fA-F] | "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] ) } SystemLibString[isolate] { "<" (![>\n\\] | "\\" _)* ">" } identifier { $[a-zA-Z_] $[a-zA-Z0-9_]* } scopedIdentifier { identifier } attributeIdentifier[@name=Identifier] { identifier } DestructorName { "~" $[ \t]* identifier } @precedence { DestructorName, BitOp } HeaderName { "<" ![>]+ ">" } LineComment[isolate] { "//" ("\\" (![\n] | "\\r"? "\\n") | ![\n])* } BlockComment[isolate] { "/*" blockCommentRest } blockCommentRest { ![*] blockCommentRest | "*" blockCommentAfterStar } blockCommentAfterStar { "/" | "*" blockCommentAfterStar | ![/*] blockCommentRest } @precedence { LineComment, BlockComment, ArithOp } ArithOp { expr } BitOp { expr } CompareOp { expr } LogicOp { expr } UpdateOp { ($[*/%+\-&^|] | "<<" | ">>") "=" } incDec[@name=UpdateOp] { "++" | "--" } charLiteralStart { "L'" | "u'" | "U'" | "u8'" | "'" } charLiteralEnd { "'" } charLiteralContent { ![\\\n']+ } stringLiteralStart { "L\"" | "u\"" | "U\"" | "u8\"" | "\"" } stringLiteralEnd { "\"" } stringLiteralContent { ![\\\n"]+ } templateArgsEnd[@name=">"] { ">" } "(" ")" "{" "}" "[" "]" "," "<" "::" @precedence { stringLiteralStart, charLiteralStart, identifier } @precedence { stringLiteralStart, charLiteralStart, attributeIdentifier } } @external propSource cppHighlighting from "./highlight" cpp-1.1.2/src/highlight.js000066400000000000000000000042201454332732200153740ustar00rootroot00000000000000import {styleTags, tags as t} from "@lezer/highlight" export const cppHighlighting = styleTags({ "typedef struct union enum class typename decltype auto template operator friend noexcept namespace using requires concept import export module __attribute__ __declspec __based": t.definitionKeyword, "extern MsCallModifier MsPointerModifier extern static register thread_local inline const volatile restrict _Atomic mutable constexpr constinit consteval virtual explicit VirtualSpecifier Access": t.modifier, "if else switch for while do case default return break continue goto throw try catch": t.controlKeyword, "co_return co_yield co_await": t.controlKeyword, "new sizeof delete static_assert": t.operatorKeyword, "NULL nullptr": t.null, this: t.self, "True False": t.bool, "TypeSize PrimitiveType": t.standard(t.typeName), TypeIdentifier: t.typeName, FieldIdentifier: t.propertyName, "CallExpression/FieldExpression/FieldIdentifier": t.function(t.propertyName), "ModuleName/Identifier": t.namespace, "PartitionName": t.labelName, StatementIdentifier: t.labelName, "Identifier DestructorName": t.variableName, "CallExpression/Identifier": t.function(t.variableName), "CallExpression/ScopedIdentifier/Identifier": t.function(t.variableName), "FunctionDeclarator/Identifier FunctionDeclarator/DestructorName": t.function(t.definition(t.variableName)), NamespaceIdentifier: t.namespace, OperatorName: t.operator, ArithOp: t.arithmeticOperator, LogicOp: t.logicOperator, BitOp: t.bitwiseOperator, CompareOp: t.compareOperator, AssignOp: t.definitionOperator, UpdateOp: t.updateOperator, LineComment: t.lineComment, BlockComment: t.blockComment, Number: t.number, String: t.string, "RawString SystemLibString": t.special(t.string), CharLiteral: t.character, EscapeSequence: t.escape, "UserDefinedLiteral/Identifier": t.literal, PreProcArg: t.meta, "PreprocDirectiveName #include #ifdef #ifndef #if #define #else #endif #elif": t.processingInstruction, MacroName: t.special(t.name), "( )": t.paren, "[ ]": t.squareBracket, "{ }": t.brace, "< >": t.angleBracket, ". ->": t.derefOperator, ", ;": t.separator }) cpp-1.1.2/src/tokens.js000066400000000000000000000041301454332732200147300ustar00rootroot00000000000000import {ExternalTokenizer} from "@lezer/lr" import {RawString, templateArgsEndFallback, MacroName} from "./parser.terms.js" const R = 82, L = 76, u = 117, U = 85, a = 97, z = 122, A = 65, Z = 90, Underscore = 95, Zero = 48, Quote = 34, ParenL = 40, ParenR = 41, Space = 32, Newline = 10, GreaterThan = 62 export const rawString = new ExternalTokenizer(input => { // Raw string literals can start with: R, LR, uR, UR, u8R if (input.next == L || input.next == U) { input.advance() } else if (input.next == u) { input.advance() if (input.next == Zero + 8) input.advance() } if (input.next != R) return input.advance() if (input.next != Quote) return input.advance() let marker = "" while (input.next != ParenL) { if (input.next == Space || input.next <= 13 || input.next == ParenR) return marker += String.fromCharCode(input.next) input.advance() } input.advance() for (;;) { if (input.next < 0) return input.acceptToken(RawString) if (input.next == ParenR) { let match = true for (let i = 0; match && i < marker.length; i++) if (input.peek(i + 1) != marker.charCodeAt(i)) match = false if (match && input.peek(marker.length + 1) == Quote) return input.acceptToken(RawString, 2 + marker.length) } input.advance() } }) export const fallback = new ExternalTokenizer(input => { if (input.next == GreaterThan) { // Provide a template-args-closing token when the next characters // are ">>", in which case the regular tokenizer will only see a // bit shift op. if (input.peek(1) == GreaterThan) input.acceptToken(templateArgsEndFallback, 1) } else { // Notice all-uppercase identifiers let sawLetter = false, i = 0 for (;; i++) { if (input.next >= A && input.next <= Z) sawLetter = true else if (input.next >= a && input.next <= z) return else if (input.next != Underscore && !(input.next >= Zero && input.next <= Zero + 9)) break input.advance() } if (sawLetter && i > 1) input.acceptToken(MacroName) } }, {extend: true}) cpp-1.1.2/test/000077500000000000000000000000001454332732200132615ustar00rootroot00000000000000cpp-1.1.2/test/ambiguities.txt000066400000000000000000000042711454332732200163300ustar00rootroot00000000000000# template functions vs relational expressions T1 a = b < c > d; T2 e = f(g); int a = std::get<0>(t); ==> Program( Declaration( TypeIdentifier, InitDeclarator( Identifier, BinaryExpression( BinaryExpression(Identifier, CompareOp, Identifier), CompareOp, Identifier))), Declaration( TypeIdentifier, InitDeclarator( Identifier, CallExpression( TemplateFunction(Identifier, TemplateArgumentList( TypeDescriptor(TypeIdentifier))), ArgumentList(Identifier)))), Declaration( PrimitiveType, InitDeclarator( Identifier, CallExpression( TemplateFunction( ScopedIdentifier(NamespaceIdentifier, Identifier), TemplateArgumentList(Number)), ArgumentList(Identifier))))) # function declarations vs variable initializations // Function declarations T1 a(T2 *b); T3 c(T4 &d, T5 &&e); // Variable declarations with initializers T7 f(g.h); T6 i{j}; ==> Program( LineComment, Declaration( TypeIdentifier, FunctionDeclarator( Identifier, ParameterList(ParameterDeclaration(TypeIdentifier, PointerDeclarator(Identifier))))), Declaration( TypeIdentifier, FunctionDeclarator( Identifier, ParameterList( ParameterDeclaration(TypeIdentifier, ReferenceDeclarator(Identifier)), ParameterDeclaration(TypeIdentifier, ReferenceDeclarator(Identifier))))), LineComment, Declaration( TypeIdentifier, InitDeclarator( Identifier, ArgumentList(FieldExpression(Identifier, FieldIdentifier)))), Declaration( TypeIdentifier, InitDeclarator( Identifier, InitializerList(Identifier)))) # template classes vs relational expressions int main() { T1 v1; T1 v2 = v3; } ==> Program(FunctionDefinition( PrimitiveType, FunctionDeclarator(Identifier, ParameterList), CompoundStatement( Declaration( TemplateType(TypeIdentifier, TemplateArgumentList(TypeDescriptor(TypeIdentifier))), Identifier), Declaration( TemplateType(TypeIdentifier, TemplateArgumentList(TypeDescriptor(TypeIdentifier))), InitDeclarator(Identifier, Identifier))))) cpp-1.1.2/test/cpp20.txt000066400000000000000000000676471454332732200147720ustar00rootroot00000000000000# Concept definition template concept Derived = std::is_base_of::value; ==> Program(TemplateDeclaration( template, TemplateParameterList(TypeParameterDeclaration(class,TypeIdentifier),TypeParameterDeclaration(class,TypeIdentifier)), ConceptDefinition(concept,Identifier,ScopedIdentifier( TemplateType( ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier), TemplateArgumentList(TypeDescriptor(TypeIdentifier),TypeDescriptor(TypeIdentifier))), Identifier)))) # Concept definition with requires expression template concept Hashable = requires(T a) { { std::hash{}(a) } -> std::convertible_to; }; ==> Program(TemplateDeclaration( template,TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), ConceptDefinition(concept,Identifier, RequiresExpression(requires,ParameterList(ParameterDeclaration(TypeIdentifier,Identifier)),RequirementList( CompoundRequirement( CallExpression(CompoundLiteralExpression(TemplateType(ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier), TemplateArgumentList(TypeDescriptor(TypeIdentifier))),InitializerList),ArgumentList(Identifier)), ReturnTypeRequirement(TemplateFunction(ScopedIdentifier(NamespaceIdentifier,Identifier), TemplateArgumentList(TypeDescriptor(ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier))))))))))) # Requires clauses and expressions template void f(T&&) requires Eq; // can appear as the last element of a function declarator template requires Addable // or right after a template parameter list T add(T a, T b) { return a + b; } template concept Addable = requires (T x) { x + x; }; // requires-expression template requires requires (T x) { x + x; } // ad-hoc constraint, note keyword used twice T add(T a, T b) { return a + b; } template requires (!std::is_same_v) // parenthesized expressions are allowed void f(T); template requires Addable && Subtractable // conjunctions T f(T); template requires Addable || Subtractable // disjunctions T f(T); template requires false || true // boolean literals T f(T); template requires (... && Addable) // fold expressions T f(T); ==> Program( TemplateDeclaration(template,TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), Declaration(PrimitiveType,FunctionDeclarator(Identifier, ParameterList(ParameterDeclaration(TypeIdentifier,AbstractReferenceDeclarator)), RequiresClause(requires,TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))))))), LineComment, TemplateDeclaration( template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier)))), LineComment, FunctionDefinition(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(TypeIdentifier,Identifier), ParameterDeclaration(TypeIdentifier,Identifier))), CompoundStatement(ReturnStatement(return,BinaryExpression(Identifier,ArithOp,Identifier))))), TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), ConceptDefinition(concept,Identifier, RequiresExpression(requires,ParameterList(ParameterDeclaration(TypeIdentifier,Identifier)), RequirementList(SimpleRequirement(BinaryExpression(Identifier,ArithOp,Identifier)))))), LineComment, TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,RequiresExpression(requires, ParameterList(ParameterDeclaration(TypeIdentifier,Identifier)), RequirementList(SimpleRequirement(BinaryExpression(Identifier,ArithOp,Identifier))))), LineComment, FunctionDefinition(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(TypeIdentifier,Identifier), ParameterDeclaration(TypeIdentifier,Identifier))), CompoundStatement(ReturnStatement(return,BinaryExpression(Identifier,ArithOp,Identifier))))), TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,ParenthesizedExpression(UnaryExpression(LogicOp,TemplateFunction( ScopedIdentifier(NamespaceIdentifier,Identifier), TemplateArgumentList(TypeDescriptor(TypeIdentifier),TypeDescriptor(PrimitiveType)))))), LineComment, Declaration(PrimitiveType,FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier))))), TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,ConstraintConjuction( TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))), LogicOp, TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))))), LineComment, Declaration(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier))))), TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,ConstraintDisjunction( TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))), LogicOp, TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))))), LineComment, Declaration(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier))))), TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,ConstraintDisjunction(False,LogicOp,True)), LineComment, Declaration(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier))))), TemplateDeclaration(template, TemplateParameterList(VariadicTypeParameterDeclaration(typename,TypeIdentifier)), RequiresClause(requires,ParenthesizedExpression(BinaryExpression( ParamPack, LogicOp, TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier)))))), LineComment, Declaration(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier)))))) # Compound requirements template concept C2 = requires(T x) { {*x} -> std::convertible_to; // the expression *x must be valid // AND the type T::inner must be valid // AND the result of *x must be convertible to T::inner {x + 1} -> std::same_as; // the expression x + 1 must be valid // AND std::same_as must be satisfied // i.e., (x + 1) must be a prvalue of type int {x * 1} -> std::convertible_to; // the expression x * 1 must be valid // AND its result must be convertible to T }; ==> Program( TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(typename,TypeIdentifier)), ConceptDefinition(concept,Identifier,RequiresExpression( requires, ParameterList(ParameterDeclaration(TypeIdentifier,Identifier)), RequirementList(CompoundRequirement( PointerExpression(Identifier), ReturnTypeRequirement(TemplateFunction( ScopedIdentifier(NamespaceIdentifier,Identifier), TemplateArgumentList( TypeDescriptor(DependentType(typename,ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier))))))), LineComment, LineComment, LineComment, CompoundRequirement( BinaryExpression(Identifier,ArithOp,Number), ReturnTypeRequirement(TemplateFunction(ScopedIdentifier(NamespaceIdentifier,Identifier), TemplateArgumentList(TypeDescriptor(PrimitiveType))))), LineComment, LineComment, LineComment, CompoundRequirement( BinaryExpression(Identifier,ArithOp,Number), ReturnTypeRequirement(TemplateFunction(ScopedIdentifier(NamespaceIdentifier,Identifier), TemplateArgumentList(TypeDescriptor(TypeIdentifier))))), LineComment, LineComment))))) # Nested requirements template concept Semiregular = DefaultConstructible && CopyConstructible && Destructible && CopyAssignable && requires(T a, size_t n) { requires Same; // nested: { a.~T() } noexcept; // compound: is a valid expression that doesn't throw requires Same; // nested: requires Same; // nested { delete new T }; // compound { delete new T[n] }; // compound }; ==> Program(TemplateDeclaration(template, TemplateParameterList(TypeParameterDeclaration(class,TypeIdentifier)), ConceptDefinition(concept,Identifier,BinaryExpression( BinaryExpression( BinaryExpression( BinaryExpression( TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))), LogicOp, TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier)))), LogicOp, TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier)))), LogicOp, TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier)))), LogicOp, RequiresExpression(requires, ParameterList(ParameterDeclaration(TypeIdentifier,Identifier),ParameterDeclaration(PrimitiveType,Identifier)), RequirementList( RequiresClause(requires,TemplateType(TypeIdentifier,TemplateArgumentList( TypeDescriptor(TypeIdentifier,AbstractPointerDeclarator), TypeDescriptor(Decltype(decltype,PointerExpression(Identifier)))))), LineComment, CompoundRequirement(CallExpression(FieldExpression(Identifier,DestructorName),ArgumentList),noexcept), LineComment, RequiresClause(requires,TemplateType(TypeIdentifier,TemplateArgumentList( TypeDescriptor(TypeIdentifier,AbstractPointerDeclarator), TypeDescriptor(Decltype(decltype,NewExpression(new,TypeIdentifier)))))), LineComment, RequiresClause(requires,TemplateType(TypeIdentifier,TemplateArgumentList( TypeDescriptor(TypeIdentifier,AbstractPointerDeclarator), TypeDescriptor(Decltype(decltype,NewExpression(new,TypeIdentifier,NewDeclarator(Identifier))))))), LineComment, CompoundRequirement(DeleteExpression(delete,NewExpression(new,TypeIdentifier))), LineComment, CompoundRequirement(DeleteExpression(delete,NewExpression(new,TypeIdentifier,NewDeclarator(Identifier)))), LineComment)))))) # Constraints template void f(const T&); // constrained function template declaration void f(const EqualityComparable auto&); // constrained function template declaration Sortable auto foo = f(); Sortable auto bar = g(); NS::Concept auto baz = h(); Sortable decltype(auto) foo = i(); ==> Program( TemplateDeclaration(template, TemplateParameterList(ParameterDeclaration(TypeIdentifier,Identifier)), Declaration(PrimitiveType,FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration( const,TypeIdentifier,AbstractReferenceDeclarator))))), LineComment, Declaration(PrimitiveType,FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(const,PlaceholderTypeSpecifier(TypeIdentifier,auto),AbstractReferenceDeclarator)))), LineComment, Declaration( PlaceholderTypeSpecifier(TypeIdentifier,auto),InitDeclarator(Identifier,CallExpression(Identifier,ArgumentList))), Declaration( PlaceholderTypeSpecifier(TemplateType(TypeIdentifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))),auto), InitDeclarator(Identifier,CallExpression(Identifier,ArgumentList))), Declaration( PlaceholderTypeSpecifier(TemplateType(ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier),TemplateArgumentList(TypeDescriptor(TypeIdentifier))),auto), InitDeclarator(Identifier,CallExpression(Identifier,ArgumentList))), Declaration( PlaceholderTypeSpecifier(TypeIdentifier,Decltype(decltype,auto)), InitDeclarator(Identifier,CallExpression(Identifier,ArgumentList)))) # Namespace definitions namespace std { int x; } // namespace std namespace A::B { } namespace A::B::inline C::D { } ==> Program( NamespaceDefinition(namespace,Identifier,CompoundStatement(Declaration(PrimitiveType,Identifier))), LineComment, NamespaceDefinition(namespace,Identifier,Identifier,CompoundStatement), NamespaceDefinition(namespace,Identifier,Identifier,inline,Identifier,Identifier,CompoundStatement)) # Namespace alias definitions namespace A = B; namespace C = ::D; namespace fs = std::filesystem; namespace bfs = ::boost::filesystem; namespace literals = std::chono::literals; ==> Program( NamespaceDefinition(namespace,Identifier,Identifier), NamespaceDefinition(namespace,Identifier,ScopedIdentifier(Identifier)), NamespaceDefinition(namespace,Identifier,ScopedIdentifier(NamespaceIdentifier,Identifier)), NamespaceDefinition(namespace,Identifier,ScopedIdentifier(ScopedNamespaceIdentifier(NamespaceIdentifier),Identifier)), NamespaceDefinition(namespace,Identifier,ScopedIdentifier( ScopedNamespaceIdentifier(NamespaceIdentifier,NamespaceIdentifier),Identifier))) # Reference declarations int main() { T &x = y(); } ==> Program(FunctionDefinition(PrimitiveType,FunctionDeclarator(Identifier,ParameterList),CompoundStatement( Declaration(TypeIdentifier,InitDeclarator(ReferenceDeclarator(Identifier),CallExpression( TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier,AbstractReferenceDeclarator))), ArgumentList)))))) # R-value reference declarations int main(T &&); int main(T &&t) { const U &&u = v; } ==> Program( Declaration(PrimitiveType, FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier,AbstractReferenceDeclarator)))), FunctionDefinition(PrimitiveType, FunctionDeclarator(Identifier,ParameterList(ParameterDeclaration(TypeIdentifier,ReferenceDeclarator(Identifier)))), CompoundStatement(Declaration(const,TypeIdentifier,InitDeclarator(ReferenceDeclarator(Identifier),Identifier))))) # Function-try-block definitions void foo() try {} catch(...) {} ==> Program(FunctionDefinition(PrimitiveType,FunctionDeclarator(Identifier,ParameterList),TryStatement( try,CompoundStatement,CatchClause(catch,ParameterList,CompoundStatement)))) # Inline try method definitions struct S { int getF2() const try { throw 1; } catch (...) { return f; } }; ==> Program(StructSpecifier(struct,TypeIdentifier,FieldDeclarationList( FunctionDefinition(PrimitiveType, FunctionDeclarator(FieldIdentifier,ParameterList,const), TryStatement(try, CompoundStatement(ThrowStatement(throw,Number)), CatchClause(catch,ParameterList,CompoundStatement(ReturnStatement(return,Identifier)))))))) # Classes with inheritance class F : [[deprecated]] public G {}; class H : public virtual I {}; class J : virtual protected I {}; ==> Program( ClassSpecifier(class,TypeIdentifier,BaseClassClause(Attribute(AttributeName(Identifier)),Access,TypeIdentifier), FieldDeclarationList), ClassSpecifier(class,TypeIdentifier,BaseClassClause(Access,virtual,TypeIdentifier),FieldDeclarationList), ClassSpecifier(class,TypeIdentifier,BaseClassClause(virtual,Access,TypeIdentifier),FieldDeclarationList)) # Attributes on declarations [[aaa]] int f() { } class [[gnu::visibility("default")]] A {}; struct [[gnu::visibility("default")]] A {}; union [[gnu::visibility("default")]] A {}; class [[gnu::visibility("default")]] [[deprecated]] A {}; class A final : [[deprecated]] public B {}; ==> Program( FunctionDefinition(Attribute(AttributeName(Identifier)),PrimitiveType, FunctionDeclarator(Identifier,ParameterList),CompoundStatement), ClassSpecifier(class,Attribute(AttributeName(Identifier,Identifier),AttributeArgs(String)),TypeIdentifier, FieldDeclarationList), StructSpecifier(struct,Attribute(AttributeName(Identifier,Identifier),AttributeArgs(String)),TypeIdentifier, FieldDeclarationList), UnionSpecifier(union,Attribute(AttributeName(Identifier,Identifier),AttributeArgs(String)),TypeIdentifier, FieldDeclarationList), ClassSpecifier(class, Attribute(AttributeName(Identifier,Identifier),AttributeArgs(String)), Attribute(AttributeName(Identifier)), TypeIdentifier,FieldDeclarationList), ClassSpecifier(class,TypeIdentifier,VirtualSpecifier, BaseClassClause(Attribute(AttributeName(Identifier)),Access,TypeIdentifier), FieldDeclarationList)) # Constinit/consteval declarations consteval int answer(); constinit int life = answer(); ==> Program( Declaration(consteval,PrimitiveType,FunctionDeclarator(Identifier,ParameterList)), Declaration(constinit,PrimitiveType,InitDeclarator(Identifier,CallExpression(Identifier,ArgumentList)))) # Ref-qualifiers class C { void f() &; void f() && noexcept; void f() & {} void f() & noexcept {} }; void C::f() &; void C::f() & noexcept; void C::f() && {} void C::f() & noexcept {} ==> Program( ClassSpecifier(class,TypeIdentifier,FieldDeclarationList( FieldDeclaration(PrimitiveType,FunctionDeclarator(FieldIdentifier,ParameterList)), FieldDeclaration(PrimitiveType,FunctionDeclarator(FieldIdentifier,ParameterList,Noexcept(noexcept))), FunctionDefinition(PrimitiveType,FunctionDeclarator(FieldIdentifier,ParameterList),CompoundStatement), FunctionDefinition(PrimitiveType,FunctionDeclarator(FieldIdentifier,ParameterList,Noexcept(noexcept)), CompoundStatement))), Declaration(PrimitiveType,FunctionDeclarator(ScopedIdentifier(NamespaceIdentifier,Identifier),ParameterList)), Declaration(PrimitiveType, FunctionDeclarator(ScopedIdentifier(NamespaceIdentifier,Identifier),ParameterList,Noexcept(noexcept))), FunctionDefinition(PrimitiveType, FunctionDeclarator(ScopedIdentifier(NamespaceIdentifier,Identifier),ParameterList),CompoundStatement), FunctionDefinition(PrimitiveType, FunctionDeclarator(ScopedIdentifier(NamespaceIdentifier,Identifier),ParameterList,Noexcept(noexcept)),CompoundStatement)) # Default comparison declarations struct A { auto operator<=>(A const &) = default; }; ==> Program(StructSpecifier(struct,TypeIdentifier,FieldDeclarationList( FunctionDefinition(auto,FunctionDeclarator( OperatorName(operator,CompareOp), ParameterList(ParameterDeclaration(TypeIdentifier,const,AbstractReferenceDeclarator))), DefaultMethodClause(default))))) # Parameter pack expansions container t1; container t2; typedef Tuple...> type; f(&args...); // expands to f(&E1, &E2, &E3) f(n, ++args...); // expands to f(n, ++E1, ++E2, ++E3); f(++args..., n); // expands to f(++E1, ++E2, ++E3, n); f(const_cast(&args)...); // f(const_cast(&X1), const_cast(&X2), const_cast(&X3)) f(h(args...) + args...); // expands to f(h(E1,E2,E3) + E1, h(E1,E2,E3) + E2, h(E1,E2,E3) + E3) const int size = sizeof...(args) + 2; int res[size] = {1,args...,2}; int dummy[sizeof...(Ts)] = { (std::cout << args, 0)... }; auto lm = [&, args...] { return g(args...); }; class X : public Mixins... { public: X(const Mixins&... mixins) : Mixins(mixins)... { } }; template void wrap(Args&&... args) { f(forward(args)...); } void f(T...) {} ==> Program( Declaration(TemplateType(TypeIdentifier,TemplateArgumentList( TypeDescriptor(TypeIdentifier), TypeDescriptor(TypeIdentifier), ParameterPackExpansion(TypeDescriptor(TypeIdentifier)) )),Identifier), Declaration(TemplateType(TypeIdentifier,TemplateArgumentList( ParameterPackExpansion(TypeDescriptor(TypeIdentifier)), TypeDescriptor(TypeIdentifier), TypeDescriptor(TypeIdentifier) )),Identifier), TypeDefinition(typedef,TemplateType(TypeIdentifier,TemplateArgumentList( ParameterPackExpansion(TypeDescriptor(TemplateType(TypeIdentifier,TemplateArgumentList( TypeDescriptor(TypeIdentifier),TypeDescriptor(TypeIdentifier))))))),TypeIdentifier), ExpressionStatement(CallExpression(Identifier,ArgumentList(ParameterPackExpansion(PointerExpression(Identifier))))), LineComment, ExpressionStatement(CallExpression(Identifier,ArgumentList( Identifier, ParameterPackExpansion(UpdateExpression(UpdateOp,Identifier))))), LineComment, ExpressionStatement(CallExpression(Identifier,ArgumentList( ParameterPackExpansion(UpdateExpression(UpdateOp,Identifier)),Identifier))), LineComment, ExpressionStatement(CallExpression(Identifier,ArgumentList( ParameterPackExpansion(CallExpression(TemplateFunction(Identifier,TemplateArgumentList( TypeDescriptor(const,TypeIdentifier,AbstractPointerDeclarator) )),ArgumentList(PointerExpression(Identifier))))))), LineComment, ExpressionStatement(CallExpression(Identifier,ArgumentList(ParameterPackExpansion(BinaryExpression( CallExpression(Identifier,ArgumentList(ParameterPackExpansion(Identifier))), ArithOp, Identifier))))), LineComment, Declaration(const,PrimitiveType,InitDeclarator(Identifier,BinaryExpression( SizeofExpression(sizeof,Identifier), ArithOp, Number))), Declaration(PrimitiveType, InitDeclarator(ArrayDeclarator(Identifier,Identifier),InitializerList(Number,ParameterPackExpansion(Identifier),Number))), Declaration(PrimitiveType,InitDeclarator( ArrayDeclarator(Identifier,SizeofExpression(sizeof,Identifier)), InitializerList(ParameterPackExpansion(ParenthesizedExpression(CommaExpression( BinaryExpression(ScopedIdentifier(NamespaceIdentifier,Identifier),BitOp,Identifier), Number)))))), Declaration(auto,InitDeclarator(Identifier,LambdaExpression( LambdaCaptureSpecifier(ParameterPackExpansion(Identifier)), CompoundStatement(ReturnStatement(return,CallExpression(Identifier,ArgumentList(ParameterPackExpansion(Identifier)))))))), ClassSpecifier(class,TypeIdentifier,BaseClassClause(Access,TypeIdentifier),FieldDeclarationList( AccessSpecifier(Access), FunctionDefinition(FunctionDeclarator(Identifier,ParameterList( VariadicParameterDeclaration(const,TypeIdentifier,ReferenceDeclarator(VariadicDeclarator(Identifier)))) ),FieldInitializerList(FieldInitializer(FieldIdentifier,ArgumentList(Identifier))),CompoundStatement))), TemplateDeclaration(template, TemplateParameterList(VariadicTypeParameterDeclaration(typename,TypeIdentifier)), FunctionDefinition(PrimitiveType,FunctionDeclarator(Identifier,ParameterList( VariadicParameterDeclaration(TypeIdentifier,ReferenceDeclarator(VariadicDeclarator(Identifier))) )),CompoundStatement( ExpressionStatement(CallExpression(Identifier,ArgumentList( ParameterPackExpansion(CallExpression(TemplateFunction(Identifier,TemplateArgumentList( TypeDescriptor(TypeIdentifier) )),ArgumentList(Identifier))))))))), FunctionDefinition(PrimitiveType, FunctionDeclarator(Identifier,ParameterList(VariadicParameterDeclaration(TypeIdentifier,VariadicDeclarator))), CompoundStatement)) # User-defined literals 1.2_km; "foo" "bar"_baz; ==> Program( ExpressionStatement(UserDefinedLiteral(Number,Identifier)), ExpressionStatement(UserDefinedLiteral(ConcatenatedString(String,String),Identifier))) # Coroutines co_await fn() || co_await var; co_return 1; co_return; co_yield 1; ==> Program( ExpressionStatement(BinaryExpression( CoAwaitExpression(co_await,CallExpression(Identifier,ArgumentList)), LogicOp, CoAwaitExpression(co_await,Identifier))), CoReturnStatement(co_return,Number), CoReturnStatement(co_return), CoYieldStatement(co_yield,Number)) # Fold Expressions bool t = (... + IndexOf); bool t2 = (IndexOf + ...); bool t3 = (1 + ... + IndexOf); bool t3 = (IndexOf + ... + 1); ==> Program( Declaration(PrimitiveType,InitDeclarator(Identifier,ParenthesizedExpression(BinaryExpression( ParamPack, ArithOp, TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))))))), Declaration(PrimitiveType,InitDeclarator(Identifier,ParenthesizedExpression(BinaryExpression( TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))), ArithOp, ParamPack)))), Declaration(PrimitiveType,InitDeclarator(Identifier,ParenthesizedExpression(BinaryExpression( BinaryExpression(Number,ArithOp,ParamPack), ArithOp, TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))))))), Declaration(PrimitiveType,InitDeclarator(Identifier,ParenthesizedExpression(BinaryExpression( BinaryExpression(TemplateFunction(Identifier,TemplateArgumentList(TypeDescriptor(TypeIdentifier))),ArithOp,ParamPack), ArithOp, Number))))) # Range-based for loops T main() { for (auto n = v.size(); auto i : v) { cout << --n + i << ' '; } for (using elem_t = T::value_type; elem_t i : v) { cout << --n + i << ' '; } } ==> Program(FunctionDefinition(TypeIdentifier,FunctionDeclarator(Identifier,ParameterList),CompoundStatement( ForRangeLoop(for, Declaration(auto,InitDeclarator(Identifier,CallExpression(FieldExpression(Identifier,FieldIdentifier),ArgumentList))), auto,Identifier,Identifier, CompoundStatement( ExpressionStatement(BinaryExpression( BinaryExpression(Identifier,BitOp,BinaryExpression(UpdateExpression(UpdateOp,Identifier),ArithOp,Identifier)), BitOp, CharLiteral)))), ForRangeLoop(for, AliasDeclaration(using,TypeIdentifier,TypeDescriptor(ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier))), TypeIdentifier,Identifier,Identifier, CompoundStatement( ExpressionStatement(BinaryExpression( BinaryExpression(Identifier,BitOp,BinaryExpression(UpdateExpression(UpdateOp,Identifier),ArithOp,Identifier)), BitOp, CharLiteral))))))) # Attributes void f() { [[a]] switch (b) { [[c]] case 1: {} } [[a]] while (true) {} [[a]] if (true) {} [[a]] for (auto x : y) {} [[a]] for (;;) {} [[a]] return; [[a]] a; [[a]]; [[a]] label: {} [[a]] goto label; } ==> Program(FunctionDefinition(PrimitiveType,FunctionDeclarator(Identifier,ParameterList),CompoundStatement( AttributeStatement( Attribute(AttributeName(Identifier)), SwitchStatement(switch,ConditionClause(Identifier),CompoundStatement( AttributeStatement(Attribute(AttributeName(Identifier)),CaseStatement(case,Number,CompoundStatement))))), AttributeStatement(Attribute(AttributeName(Identifier)),WhileStatement(while,ConditionClause(True),CompoundStatement)), AttributeStatement(Attribute(AttributeName(Identifier)),IfStatement(if,ConditionClause(True),CompoundStatement)), AttributeStatement(Attribute(AttributeName(Identifier)),ForRangeLoop(for,auto,Identifier,Identifier,CompoundStatement)), AttributeStatement(Attribute(AttributeName(Identifier)),ForStatement(for,CompoundStatement)), AttributeStatement(Attribute(AttributeName(Identifier)),ReturnStatement(return)), AttributeStatement(Attribute(AttributeName(Identifier)),ExpressionStatement(Identifier)), AttributeStatement(Attribute(AttributeName(Identifier)),ExpressionStatement), AttributeStatement(Attribute(AttributeName(Identifier)),LabeledStatement(StatementIdentifier,CompoundStatement)), AttributeStatement(Attribute(AttributeName(Identifier)),GotoStatement(goto,StatementIdentifier))))) # Modules export module helloworld; module a.b; module : private; import a:C; import ; export void hello() {} export import :B; ==> Program( ExportDeclaration(export,ModuleDeclaration(module,ModuleName(Identifier))), ModuleDeclaration(module,ModuleName(Identifier,Identifier)), ModuleDeclaration(module,Access), ImportDeclaration(import,ModuleName(Identifier,PartitionName)), ImportDeclaration(import,HeaderName), ExportDeclaration(export,FunctionDefinition(PrimitiveType,FunctionDeclarator(Identifier,ParameterList),CompoundStatement)), ExportDeclaration(export,ImportDeclaration(import,ModuleName(PartitionName)))) cpp-1.1.2/test/declarations.txt000066400000000000000000000675131454332732200165060ustar00rootroot00000000000000# Namespace definitions namespace std { int x; } // namespace std ==> Program( NamespaceDefinition(namespace, Identifier, CompoundStatement( Declaration( PrimitiveType, Identifier))), LineComment) # Using declarations using a; using ::b; using c::d; using ::e::f::g; using h = i::j; using namespace std; template using a = typename b::c; ==> Program( UsingDeclaration(using, Identifier), UsingDeclaration(using, ScopedIdentifier(Identifier)), UsingDeclaration(using, ScopedIdentifier(NamespaceIdentifier, Identifier)), UsingDeclaration(using, ScopedIdentifier( ScopedNamespaceIdentifier( ScopedNamespaceIdentifier(NamespaceIdentifier), NamespaceIdentifier), Identifier)), AliasDeclaration(using, TypeIdentifier, TypeDescriptor(ScopedTypeIdentifier(NamespaceIdentifier, TypeIdentifier))), UsingDeclaration(using, namespace, Identifier), TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier)), AliasDeclaration(using, TypeIdentifier, TypeDescriptor( DependentType( typename, ScopedTypeIdentifier( TemplateType(TypeIdentifier, TemplateArgumentList(TypeDescriptor(TypeIdentifier))), TypeIdentifier)))))) # Reference declarations int main() { T &x = y(); } ==> Program( FunctionDefinition( PrimitiveType, FunctionDeclarator(Identifier, ParameterList), CompoundStatement( Declaration( TypeIdentifier, InitDeclarator( ReferenceDeclarator(Identifier), CallExpression( TemplateFunction( Identifier, TemplateArgumentList( TypeDescriptor(TypeIdentifier, AbstractReferenceDeclarator))), ArgumentList)))))) # R-value reference declarations int main(T &&); int main(T &&t) { const U &&u = v; } ==> Program( Declaration( PrimitiveType, FunctionDeclarator( Identifier, ParameterList(ParameterDeclaration(TypeIdentifier, AbstractReferenceDeclarator)))), FunctionDefinition( PrimitiveType, FunctionDeclarator( Identifier, ParameterList(ParameterDeclaration(TypeIdentifier, ReferenceDeclarator(Identifier)))), CompoundStatement( Declaration( const, TypeIdentifier, InitDeclarator(ReferenceDeclarator(Identifier), Identifier))))) # Inline method definitions struct S { int f; S() : f(0) {} private: int getF() const { return f; } }; ==> Program( StructSpecifier(struct, TypeIdentifier, FieldDeclarationList( FieldDeclaration(PrimitiveType, FieldIdentifier), FunctionDefinition( FunctionDeclarator(Identifier, ParameterList), FieldInitializerList(FieldInitializer(FieldIdentifier, ArgumentList(Number))), CompoundStatement), AccessSpecifier(Access), FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, const), CompoundStatement(ReturnStatement(return, Identifier)))))) # Inline method definitions with overrides struct B : A { int foo() override { return 2; } int pho() final { return 3; } int bar() const override { return 4; } int baz() const final { return 5; } int bag() const final override { return 6; } int bah() const override final { return 7; } }; ==> Program( StructSpecifier(struct, TypeIdentifier, BaseClassClause(TypeIdentifier), FieldDeclarationList( FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, VirtualSpecifier), CompoundStatement(ReturnStatement(return, Number))), FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, VirtualSpecifier), CompoundStatement(ReturnStatement(return, Number))), FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, const, VirtualSpecifier), CompoundStatement(ReturnStatement(return, Number))), FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, const, VirtualSpecifier), CompoundStatement(ReturnStatement(return, Number))), FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, const, VirtualSpecifier, VirtualSpecifier), CompoundStatement(ReturnStatement(return, Number))), FunctionDefinition( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList, const, VirtualSpecifier, VirtualSpecifier), CompoundStatement(ReturnStatement(return, Number)))))) # Virtual method declarations class A { virtual ~Point(); void b(); virtual void foo() {} virtual void bar(); }; ==> Program( ClassSpecifier(class, TypeIdentifier, FieldDeclarationList( Declaration( virtual, FunctionDeclarator(DestructorName, ParameterList)), FieldDeclaration( PrimitiveType, FunctionDeclarator(FieldIdentifier, ParameterList)), FunctionDefinition( virtual, PrimitiveType, FunctionDeclarator( FieldIdentifier, ParameterList), CompoundStatement), FieldDeclaration( virtual, PrimitiveType, FunctionDeclarator( FieldIdentifier, ParameterList))))) # Constructor and destructor declarations class C { void *data_; public: C(); C(int, float); ~C(); }; ==> Program( ClassSpecifier(class, TypeIdentifier, FieldDeclarationList( FieldDeclaration(PrimitiveType, PointerDeclarator(FieldIdentifier)), AccessSpecifier(Access), Declaration(FunctionDeclarator(Identifier, ParameterList)), Declaration(FunctionDeclarator(Identifier, ParameterList( ParameterDeclaration(PrimitiveType), ParameterDeclaration(PrimitiveType)))), Declaration(FunctionDeclarator(DestructorName, ParameterList))))) # Forward class declaration class Foo; ==> Program(ClassSpecifier(class,TypeIdentifier)) # Forward function declarations void foo(Bar& x, Bar& y) noexcept; void Foo(const unsigned char* in_bytes, char* out); foo::bar(); void foo(::y data, void* arg, void (*r)(void*)); ==> Program( Declaration(PrimitiveType,FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(TypeIdentifier,ReferenceDeclarator(Identifier)), ParameterDeclaration(TypeIdentifier,ReferenceDeclarator(Identifier))), Noexcept(noexcept))), Declaration(PrimitiveType,FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(const,SizedTypeSpecifier(TypeSize,PrimitiveType),PointerDeclarator(Identifier)), ParameterDeclaration(PrimitiveType,PointerDeclarator(Identifier))))), ExpressionStatement(CallExpression(ScopedIdentifier(NamespaceIdentifier,Identifier),ArgumentList)) Declaration(PrimitiveType,FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(ScopedTypeIdentifier(TypeIdentifier),Identifier), ParameterDeclaration(PrimitiveType,PointerDeclarator(Identifier)), ParameterDeclaration(PrimitiveType,FunctionDeclarator(ParenthesizedDeclarator( PointerDeclarator(Identifier)),ParameterList(ParameterDeclaration(PrimitiveType,AbstractPointerDeclarator)))))))) # Classes with inheritance class A : public B { }; class C : C::D, public E { }; ==> Program( ClassSpecifier(class, TypeIdentifier, BaseClassClause(Access, TypeIdentifier), FieldDeclarationList), ClassSpecifier(class, TypeIdentifier, BaseClassClause( ScopedTypeIdentifier(NamespaceIdentifier, TypeIdentifier), Access, TypeIdentifier), FieldDeclarationList)) # Classes with final virt specifier class A final : public B { }; class C final {}; struct D final {}; ==> Program( ClassSpecifier(class, TypeIdentifier, VirtualSpecifier, BaseClassClause(Access, TypeIdentifier), FieldDeclarationList), ClassSpecifier(class, TypeIdentifier, VirtualSpecifier, FieldDeclarationList), StructSpecifier(struct, TypeIdentifier, VirtualSpecifier, FieldDeclarationList)) # Nested classes class A { private: class B : private C, public D { }; B e, f; }; ==> Program( ClassSpecifier(class, TypeIdentifier, FieldDeclarationList( AccessSpecifier(Access), FieldDeclaration( ClassSpecifier(class, TypeIdentifier, BaseClassClause(Access, TypeIdentifier, Access, TypeIdentifier), FieldDeclarationList)), FieldDeclaration( TypeIdentifier, FieldIdentifier, FieldIdentifier)))) # Macro noise class Z { int num_to_block_ GUARDED_BY("foo" ++ lock_); MOCK_METHOD3(DoFoo, bool(const ns::Something&, const std::vector&, const string)); }; SOME_OTHER_MACRO(2.44e3); int z MACRO() = 0; ==> Program( ClassSpecifier(class,TypeIdentifier,FieldDeclarationList( FieldDeclaration(PrimitiveType,FieldIdentifier(Macro(MacroName,ArgumentList(BinaryExpression(String,UpdateOp,Identifier))))), Declaration(FunctionDeclarator(Identifier,ParameterList( ParameterDeclaration(TypeIdentifier), ParameterDeclaration(PrimitiveType,AbstractFunctionDeclarator(ParameterList( ParameterDeclaration(const,ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier),AbstractReferenceDeclarator), ParameterDeclaration(const,TemplateType( ScopedTypeIdentifier(NamespaceIdentifier,TypeIdentifier), TemplateArgumentList(TypeDescriptor(PrimitiveType))),AbstractReferenceDeclarator), ParameterDeclaration(const,TypeIdentifier))))))))), ExpressionStatement(CallExpression(Identifier,ArgumentList(Number))), Declaration(PrimitiveType,InitDeclarator(Identifier(Macro(MacroName,ArgumentList)),Number))) # Friend declarations struct C { friend class D; friend D; friend int f(C &); }; ==> Program( StructSpecifier(struct, TypeIdentifier, FieldDeclarationList( FriendDeclaration(friend, class, TypeIdentifier), FriendDeclaration(friend, TypeIdentifier), FriendDeclaration(friend, Declaration(PrimitiveType, FunctionDeclarator( Identifier, ParameterList(ParameterDeclaration(TypeIdentifier, AbstractReferenceDeclarator)))))))) # Default member initializers struct A { bool a = 1; vector b = {c, d, e}; F g {h}; }; ==> Program( StructSpecifier(struct, TypeIdentifier, FieldDeclarationList( FieldDeclaration( PrimitiveType, FieldIdentifier, Number), FieldDeclaration( TemplateType(TypeIdentifier, TemplateArgumentList(TypeDescriptor(PrimitiveType))), FieldIdentifier, InitializerList(Identifier, Identifier, Identifier)), FieldDeclaration( TypeIdentifier, FieldIdentifier, InitializerList(Identifier))))) # Function parameters with default values int foo(bool x = 5) {} ==> Program( FunctionDefinition( PrimitiveType, FunctionDeclarator( Identifier, ParameterList( OptionalParameterDeclaration(PrimitiveType, Identifier, Number))), CompoundStatement)) # Declarations with attributes int f([[a::b(c), d]] int x) {} [[gnu::always_inline]] [[gnu::hot]] [[gnu::const]] [[nodiscard]] inline int g(); [[ foo::target(gpu, [cpu]) ]] class Foo; ==> Program( FunctionDefinition( PrimitiveType, FunctionDeclarator( Identifier, ParameterList( ParameterDeclaration( Attribute(AttributeName(Identifier,Identifier),AttributeArgs(Identifier),AttributeName(Identifier)) PrimitiveType, Identifier))), CompoundStatement), Declaration( Attribute(AttributeName(Identifier,Identifier)) Attribute(AttributeName(Identifier,Identifier)) Attribute(AttributeName(Identifier,Identifier)) Attribute(AttributeName(Identifier)) inline, PrimitiveType, FunctionDeclarator(Identifier, ParameterList)), Attribute(AttributeName(Identifier,Identifier),AttributeArgs(Identifier,Identifier)), ClassSpecifier(class,TypeIdentifier)) # Operator overload declarations ostream &operator<<(ostream &, const A &a); bool A::operator!=(const A &other) const; ==> Program( Declaration( TypeIdentifier, ReferenceDeclarator( FunctionDeclarator( OperatorName(operator, BitOp), ParameterList( ParameterDeclaration(TypeIdentifier, AbstractReferenceDeclarator), ParameterDeclaration(const, TypeIdentifier, ReferenceDeclarator(Identifier)))))), Declaration( PrimitiveType, FunctionDeclarator( ScopedIdentifier(NamespaceIdentifier, OperatorName(operator, CompareOp)), ParameterList( ParameterDeclaration(const, TypeIdentifier, ReferenceDeclarator(Identifier))), const))) # Template declarations template void foo(T &t); template int bar(T &t) { return u; } template class Foo {}; template Foo::Foo(int mem) : mem_(mem) {} template template void A::foo(U&) {} ==> Program( TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier)), Declaration( PrimitiveType, FunctionDeclarator( Identifier, ParameterList( ParameterDeclaration(TypeIdentifier, ReferenceDeclarator(Identifier)))))), TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier), ParameterDeclaration(PrimitiveType, Identifier)), FunctionDefinition( PrimitiveType, FunctionDeclarator( Identifier, ParameterList(ParameterDeclaration(TypeIdentifier, ReferenceDeclarator(Identifier)))), CompoundStatement(ReturnStatement(return, Identifier)))), TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier)), ClassSpecifier(class, TypeIdentifier, FieldDeclarationList)), TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier)), FunctionDefinition( FunctionDeclarator( ScopedIdentifier( TemplateType( TypeIdentifier, TemplateArgumentList(TypeDescriptor(TypeIdentifier))), Identifier), ParameterList( ParameterDeclaration(PrimitiveType, Identifier))), FieldInitializerList( FieldInitializer( FieldIdentifier, ArgumentList(Identifier))), CompoundStatement)), TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier)), TemplateDeclaration(template, TemplateParameterList( TypeParameterDeclaration(typename, TypeIdentifier)), FunctionDefinition( PrimitiveType, FunctionDeclarator( ScopedIdentifier( TemplateType( TypeIdentifier, TemplateArgumentList(TypeDescriptor(TypeIdentifier))), Identifier), ParameterList( ParameterDeclaration( TypeIdentifier, AbstractReferenceDeclarator))), CompoundStatement)))) # Template template declarations template