Skip to content

Commit f5b6da3

Browse files
committed
Improve printing enumerator AST nodes
1 parent 8608cf1 commit f5b6da3

File tree

11 files changed

+91
-49
lines changed

11 files changed

+91
-49
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ export function gen_ast_decoder_cc({
4848
const className = makeClassName(base);
4949
emit();
5050
emit(
51-
` auto ASTDecoder::decode${className}(const void* ptr, io::${className} type) -> ${base}* {`
51+
` auto ASTDecoder::decode${className}(const void* ptr, io::${className} type) -> ${base}* {`,
5252
);
5353
emit(` switch (type) {`);
5454
nodes.forEach(({ name }) => {
5555
const className = makeClassName(name);
5656
emit(` case io::${baseClassName}_${className}:`);
5757
emit(
58-
` return decode${className}(reinterpret_cast<const io::${className}*>(ptr));`
58+
` return decode${className}(reinterpret_cast<const io::${className}*>(ptr));`,
5959
);
6060
});
6161
emit(` default:`);
@@ -70,7 +70,7 @@ export function gen_ast_decoder_cc({
7070
const className = makeClassName(name);
7171
emit();
7272
emit(
73-
` auto ASTDecoder::decode${className}(const io::${className}* node) -> ${name}* {`
73+
` auto ASTDecoder::decode${className}(const io::${className}* node) -> ${name}* {`,
7474
);
7575
emit(` if (!node) return nullptr;`);
7676
emit();
@@ -88,7 +88,9 @@ export function gen_ast_decoder_cc({
8888
const className = makeClassName(m.type);
8989
emit(` if (node->${snakeName}()) {`);
9090
emit(` auto* inserter = &ast->${m.name};`);
91-
emit(` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`);
91+
emit(
92+
` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`,
93+
);
9294
emit(` ++i) {`);
9395
emit(` *inserter = new (pool_) List(decode${className}(`);
9496
emit(` node->${snakeName}()->Get(i),`);
@@ -100,7 +102,9 @@ export function gen_ast_decoder_cc({
100102
const className = makeClassName(m.type);
101103
emit(` if (node->${snakeName}()) {`);
102104
emit(` auto* inserter = &ast->${m.name};`);
103-
emit(` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`);
105+
emit(
106+
` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`,
107+
);
104108
emit(` ++i) {`);
105109
emit(` *inserter = new (pool_) List(decode${className}(`);
106110
emit(` node->${snakeName}()->Get(i)));`);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function gen_ast_encoder_cc({
4444
const emitLiteral = (
4545
m: Attribute,
4646
table: string,
47-
finalizers: (() => void)[]
47+
finalizers: (() => void)[],
4848
) => {
4949
const fieldName = toSnakeName(m.name);
5050

@@ -70,7 +70,7 @@ export function gen_ast_encoder_cc({
7070
const className = makeClassName(base);
7171
emit();
7272
emit(
73-
` auto ASTEncoder::accept${className}(${base}* ast) -> std::tuple<flatbuffers::Offset<>, std::uint32_t> {`
73+
` auto ASTEncoder::accept${className}(${base}* ast) -> std::tuple<flatbuffers::Offset<>, std::uint32_t> {`,
7474
);
7575
emit(` if (!ast) return {};`);
7676
emit(` flatbuffers::Offset<> offset;`);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
9090
}
9191

9292
emit(
93-
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`
93+
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`,
9494
);
9595
emit();
9696
emit(` auto firstSourceLocation() -> SourceLocation override;`);
@@ -117,7 +117,7 @@ template <typename T>
117117
emit(` switch (ast->kind()) {`);
118118
nodes.forEach(({ name }) => {
119119
emit(
120-
` case ${name}::Kind: return std::invoke(std::forward<Visitor>(visitor), static_cast<${name}*>(ast));`
120+
` case ${name}::Kind: return std::invoke(std::forward<Visitor>(visitor), static_cast<${name}*>(ast));`,
121121
);
122122
});
123123
emit(` default: cxx_runtime_error("unexpected ${variantName}");`);

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,17 @@ export function gen_ast_pretty_printer_cc({
104104
case "dotLoc":
105105
case "accessLoc":
106106
case "lbracketLoc":
107+
case "lbracket2Loc":
107108
case "lparen2Loc":
108109
case "lparenLoc":
109110
case "openLoc":
110111
case "rbracketLoc":
112+
case "rbracket2Loc":
111113
case "rparen2Loc":
112114
case "rparenLoc":
113115
case "greaterLoc":
114116
case "scopeLoc":
117+
case "commaLoc":
115118
emit(`nospace();`);
116119
break;
117120

@@ -127,7 +130,9 @@ export function gen_ast_pretty_printer_cc({
127130

128131
case "rbraceLoc":
129132
emit(`unindent();`);
130-
emit(`newline();`);
133+
if (!["BracedInitListAST"].includes(name)) {
134+
emit(`newline();`);
135+
}
131136
break;
132137

133138
default:
@@ -139,22 +144,27 @@ export function gen_ast_pretty_printer_cc({
139144
switch (m.name) {
140145
case "lbraceLoc":
141146
emit(`indent();`);
142-
emit(`newline();`);
147+
if (!["BracedInitListAST"].includes(name)) {
148+
emit(`newline();`);
149+
}
143150
break;
144151

145152
case "closeLoc":
153+
case "lbracketLoc":
154+
case "lbracket2Loc":
146155
case "dotLoc":
147156
case "lessLoc":
148157
case "lparenLoc":
149-
case "minusGreaterLoc":
150158
case "openLoc":
151159
case "scopeLoc":
152160
case "accessLoc":
153161
emit(`nospace();`);
154162
break;
155163

156164
case "rbraceLoc":
157-
emit(`newline();`);
165+
if (!["BracedInitListAST"].includes(name)) {
166+
emit(`newline();`);
167+
}
158168
break;
159169

160170
case "semicolonLoc":
@@ -167,6 +177,10 @@ export function gen_ast_pretty_printer_cc({
167177
if (name === "TemplateDeclarationAST") emit(`newline();`);
168178
break;
169179

180+
case "minusGreaterLoc":
181+
emit(`space();`);
182+
break;
183+
170184
case "colonLoc":
171185
if (
172186
[
@@ -242,7 +256,7 @@ export function gen_ast_pretty_printer_cc({
242256
nodes.forEach(({ name, members }) => {
243257
emit();
244258
emit(
245-
`void ASTPrettyPrinter::${className}Visitor::operator()(${name}* ast) {`
259+
`void ASTPrettyPrinter::${className}Visitor::operator()(${name}* ast) {`,
246260
);
247261

248262
members.forEach((m) => {
@@ -270,6 +284,7 @@ export function gen_ast_pretty_printer_cc({
270284

271285
if (m.name === "lparen2Loc" && name === "GccAttributeAST") {
272286
emit(`
287+
nospace();
273288
for (auto loc = ast->lparen2Loc; loc; loc = loc.next()) {
274289
if (loc == ast->rparenLoc) break;
275290
accept.writeToken(loc);
@@ -314,7 +329,13 @@ if (ast->op == TokenKind::T_NEW_ARRAY) {
314329
emit(`for (auto it = ast->${m.name}; it; it = it->next) {`);
315330
emit(`accept(it->value);`);
316331
if (isCommaSeparated(m, name)) {
317-
emit(`if (it->next) { nospace(); accept.write(","); }`);
332+
if (["enumeratorList"].includes(m.name)) {
333+
emit(
334+
`if (it->next) { nospace(); accept.write(","); newline(); }`,
335+
);
336+
} else {
337+
emit(`if (it->next) { nospace(); accept.write(","); }`);
338+
}
318339
}
319340
emit(`}`);
320341
emit();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,23 @@ export function gen_ast_printer_cc({
6262
emit(` ++indent_;`);
6363
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
6464
emit(
65-
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`
65+
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`,
6666
);
6767
emit(` --indent_;`);
6868
emit(` }`);
6969
} else if (member.kind == "attribute" && member.type === "int") {
7070
emit(` ++indent_;`);
7171
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
7272
emit(
73-
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`
73+
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`,
7474
);
7575
emit(` --indent_;`);
7676
} else if (member.kind == "attribute" && member.type.endsWith("Literal")) {
7777
emit(` if (ast->${member.name}) {`);
7878
emit(` ++indent_;`);
7979
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
8080
emit(
81-
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name}->value());`
81+
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name}->value());`,
8282
);
8383
emit(` --indent_;`);
8484
emit(` }`);
@@ -90,7 +90,7 @@ export function gen_ast_printer_cc({
9090
emit(` ++indent_;`);
9191
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
9292
emit(
93-
` out_ << std::format("${fieldName}: {}\\n", Token::spell(ast->${member.name}));`
93+
` out_ << std::format("${fieldName}: {}\\n", Token::spell(ast->${member.name}));`,
9494
);
9595
emit(` --indent_;`);
9696
emit(` }`);
@@ -101,7 +101,7 @@ export function gen_ast_printer_cc({
101101
emit(` ++indent_;`);
102102
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
103103
emit(
104-
` out_ << std::format("${fieldName}: {}\\n", to_string(ast->${member.name}));`
104+
` out_ << std::format("${fieldName}: {}\\n", to_string(ast->${member.name}));`,
105105
);
106106
emit(` --indent_;`);
107107
}
@@ -115,7 +115,7 @@ export function gen_ast_printer_cc({
115115
emit(` out_ << "${astName(name)}";`);
116116
emit(` if (ast->type) {`);
117117
emit(
118-
` out_ << std::format(" [{} {}]", to_string(ast->valueCategory), to_string(ast->type));`
118+
` out_ << std::format(" [{} {}]", to_string(ast->valueCategory), to_string(ast->type));`,
119119
);
120120
emit(` }`);
121121
emit(` out_ << "\\n";`);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
5151
by_base.forEach((nodes) => {
5252
nodes.forEach(({ name, members }) => {
5353
const memberSlots = members.filter(
54-
(m) => classifyMemberSlot(m) !== undefined
54+
(m) => classifyMemberSlot(m) !== undefined,
5555
);
5656

5757
emit();
@@ -87,7 +87,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
8787
case MemberSlotClassification.IdentifierAttribute:
8888
emit(` case ${slotCount}: // ${m.name}`);
8989
emit(
90-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
90+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
9191
);
9292
emit(` slotKind_ = ASTSlotKind::kIdentifierAttribute;`);
9393
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
@@ -96,7 +96,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
9696
case MemberSlotClassification.LiteralAttribute:
9797
emit(` case ${slotCount}: // ${m.name}`);
9898
emit(
99-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
99+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
100100
);
101101
emit(` slotKind_ = ASTSlotKind::kLiteralAttribute;`);
102102
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
@@ -112,7 +112,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
112112
case MemberSlotClassification.Node:
113113
emit(` case ${slotCount}: // ${m.name}`);
114114
emit(
115-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
115+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
116116
);
117117
emit(` slotKind_ = ASTSlotKind::kNode;`);
118118
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
@@ -121,7 +121,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
121121
case MemberSlotClassification.NodeList:
122122
emit(` case ${slotCount}: // ${m.name}`);
123123
emit(
124-
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
124+
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
125125
);
126126
emit(` slotKind_ = ASTSlotKind::kNodeList;`);
127127
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {
3030

3131
emit("#define FOR_EACH_BASE_TOKEN(V) \\");
3232
tokens.BASE_TOKENS.forEach((tk) =>
33-
emit(` V(${tk}, "${baseTokenId(tk)}") \\`)
33+
emit(` V(${tk}, "${baseTokenId(tk)}") \\`),
3434
);
3535

3636
emit();
3737
emit("#define FOR_EACH_OPERATOR(V) \\");
3838
tokens.OPERATORS.forEach(([tk, spelling]) =>
39-
emit(` V(${tk}, "${spelling}") \\`)
39+
emit(` V(${tk}, "${spelling}") \\`),
4040
);
4141

4242
emit();
@@ -46,13 +46,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {
4646
emit();
4747
emit("#define FOR_EACH_BUILTIN_TYPE_TRAIT(V) \\");
4848
tokens.BUILTIN_TYPE_TRAITS.forEach((tk) =>
49-
emit(` V(${tk.toUpperCase()}, "${tk}") \\`)
49+
emit(` V(${tk.toUpperCase()}, "${tk}") \\`),
5050
);
5151

5252
emit();
5353
emit("#define FOR_EACH_TOKEN_ALIAS(V) \\");
5454
tokens.TOKEN_ALIASES.forEach(([tk, other]) =>
55-
emit(` V(${tk.toUpperCase()}, ${other}) \\`)
55+
emit(` V(${tk.toUpperCase()}, ${other}) \\`),
5656
);
5757

5858
const out = `${cpy_header}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function new_ast_op_cc({
116116
const resultTy = `${chopAST(base)}Result`;
117117
emit();
118118
emit(
119-
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`
119+
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`,
120120
);
121121
members.forEach((m) => {
122122
switch (m.kind) {

0 commit comments

Comments
 (0)