Skip to content

Commit ce61309

Browse files
committed
fix(parser): Create AST for modules
1 parent e76ab93 commit ce61309

23 files changed

+1410
-107
lines changed

Diff for: packages/cxx-frontend/src/AST.ts

+160
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,105 @@ export class TypeConstraintAST extends AST {
425425
}
426426
}
427427

428+
export class GlobalModuleFragmentAST extends AST {
429+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
430+
return visitor.visitGlobalModuleFragment(this, context);
431+
}
432+
getModuleToken(): Token | undefined {
433+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
434+
}
435+
getSemicolonToken(): Token | undefined {
436+
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
437+
}
438+
*getDeclarationList(): Generator<DeclarationAST | undefined> {
439+
for (let it = cxx.getASTSlot(this.getHandle(), 2); it; it = cxx.getListNext(it)) {
440+
yield AST.from<DeclarationAST>(cxx.getListValue(it), this.parser);
441+
}
442+
}
443+
}
444+
445+
export class PrivateModuleFragmentAST extends AST {
446+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
447+
return visitor.visitPrivateModuleFragment(this, context);
448+
}
449+
getModuleToken(): Token | undefined {
450+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
451+
}
452+
getColonToken(): Token | undefined {
453+
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
454+
}
455+
getPrivateToken(): Token | undefined {
456+
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
457+
}
458+
getSemicolonToken(): Token | undefined {
459+
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
460+
}
461+
*getDeclarationList(): Generator<DeclarationAST | undefined> {
462+
for (let it = cxx.getASTSlot(this.getHandle(), 4); it; it = cxx.getListNext(it)) {
463+
yield AST.from<DeclarationAST>(cxx.getListValue(it), this.parser);
464+
}
465+
}
466+
}
467+
468+
export class ModuleDeclarationAST extends AST {
469+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
470+
return visitor.visitModuleDeclaration(this, context);
471+
}
472+
getExportToken(): Token | undefined {
473+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
474+
}
475+
getModuleToken(): Token | undefined {
476+
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
477+
}
478+
getModuleName(): ModuleNameAST | undefined {
479+
return AST.from<ModuleNameAST>(cxx.getASTSlot(this.getHandle(), 2), this.parser);
480+
}
481+
getModulePartition(): ModulePartitionAST | undefined {
482+
return AST.from<ModulePartitionAST>(cxx.getASTSlot(this.getHandle(), 3), this.parser);
483+
}
484+
*getAttributeList(): Generator<AttributeAST | undefined> {
485+
for (let it = cxx.getASTSlot(this.getHandle(), 4); it; it = cxx.getListNext(it)) {
486+
yield AST.from<AttributeAST>(cxx.getListValue(it), this.parser);
487+
}
488+
}
489+
getSemicolonToken(): Token | undefined {
490+
return Token.from(cxx.getASTSlot(this.getHandle(), 5), this.parser);
491+
}
492+
}
493+
494+
export class ModuleNameAST extends AST {
495+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
496+
return visitor.visitModuleName(this, context);
497+
}
498+
}
499+
500+
export class ImportNameAST extends AST {
501+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
502+
return visitor.visitImportName(this, context);
503+
}
504+
getHeaderToken(): Token | undefined {
505+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
506+
}
507+
getModulePartition(): ModulePartitionAST | undefined {
508+
return AST.from<ModulePartitionAST>(cxx.getASTSlot(this.getHandle(), 1), this.parser);
509+
}
510+
getModuleName(): ModuleNameAST | undefined {
511+
return AST.from<ModuleNameAST>(cxx.getASTSlot(this.getHandle(), 2), this.parser);
512+
}
513+
}
514+
515+
export class ModulePartitionAST extends AST {
516+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
517+
return visitor.visitModulePartition(this, context);
518+
}
519+
getColonToken(): Token | undefined {
520+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
521+
}
522+
getModuleName(): ModuleNameAST | undefined {
523+
return AST.from<ModuleNameAST>(cxx.getASTSlot(this.getHandle(), 1), this.parser);
524+
}
525+
}
526+
428527
export class SimpleRequirementAST extends RequirementAST {
429528
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
430529
return visitor.visitSimpleRequirement(this, context);
@@ -814,6 +913,20 @@ export class ModuleUnitAST extends UnitAST {
814913
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
815914
return visitor.visitModuleUnit(this, context);
816915
}
916+
getGlobalModuleFragment(): GlobalModuleFragmentAST | undefined {
917+
return AST.from<GlobalModuleFragmentAST>(cxx.getASTSlot(this.getHandle(), 0), this.parser);
918+
}
919+
getModuleDeclaration(): ModuleDeclarationAST | undefined {
920+
return AST.from<ModuleDeclarationAST>(cxx.getASTSlot(this.getHandle(), 1), this.parser);
921+
}
922+
*getDeclarationList(): Generator<DeclarationAST | undefined> {
923+
for (let it = cxx.getASTSlot(this.getHandle(), 2); it; it = cxx.getListNext(it)) {
924+
yield AST.from<DeclarationAST>(cxx.getListValue(it), this.parser);
925+
}
926+
}
927+
getPrivateModuleFragmentAST(): PrivateModuleFragmentAST | undefined {
928+
return AST.from<PrivateModuleFragmentAST>(cxx.getASTSlot(this.getHandle(), 3), this.parser);
929+
}
817930
}
818931

