Skip to content

Commit df4fb1e

Browse files
committed
Add -ast-print option to CLI for printing AST in C++ syntax
The output is not necessarily well formatted, to make it more readable use clang-format, for example: ./build/src/frontend/cxx -ast-print tests/manual/source.cc | clang-format
1 parent 0f2bbb9 commit df4fb1e

File tree

21 files changed

+5124
-59
lines changed

21 files changed

+5124
-59
lines changed

packages/cxx-frontend/src/AST.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9623,13 +9623,6 @@ export class EnumSpecifierAST extends SpecifierAST {
96239623
return Token.from(cxx.getASTSlot(this.getHandle(), 7), this.parser);
96249624
}
96259625

9626-
/**
9627-
* Returns the location of the comma token in this node
9628-
*/
9629-
getCommaToken(): Token | undefined {
9630-
return Token.from(cxx.getASTSlot(this.getHandle(), 8), this.parser);
9631-
}
9632-
96339626
/**
96349627
* Returns the enumeratorList of this node
96359628
*/
@@ -9656,6 +9649,13 @@ export class EnumSpecifierAST extends SpecifierAST {
96569649
};
96579650
}
96589651

9652+
/**
9653+
* Returns the location of the comma token in this node
9654+
*/
9655+
getCommaToken(): Token | undefined {
9656+
return Token.from(cxx.getASTSlot(this.getHandle(), 9), this.parser);
9657+
}
9658+
96599659
/**
96609660
* Returns the location of the rbrace token in this node
96619661
*/
@@ -9863,15 +9863,29 @@ export class TypenameSpecifierAST extends SpecifierAST {
98639863
);
98649864
}
98659865

9866+
/**
9867+
* Returns the location of the template token in this node
9868+
*/
9869+
getTemplateToken(): Token | undefined {
9870+
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
9871+
}
9872+
98669873
/**
98679874
* Returns the unqualifiedId of this node
98689875
*/
98699876
getUnqualifiedId(): UnqualifiedIdAST | undefined {
98709877
return AST.from<UnqualifiedIdAST>(
9871-
cxx.getASTSlot(this.getHandle(), 2),
9878+
cxx.getASTSlot(this.getHandle(), 3),
98729879
this.parser,
98739880
);
98749881
}
9882+
9883+
/**
9884+
* Returns the isTemplateIntroduced attribute of this node
9885+
*/
9886+
getIsTemplateIntroduced(): boolean {
9887+
return cxx.getASTSlot(this.getHandle(), 4) !== 0;
9888+
}
98759889
}
98769890

98779891
/**
@@ -11677,12 +11691,19 @@ export class TypeRequirementAST extends RequirementAST {
1167711691
);
1167811692
}
1167911693

11694+
/**
11695+
* Returns the location of the template token in this node
11696+
*/
11697+
getTemplateToken(): Token | undefined {
11698+
return Token.from(cxx.getASTSlot(this.getHandle(), 2), this.parser);
11699+
}
11700+
1168011701
/**
1168111702
* Returns the unqualifiedId of this node
1168211703
*/
1168311704
getUnqualifiedId(): UnqualifiedIdAST | undefined {
1168411705
return AST.from<UnqualifiedIdAST>(
11685-
cxx.getASTSlot(this.getHandle(), 2),
11706+
cxx.getASTSlot(this.getHandle(), 3),
1168611707
this.parser,
1168711708
);
1168811709
}
@@ -11691,7 +11712,14 @@ export class TypeRequirementAST extends RequirementAST {
1169111712
* Returns the location of the semicolon token in this node
1169211713
*/
1169311714
getSemicolonToken(): Token | undefined {
11694-
return Token.from(cxx.getASTSlot(this.getHandle(), 3), this.parser);
11715+
return Token.from(cxx.getASTSlot(this.getHandle(), 4), this.parser);
11716+
}
11717+
11718+
/**
11719+
* Returns the isTemplateIntroduced attribute of this node
11720+
*/
11721+
getIsTemplateIntroduced(): boolean {
11722+
return cxx.getASTSlot(this.getHandle(), 5) !== 0;
1169511723
}
1169611724
}
1169711725

packages/cxx-gen-ast/src/gen.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
// SOFTWARE.
2222

2323
import { gen_ast_cc } from "./gen_ast_cc.js";
24-
import { gen_ast_dump_cc } from "./gen_ast_dump_cc.js";
25-
import { gen_ast_dump_h } from "./gen_ast_dump_h.js";
24+
import { gen_ast_printer_cc } from "./gen_ast_printer_cc.js";
25+
import { gen_ast_printer_h } from "./gen_ast_printer_h.js";
2626
import { gen_ast_fwd_h } from "./gen_ast_fwd_h.js";
2727
import { gen_ast_h } from "./gen_ast_h.js";
2828
import { gen_ast_kind_h } from "./gen_ast_kind_h.js";
@@ -40,14 +40,16 @@ import { gen_ast_encoder_cc } from "./gen_ast_encoder_cc.js";
4040
import { gen_ast_decoder_h } from "./gen_ast_decoder_h.js";
4141
import { gen_ast_decoder_cc } from "./gen_ast_decoder_cc.js";
4242
import { gen_ast_slot_ts } from "./gen_ast_slot_ts.js";
43+
import { gen_token_fwd_h } from "./gen_token_fwd_h.js";
44+
import { gen_tokenkind_ts } from "./gen_tokenkind_ts.js";
45+
import { gen_keywords_kwgen } from "./gen_keywords_kwgen.js";
46+
import { gen_ast_pretty_printer_h } from "./gen_ast_pretty_printer_h.js";
47+
import { gen_ast_pretty_printer_cc } from "./gen_ast_pretty_printer_cc.js";
4348

4449
import * as fs from "fs";
4550
import * as path from "path";
4651
import * as process from "process";
4752
import * as child_process from "child_process";
48-
import { gen_token_fwd_h } from "./gen_token_fwd_h.js";
49-
import { gen_tokenkind_ts } from "./gen_tokenkind_ts.js";
50-
import { gen_keywords_kwgen } from "./gen_keywords_kwgen.js";
5153

5254
const outdir = process.cwd();
5355

@@ -65,14 +67,22 @@ gen_ast_visitor_h({
6567
ast,
6668
output: path.join(outdir, "src/parser/cxx/ast_visitor.h"),
6769
});
68-
gen_ast_dump_h({
70+
gen_ast_printer_h({
6971
ast,
7072
output: path.join(outdir, "src/parser/cxx/ast_printer.h"),
7173
});
72-
gen_ast_dump_cc({
74+
gen_ast_printer_cc({
7375
ast,
7476
output: path.join(outdir, "src/parser/cxx/ast_printer.cc"),
7577
});
78+
gen_ast_pretty_printer_h({
79+
ast,
80+
output: path.join(outdir, "src/parser/cxx/ast_pretty_printer.h"),
81+
});
82+
gen_ast_pretty_printer_cc({
83+
ast,
84+
output: path.join(outdir, "src/parser/cxx/ast_pretty_printer.cc"),
85+
});
7686
gen_ast_kind_h({ ast, output: path.join(outdir, "src/parser/cxx/ast_kind.h") });
7787
gen_ast_slot_h({ ast, output: path.join(outdir, "src/parser/cxx/ast_slot.h") });
7888
gen_ast_slot_cc({

0 commit comments

Comments
 (0)