Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/cxx-gen-ast/src/gen_ast_decoder_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ export function gen_ast_decoder_cc({
const className = makeClassName(base);
emit();
emit(
` auto ASTDecoder::decode${className}(const void* ptr, io::${className} type) -> ${base}* {`
` auto ASTDecoder::decode${className}(const void* ptr, io::${className} type) -> ${base}* {`,
);
emit(` switch (type) {`);
nodes.forEach(({ name }) => {
const className = makeClassName(name);
emit(` case io::${baseClassName}_${className}:`);
emit(
` return decode${className}(reinterpret_cast<const io::${className}*>(ptr));`
` return decode${className}(reinterpret_cast<const io::${className}*>(ptr));`,
);
});
emit(` default:`);
Expand All @@ -70,7 +70,7 @@ export function gen_ast_decoder_cc({
const className = makeClassName(name);
emit();
emit(
` auto ASTDecoder::decode${className}(const io::${className}* node) -> ${name}* {`
` auto ASTDecoder::decode${className}(const io::${className}* node) -> ${name}* {`,
);
emit(` if (!node) return nullptr;`);
emit();
Expand All @@ -88,7 +88,9 @@ export function gen_ast_decoder_cc({
const className = makeClassName(m.type);
emit(` if (node->${snakeName}()) {`);
emit(` auto* inserter = &ast->${m.name};`);
emit(` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`);
emit(
` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`,
);
emit(` ++i) {`);
emit(` *inserter = new (pool_) List(decode${className}(`);
emit(` node->${snakeName}()->Get(i),`);
Expand All @@ -100,7 +102,9 @@ export function gen_ast_decoder_cc({
const className = makeClassName(m.type);
emit(` if (node->${snakeName}()) {`);
emit(` auto* inserter = &ast->${m.name};`);
emit(` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`);
emit(
` for (std::uint32_t i = 0; i < node->${snakeName}()->size();`,
);
emit(` ++i) {`);
emit(` *inserter = new (pool_) List(decode${className}(`);
emit(` node->${snakeName}()->Get(i)));`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cxx-gen-ast/src/gen_ast_encoder_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function gen_ast_encoder_cc({
const emitLiteral = (
m: Attribute,
table: string,
finalizers: (() => void)[]
finalizers: (() => void)[],
) => {
const fieldName = toSnakeName(m.name);

Expand All @@ -70,7 +70,7 @@ export function gen_ast_encoder_cc({
const className = makeClassName(base);
emit();
emit(
` auto ASTEncoder::accept${className}(${base}* ast) -> std::tuple<flatbuffers::Offset<>, std::uint32_t> {`
` auto ASTEncoder::accept${className}(${base}* ast) -> std::tuple<flatbuffers::Offset<>, std::uint32_t> {`,
);
emit(` if (!ast) return {};`);
emit(` flatbuffers::Offset<> offset;`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cxx-gen-ast/src/gen_ast_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function gen_ast_h({ ast, output }: { ast: AST; output: string }) {
}

emit(
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`
` void accept(ASTVisitor* visitor) override { visitor->visit(this); }`,
);
emit();
emit(` auto firstSourceLocation() -> SourceLocation override;`);
Expand All @@ -117,7 +117,7 @@ template <typename T>
emit(` switch (ast->kind()) {`);
nodes.forEach(({ name }) => {
emit(
` case ${name}::Kind: return std::invoke(std::forward<Visitor>(visitor), static_cast<${name}*>(ast));`
` case ${name}::Kind: return std::invoke(std::forward<Visitor>(visitor), static_cast<${name}*>(ast));`,
);
});
emit(` default: cxx_runtime_error("unexpected ${variantName}");`);
Expand Down
33 changes: 27 additions & 6 deletions packages/cxx-gen-ast/src/gen_ast_pretty_printer_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,17 @@ export function gen_ast_pretty_printer_cc({
case "dotLoc":
case "accessLoc":
case "lbracketLoc":
case "lbracket2Loc":
case "lparen2Loc":
case "lparenLoc":
case "openLoc":
case "rbracketLoc":
case "rbracket2Loc":
case "rparen2Loc":
case "rparenLoc":
case "greaterLoc":
case "scopeLoc":
case "commaLoc":
emit(`nospace();`);
break;

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

case "rbraceLoc":
emit(`unindent();`);
emit(`newline();`);
if (!["BracedInitListAST"].includes(name)) {
emit(`newline();`);
}
break;

default:
Expand All @@ -139,22 +144,27 @@ export function gen_ast_pretty_printer_cc({
switch (m.name) {
case "lbraceLoc":
emit(`indent();`);
emit(`newline();`);
if (!["BracedInitListAST"].includes(name)) {
emit(`newline();`);
}
break;

case "closeLoc":
case "lbracketLoc":
case "lbracket2Loc":
case "dotLoc":
case "lessLoc":
case "lparenLoc":
case "minusGreaterLoc":
case "openLoc":
case "scopeLoc":
case "accessLoc":
emit(`nospace();`);
break;

case "rbraceLoc":
emit(`newline();`);
if (!["BracedInitListAST"].includes(name)) {
emit(`newline();`);
}
break;

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

case "minusGreaterLoc":
emit(`space();`);
break;

case "colonLoc":
if (
[
Expand Down Expand Up @@ -242,7 +256,7 @@ export function gen_ast_pretty_printer_cc({
nodes.forEach(({ name, members }) => {
emit();
emit(
`void ASTPrettyPrinter::${className}Visitor::operator()(${name}* ast) {`
`void ASTPrettyPrinter::${className}Visitor::operator()(${name}* ast) {`,
);

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

if (m.name === "lparen2Loc" && name === "GccAttributeAST") {
emit(`
nospace();
for (auto loc = ast->lparen2Loc; loc; loc = loc.next()) {
if (loc == ast->rparenLoc) break;
accept.writeToken(loc);
Expand Down Expand Up @@ -314,7 +329,13 @@ if (ast->op == TokenKind::T_NEW_ARRAY) {
emit(`for (auto it = ast->${m.name}; it; it = it->next) {`);
emit(`accept(it->value);`);
if (isCommaSeparated(m, name)) {
emit(`if (it->next) { nospace(); accept.write(","); }`);
if (["enumeratorList"].includes(m.name)) {
emit(
`if (it->next) { nospace(); accept.write(","); newline(); }`,
);
} else {
emit(`if (it->next) { nospace(); accept.write(","); }`);
}
}
emit(`}`);
emit();
Expand Down
12 changes: 6 additions & 6 deletions packages/cxx-gen-ast/src/gen_ast_printer_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ export function gen_ast_printer_cc({
emit(` ++indent_;`);
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
emit(
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`,
);
emit(` --indent_;`);
emit(` }`);
} else if (member.kind == "attribute" && member.type === "int") {
emit(` ++indent_;`);
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
emit(
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name});`,
);
emit(` --indent_;`);
} else if (member.kind == "attribute" && member.type.endsWith("Literal")) {
emit(` if (ast->${member.name}) {`);
emit(` ++indent_;`);
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
emit(
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name}->value());`
` out_ << std::format("${fieldName}: {}\\n", ast->${member.name}->value());`,
);
emit(` --indent_;`);
emit(` }`);
Expand All @@ -90,7 +90,7 @@ export function gen_ast_printer_cc({
emit(` ++indent_;`);
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
emit(
` out_ << std::format("${fieldName}: {}\\n", Token::spell(ast->${member.name}));`
` out_ << std::format("${fieldName}: {}\\n", Token::spell(ast->${member.name}));`,
);
emit(` --indent_;`);
emit(` }`);
Expand All @@ -101,7 +101,7 @@ export function gen_ast_printer_cc({
emit(` ++indent_;`);
emit(` out_ << std::format("{:{}}", "", indent_ * 2);`);
emit(
` out_ << std::format("${fieldName}: {}\\n", to_string(ast->${member.name}));`
` out_ << std::format("${fieldName}: {}\\n", to_string(ast->${member.name}));`,
);
emit(` --indent_;`);
}
Expand All @@ -115,7 +115,7 @@ export function gen_ast_printer_cc({
emit(` out_ << "${astName(name)}";`);
emit(` if (ast->type) {`);
emit(
` out_ << std::format(" [{} {}]", to_string(ast->valueCategory), to_string(ast->type));`
` out_ << std::format(" [{} {}]", to_string(ast->valueCategory), to_string(ast->type));`,
);
emit(` }`);
emit(` out_ << "\\n";`);
Expand Down
10 changes: 5 additions & 5 deletions packages/cxx-gen-ast/src/gen_ast_slot_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
by_base.forEach((nodes) => {
nodes.forEach(({ name, members }) => {
const memberSlots = members.filter(
(m) => classifyMemberSlot(m) !== undefined
(m) => classifyMemberSlot(m) !== undefined,
);

emit();
Expand Down Expand Up @@ -87,7 +87,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.IdentifierAttribute:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
);
emit(` slotKind_ = ASTSlotKind::kIdentifierAttribute;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand All @@ -96,7 +96,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.LiteralAttribute:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
);
emit(` slotKind_ = ASTSlotKind::kLiteralAttribute;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand All @@ -112,7 +112,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.Node:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
);
emit(` slotKind_ = ASTSlotKind::kNode;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand All @@ -121,7 +121,7 @@ export function gen_ast_slot_cc({ ast, output }: { ast: AST; output: string }) {
case MemberSlotClassification.NodeList:
emit(` case ${slotCount}: // ${m.name}`);
emit(
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`
` value_ = reinterpret_cast<std::intptr_t>(ast->${m.name});`,
);
emit(` slotKind_ = ASTSlotKind::kNodeList;`);
emit(` slotNameIndex_ = SlotNameIndex{${slotNameIndex}};`);
Expand Down
8 changes: 4 additions & 4 deletions packages/cxx-gen-ast/src/gen_token_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {

emit("#define FOR_EACH_BASE_TOKEN(V) \\");
tokens.BASE_TOKENS.forEach((tk) =>
emit(` V(${tk}, "${baseTokenId(tk)}") \\`)
emit(` V(${tk}, "${baseTokenId(tk)}") \\`),
);

emit();
emit("#define FOR_EACH_OPERATOR(V) \\");
tokens.OPERATORS.forEach(([tk, spelling]) =>
emit(` V(${tk}, "${spelling}") \\`)
emit(` V(${tk}, "${spelling}") \\`),
);

emit();
Expand All @@ -46,13 +46,13 @@ export function gen_token_fwd_h({ output }: { output: string }) {
emit();
emit("#define FOR_EACH_BUILTIN_TYPE_TRAIT(V) \\");
tokens.BUILTIN_TYPE_TRAITS.forEach((tk) =>
emit(` V(${tk.toUpperCase()}, "${tk}") \\`)
emit(` V(${tk.toUpperCase()}, "${tk}") \\`),
);

emit();
emit("#define FOR_EACH_TOKEN_ALIAS(V) \\");
tokens.TOKEN_ALIASES.forEach(([tk, other]) =>
emit(` V(${tk.toUpperCase()}, ${other}) \\`)
emit(` V(${tk.toUpperCase()}, ${other}) \\`),
);

const out = `${cpy_header}
Expand Down
2 changes: 1 addition & 1 deletion packages/cxx-gen-ast/src/new_ast_op_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function new_ast_op_cc({
const resultTy = `${chopAST(base)}Result`;
emit();
emit(
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`
`auto ${opName}::${className}Visitor::operator()(${name}* ast) -> ${resultTy} {`,
);
members.forEach((m) => {
switch (m.kind) {
Expand Down
Loading