819932
export class ThisExpressionAST extends ExpressionAST {
@@ -2065,12 +2178,52 @@ export class ExportDeclarationAST extends DeclarationAST {
20652178
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
20662179
return visitor.visitExportDeclaration(this, context);
20672180
}
2181+
getExportToken(): Token | undefined {
2182+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
2183+
}
2184+
getDeclaration(): DeclarationAST | undefined {
2185+
return AST.from<DeclarationAST>(cxx.getASTSlot(this.getHandle(), 1), this.parser);
2186+
}
2187+
}
2188+
2189+
export class ExportCompoundDeclarationAST extends DeclarationAST {
2190+
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
2191+
return visitor.visitExportCompoundDeclaration(this, context);
2192+
}
2193+
getExportToken(): Token | undefined {
2194+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
2195+
}
2196+
getLbraceToken(): Token | undefined {
2197+
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
2198+
}
2199+
*getDeclarationList(): Generator<DeclarationAST | undefined> {
2200+
for (let it = cxx.getASTSlot(this.getHandle(), 2); it; it = cxx.getListNext(it)) {
2201+
yield AST.from<DeclarationAST>(cxx.getListValue(it), this.parser);
2202+
}
2203+
}
2204+
getRbraceToken(): Token | undefined {
2205+
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
2206+
}
20682207
}
20692208

20702209
export class ModuleImportDeclarationAST extends DeclarationAST {
20712210
accept<Context, Result>(visitor: ASTVisitor<Context, Result>, context: Context): Result {
20722211
return visitor.visitModuleImportDeclaration(this, context);
20732212
}
2213+
getImportToken(): Token | undefined {
2214+
return Token.from(cxx.getASTSlot(this.getHandle(), 0), this.parser);
2215+
}
2216+
getImportName(): ImportNameAST | undefined {
2217+
return AST.from<ImportNameAST>(cxx.getASTSlot(this.getHandle(), 1), this.parser);
2218+
}
2219+
*getAttributeList(): Generator<AttributeAST | undefined> {
2220+
for (let it = cxx.getASTSlot(this.getHandle(), 2); it; it = cxx.getListNext(it)) {
2221+
yield AST.from<AttributeAST>(cxx.getListValue(it), this.parser);
2222+
}
2223+
}
2224+
getSemicolonToken(): Token | undefined {
2225+
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
2226+
}
20742227
}
20752228

20762229
export class TemplateDeclarationAST extends DeclarationAST {
@@ -2906,6 +3059,12 @@ const AST_CONSTRUCTORS: Array<new (handle: number, kind: ASTKind, parser: Parser
29063059
CtorInitializerAST,
29073060
RequirementBodyAST,
29083061
TypeConstraintAST,
3062+
GlobalModuleFragmentAST,
3063+
PrivateModuleFragmentAST,
3064+
ModuleDeclarationAST,
3065+
ModuleNameAST,
3066+
ImportNameAST,
3067+
ModulePartitionAST,
29093068
SimpleRequirementAST,
29103069
CompoundRequirementAST,
29113070
TypeRequirementAST,
@@ -3006,6 +3165,7 @@ const AST_CONSTRUCTORS: Array<new (handle: number, kind: ASTKind, parser: Parser
30063165
UsingDeclarationAST,
30073166
AsmDeclarationAST,
30083167
ExportDeclarationAST,
3168+
ExportCompoundDeclarationAST,
30093169
ModuleImportDeclarationAST,
30103170
TemplateDeclarationAST,
30113171
TypenameTypeParameterAST,

Diff for: packages/cxx-frontend/src/ASTKind.ts

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export enum ASTKind {
4141
CtorInitializer,
4242
RequirementBody,
4343
TypeConstraint,
44+
GlobalModuleFragment,
45+
PrivateModuleFragment,
46+
ModuleDeclaration,
47+
ModuleName,
48+
ImportName,
49+
ModulePartition,
4450

4551
// RequirementAST
4652
SimpleRequirement,
@@ -165,6 +171,7 @@ export enum ASTKind {
165171
UsingDeclaration,
166172
AsmDeclaration,
167173
ExportDeclaration,
174+
ExportCompoundDeclaration,
168175
ModuleImportDeclaration,
169176
TemplateDeclaration,
170177
TypenameTypeParameter,

Diff for: packages/cxx-frontend/src/ASTVisitor.ts

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export abstract class ASTVisitor<Context, Result> {
4545
abstract visitCtorInitializer(node: ast.CtorInitializerAST, context: Context): Result;
4646
abstract visitRequirementBody(node: ast.RequirementBodyAST, context: Context): Result;
4747
abstract visitTypeConstraint(node: ast.TypeConstraintAST, context: Context): Result;
48+
abstract visitGlobalModuleFragment(node: ast.GlobalModuleFragmentAST, context: Context): Result;
49+
abstract visitPrivateModuleFragment(node: ast.PrivateModuleFragmentAST, context: Context): Result;
50+
abstract visitModuleDeclaration(node: ast.ModuleDeclarationAST, context: Context): Result;
51+
abstract visitModuleName(node: ast.ModuleNameAST, context: Context): Result;
52+
abstract visitImportName(node: ast.ImportNameAST, context: Context): Result;
53+
abstract visitModulePartition(node: ast.ModulePartitionAST, context: Context): Result;
4854

4955
// RequirementAST
5056
abstract visitSimpleRequirement(node: ast.SimpleRequirementAST, context: Context): Result;
@@ -169,6 +175,7 @@ export abstract class ASTVisitor<Context, Result> {
169175
abstract visitUsingDeclaration(node: ast.UsingDeclarationAST, context: Context): Result;
170176
abstract visitAsmDeclaration(node: ast.AsmDeclarationAST, context: Context): Result;
171177
abstract visitExportDeclaration(node: ast.ExportDeclarationAST, context: Context): Result;
178+
abstract visitExportCompoundDeclaration(node: ast.ExportCompoundDeclarationAST, context: Context): Result;
172179
abstract visitModuleImportDeclaration(node: ast.ModuleImportDeclarationAST, context: Context): Result;
173180
abstract visitTemplateDeclaration(node: ast.TemplateDeclarationAST, context: Context): Result;
174181
abstract visitTypenameTypeParameter(node: ast.TypenameTypeParameterAST, context: Context): Result;

Diff for: packages/cxx-frontend/src/RecursiveASTVisitor.ts

+49
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,38 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
161161
this.accept(node.getName(), context);
162162
}
163163

164+
visitGlobalModuleFragment(node: ast.GlobalModuleFragmentAST, context: Context): void {
165+
for (const element of node.getDeclarationList()) {
166+
this.accept(element, context);
167+
}
168+
}
169+
170+
visitPrivateModuleFragment(node: ast.PrivateModuleFragmentAST, context: Context): void {
171+
for (const element of node.getDeclarationList()) {
172+
this.accept(element, context);
173+
}
174+
}
175+
176+
visitModuleDeclaration(node: ast.ModuleDeclarationAST, context: Context): void {
177+
this.accept(node.getModuleName(), context);
178+
this.accept(node.getModulePartition(), context);
179+
for (const element of node.getAttributeList()) {
180+
this.accept(element, context);
181+
}
182+
}
183+
184+
visitModuleName(node: ast.ModuleNameAST, context: Context): void {
185+
}
186+
187+
visitImportName(node: ast.ImportNameAST, context: Context): void {
188+
this.accept(node.getModulePartition(), context);
189+
this.accept(node.getModuleName(), context);
190+
}
191+
192+
visitModulePartition(node: ast.ModulePartitionAST, context: Context): void {
193+
this.accept(node.getModuleName(), context);
194+
}
195+
164196
visitSimpleRequirement(node: ast.SimpleRequirementAST, context: Context): void {
165197
this.accept(node.getExpression(), context);
166198
}
@@ -284,6 +316,12 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
284316
}
285317

286318
visitModuleUnit(node: ast.ModuleUnitAST, context: Context): void {
319+
this.accept(node.getGlobalModuleFragment(), context);
320+
this.accept(node.getModuleDeclaration(), context);
321+
for (const element of node.getDeclarationList()) {
322+
this.accept(element, context);
323+
}
324+
this.accept(node.getPrivateModuleFragmentAST(), context);
287325
}
288326

289327
visitThisExpression(node: ast.ThisExpressionAST, context: Context): void {
@@ -644,9 +682,20 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
644682
}
645683

646684
visitExportDeclaration(node: ast.ExportDeclarationAST, context: Context): void {
685+
this.accept(node.getDeclaration(), context);
686+
}
687+
688+
visitExportCompoundDeclaration(node: ast.ExportCompoundDeclarationAST, context: Context): void {
689+
for (const element of node.getDeclarationList()) {
690+
this.accept(element, context);
691+
}
647692
}
648693

649694
visitModuleImportDeclaration(node: ast.ModuleImportDeclarationAST, context: Context): void {
695+
this.accept(node.getImportName(), context);
696+
for (const element of node.getAttributeList()) {
697+
this.accept(element, context);
698+
}
650699
}
651700

652701
visitTemplateDeclaration(node: ast.TemplateDeclarationAST, context: Context): void {

0 commit comments

Comments
 (0)