mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
1512 lines
59 KiB
TypeScript
1512 lines
59 KiB
TypeScript
/**
|
|
* Go AST code generator: reads _scripts/ast.json and produces internal/ast/ast_generated.go
|
|
*
|
|
* Usage: node --experimental-strip-types _scripts/generate-go-ast.ts
|
|
*
|
|
* Generates:
|
|
* - Struct definitions for each node kind
|
|
* - As*() cast methods on *Node
|
|
* - New*()/Update*() factory methods on *NodeFactory
|
|
* - ForEachChild() implementations
|
|
* - VisitEachChild() implementations
|
|
* - Clone() implementations
|
|
* - Polymorphic accessor switch statements (Expression(), Type(), etc.)
|
|
* - Is*() type guard functions
|
|
*/
|
|
|
|
import { execaSync } from "execa";
|
|
import * as fs from "node:fs";
|
|
import * as path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import type {
|
|
MemberInfo,
|
|
NodeType,
|
|
Type,
|
|
} from "./schema.ts";
|
|
import {
|
|
api,
|
|
kindGuardName,
|
|
} from "./schema.ts";
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
// Load schema
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
|
|
const ROOT = path.resolve(import.meta.dirname!, "..");
|
|
|
|
// Members that participate in factory/visitor/clone (excludes noFactory)
|
|
function schemaMembers(node: NodeType): MemberInfo[] {
|
|
return node.members.filter(m => !m.noFactory);
|
|
}
|
|
|
|
function isTextContentType(type: Type): boolean {
|
|
if (type.kind === "primitive") {
|
|
return type.name === "string";
|
|
}
|
|
if (type.kind === "list" && type.listKind === "raw") {
|
|
return type.elementType.kind === "primitive" && type.elementType.name === "string";
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Does this node have any string-typed member (triggering textCount++)?
|
|
function hasTextContent(node: NodeType): boolean {
|
|
if (node.members.length === 0) return false;
|
|
return schemaMembers(node).some(m => isTextContentType(m.type));
|
|
}
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
// Code generation
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
|
|
class CodeWriter {
|
|
private lines: string[] = [];
|
|
private indent = 0;
|
|
|
|
write(line: string = "") {
|
|
if (line === "") {
|
|
this.lines.push("");
|
|
}
|
|
else {
|
|
this.lines.push("\t".repeat(this.indent) + line);
|
|
}
|
|
}
|
|
|
|
writeRaw(line: string) {
|
|
this.lines.push(line);
|
|
}
|
|
|
|
push() {
|
|
this.indent++;
|
|
}
|
|
pop() {
|
|
this.indent--;
|
|
}
|
|
|
|
toString(): string {
|
|
return this.lines.join("\n");
|
|
}
|
|
}
|
|
|
|
function generateNodeFactoryStruct(w: CodeWriter) {
|
|
const arenaFields: { fieldName: string; typeName: string; }[] = [];
|
|
for (const node of api.nodes()) {
|
|
if (!node.arena) continue;
|
|
arenaFields.push({
|
|
fieldName: `${api.uncapitalize(node.name)}Arena`,
|
|
typeName: node.name,
|
|
});
|
|
}
|
|
arenaFields.push({ fieldName: "modifierListArena", typeName: "ModifierList" });
|
|
arenaFields.push({ fieldName: "nodeListArena", typeName: "NodeList" });
|
|
arenaFields.sort((a, b) => a.fieldName.localeCompare(b.fieldName));
|
|
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// NodeFactory");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
w.write("type NodeFactory struct {");
|
|
w.push();
|
|
w.write("hooks NodeFactoryHooks");
|
|
for (const { fieldName, typeName } of arenaFields) {
|
|
w.write(`${fieldName} core.Arena[${typeName}]`);
|
|
}
|
|
w.write("");
|
|
w.write("nodeCount int");
|
|
w.write("textCount int");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateHeader(w: CodeWriter) {
|
|
w.write("// Code generated by _scripts/generate-go-ast.ts. DO NOT EDIT.");
|
|
w.write("");
|
|
w.write("package ast");
|
|
w.write("");
|
|
w.write("import (");
|
|
w.push();
|
|
w.write('"sync/atomic"');
|
|
w.write('"unsafe"');
|
|
w.write("");
|
|
w.write('"github.com/microsoft/typescript-go/internal/core"');
|
|
w.pop();
|
|
w.write(")");
|
|
w.write("");
|
|
w.write("var _ = core.Same[string] // prevent unused import");
|
|
w.write("var _ atomic.Uint32 // prevent unused import");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate struct definitions ────────────────────────────────────────────
|
|
|
|
/**
|
|
* Verifies that nothing inherits a base along more than one path.
|
|
*/
|
|
function verifyNoDuplicateBases(): void {
|
|
const problems: string[] = [];
|
|
const check = (name: string, extendsKeys: string[]) => {
|
|
const copies = new Map<string, number>();
|
|
const walk = (keys: string[]) => {
|
|
for (const key of keys) {
|
|
copies.set(key, (copies.get(key) ?? 0) + 1);
|
|
walk(api.getBase(key)?.extendsKeys ?? []);
|
|
}
|
|
};
|
|
walk(extendsKeys);
|
|
for (const [base, count] of copies) {
|
|
if (count > 1) problems.push(`${name} inherits ${base} ${count} times`);
|
|
}
|
|
};
|
|
for (const base of api.bases()) check(base.key, base.extendsKeys);
|
|
for (const node of api.nodes()) check(node.name, node.extendsKeys);
|
|
|
|
if (problems.length > 0) {
|
|
throw new Error(`ast.json declares duplicate embedded bases:\n ${problems.sort().join("\n ")}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Verifies that the inheritance path containing NodeBase is embedded first.
|
|
*/
|
|
function verifyNodeBaseAtOffsetZero(): void {
|
|
const containsNodeBase = (key: string): boolean => {
|
|
if (key === "NodeBase") return true;
|
|
return api.getBase(key)?.extendsKeys.some(containsNodeBase) ?? false;
|
|
};
|
|
const problems: string[] = [];
|
|
const check = (name: string, extendsKeys: string[], requireNodeBase: boolean) => {
|
|
const nodeBaseIndex = extendsKeys.findIndex(containsNodeBase);
|
|
if (nodeBaseIndex < 0 && requireNodeBase) {
|
|
problems.push(`${name} does not embed NodeBase`);
|
|
}
|
|
else if (nodeBaseIndex > 0) {
|
|
problems.push(`${name} embeds ${extendsKeys[nodeBaseIndex]} after ${extendsKeys.slice(0, nodeBaseIndex).join(", ")}`);
|
|
}
|
|
};
|
|
for (const base of api.bases()) check(base.key, base.extendsKeys, false);
|
|
for (const node of api.nodes()) check(node.name, node.extendsKeys, true);
|
|
|
|
if (problems.length > 0) {
|
|
throw new Error(`ast.json does not embed NodeBase at offset zero:\n ${problems.sort().join("\n ")}`);
|
|
}
|
|
}
|
|
|
|
verifyNoDuplicateBases();
|
|
verifyNodeBaseAtOffsetZero();
|
|
|
|
// A base with no fields and no extends is a struct{} marker. Go pads a struct whose last field is
|
|
// zero-size (so the field's address stays in bounds), so markers are embedded first, never last; being
|
|
// zero-size, they don't move NodeBase off offset zero.
|
|
function isZeroSizeBase(key: string): boolean {
|
|
const base = api.getBase(key);
|
|
return !!base && base.extendsKeys.length === 0 && base.fields.length === 0;
|
|
}
|
|
|
|
function goEmbeds(extendsKeys: string[]): string[] {
|
|
const zeroSized: string[] = [];
|
|
const nonZeroSized: string[] = [];
|
|
for (const key of extendsKeys) {
|
|
if (isZeroSizeBase(key)) {
|
|
zeroSized.push(key);
|
|
}
|
|
else {
|
|
nonZeroSized.push(key);
|
|
}
|
|
}
|
|
return [...zeroSized, ...nonZeroSized];
|
|
}
|
|
|
|
function generateStructDef(w: CodeWriter, node: NodeType) {
|
|
const structName = node.name;
|
|
w.write(`type ${structName} struct {`);
|
|
w.push();
|
|
|
|
// Embeddings from extends (each maps to a Go struct via convention)
|
|
for (const ext of goEmbeds(node.extendsKeys)) {
|
|
w.write(ext);
|
|
}
|
|
|
|
// Fields from members (skip inherited, Kind params, and noGo)
|
|
if (node.members.length > 0) {
|
|
for (const m of node.members) {
|
|
if (m.inherited || m.isKindParam() || m.noGo) continue;
|
|
const fieldName = m.name;
|
|
const goType = m.goOnly ? m.rawType as string : m.type.formatGoReference();
|
|
const comment = buildFieldComment(m);
|
|
if (comment) {
|
|
w.write(`${fieldName} ${goType} ${comment}`);
|
|
}
|
|
else {
|
|
w.write(`${fieldName} ${goType}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function buildFieldComment(m: MemberInfo): string {
|
|
const parts: string[] = [];
|
|
if (m.optional) parts.push("Optional");
|
|
if (parts.length > 0) return `// ${parts.join(". ")}`;
|
|
return "";
|
|
}
|
|
|
|
function goSubtreeFactsTerm(m: MemberInfo): string {
|
|
const access = `node.${m.name}`;
|
|
if (m.listKind === "ModifierList") {
|
|
return `propagateModifierListSubtreeFacts(${access})`;
|
|
}
|
|
if (m.type.kind === "list") {
|
|
return `propagateNodeListSubtreeFacts(${access}, propagateSubtreeFacts)`;
|
|
}
|
|
return `propagateSubtreeFacts(${access})`;
|
|
}
|
|
|
|
function generateSubtreeFacts(w: CodeWriter, node: NodeType) {
|
|
if (!node.generateSubtreeFacts) return;
|
|
|
|
const childMembers = schemaMembers(node).filter(m => m.isChild());
|
|
const structName = node.name;
|
|
w.write(`func (node *${structName}) computeSubtreeFacts() SubtreeFacts {`);
|
|
w.push();
|
|
if (childMembers.length === 0) {
|
|
w.write("return SubtreeFactsNone");
|
|
}
|
|
else {
|
|
for (let index = 0; index < childMembers.length; index++) {
|
|
const term = goSubtreeFactsTerm(childMembers[index]);
|
|
if (index === 0) {
|
|
const suffix = childMembers.length > 1 ? " |" : "";
|
|
w.write(`return ${term}${suffix}`);
|
|
}
|
|
else {
|
|
const suffix = index < childMembers.length - 1 ? " |" : "";
|
|
w.write(`${term}${suffix}`);
|
|
}
|
|
}
|
|
}
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate base struct definitions ──────────────────────────────────────
|
|
|
|
// Bases that are hand-written and should not be generated
|
|
const HAND_WRITTEN_BASES = new Set(["NodeBase"]);
|
|
|
|
function generateBaseStructDefs(w: CodeWriter) {
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// Base struct definitions");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
|
|
for (const base of api.bases()) {
|
|
if (HAND_WRITTEN_BASES.has(base.key)) continue;
|
|
|
|
const structName = base.key;
|
|
|
|
const goExts = goEmbeds(base.extendsKeys);
|
|
|
|
w.write(`type ${structName} struct {`);
|
|
w.push();
|
|
|
|
// Embeddings from extends
|
|
for (const ext of goExts) {
|
|
w.write(ext);
|
|
}
|
|
|
|
// Schema fields
|
|
if (base.fields.length > 0) {
|
|
for (const field of base.fields) {
|
|
if (field.noGo) continue;
|
|
const goType = field.goOnly ? field.rawType as string : field.type.formatGoReference();
|
|
const comment = field.optional ? " // Optional" : "";
|
|
w.write(`${field.name} ${goType}${comment}`);
|
|
}
|
|
}
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
}
|
|
|
|
// ── Generate As*() cast methods ────────────────────────────────────────────
|
|
|
|
function generateAsCast(w: CodeWriter, node: NodeType, canonicalKinds: Map<NodeType, string[]>) {
|
|
w.write(`func (n *Node) As${node.name}() *${node.name} {`);
|
|
w.push();
|
|
w.write("switch n.Kind {");
|
|
const kinds = canonicalKinds.get(node);
|
|
if (kinds) {
|
|
w.write(`case ${kinds.join(", ")}:`);
|
|
w.push();
|
|
w.write(`return (*${node.name})(unsafe.Pointer(n))`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write(`panic("Cannot cast " + n.Kind.String() + " to ${node.name}")`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate New*() factory methods ────────────────────────────────────────
|
|
|
|
function canonicalNodesByKind(): Map<NodeType, string[]> {
|
|
// Token's kind union overlaps the concrete identifier, literal, and keyword
|
|
// nodes. Later schema entries are the canonical representation for those
|
|
// kinds.
|
|
const nodeByKind = new Map<string, NodeType>();
|
|
for (const node of api.nodes()) {
|
|
for (const kind of node.allKinds()) {
|
|
nodeByKind.set(kind.formatGoConstant(), node);
|
|
}
|
|
}
|
|
|
|
const kindsByNode = new Map<NodeType, string[]>();
|
|
for (const [kind, node] of nodeByKind) {
|
|
const kinds = kindsByNode.get(node);
|
|
if (kinds) {
|
|
kinds.push(kind);
|
|
}
|
|
else {
|
|
kindsByNode.set(node, [kind]);
|
|
}
|
|
}
|
|
return kindsByNode;
|
|
}
|
|
|
|
function extendsBase(extendsKeys: string[], baseKey: string): boolean {
|
|
return extendsKeys.some(key => key === baseKey || extendsBase(api.getBase(key)?.extendsKeys ?? [], baseKey));
|
|
}
|
|
|
|
function isNodeFlagsMember(m: MemberInfo): boolean {
|
|
const type = m.type;
|
|
return type.kind === "primitive" && type.name === "NodeFlags";
|
|
}
|
|
|
|
function emitNewFactory(
|
|
w: CodeWriter,
|
|
funcName: string,
|
|
kindName: string,
|
|
structName: string,
|
|
node: NodeType,
|
|
members: MemberInfo[],
|
|
kindMember: MemberInfo | undefined,
|
|
nodeFlagsMembers: MemberInfo[],
|
|
) {
|
|
const params = members.map(m => `${m.goParamName()} ${m.type.formatGoReference()}`).join(", ");
|
|
|
|
w.write(`func (f *NodeFactory) ${funcName}(${params}) *Node {`);
|
|
w.push();
|
|
|
|
if (node.arena) {
|
|
w.write(`data := f.${api.uncapitalize(structName)}Arena.New()`);
|
|
}
|
|
else {
|
|
w.write(`data := &${structName}{}`);
|
|
}
|
|
|
|
for (const m of members) {
|
|
if (m.isKindParam()) continue;
|
|
if (isNodeFlagsMember(m)) continue;
|
|
const value = m.bitmask ? `${m.goParamName()} & ${m.bitmask}` : m.goParamName();
|
|
w.write(`data.${m.name} = ${value}`);
|
|
}
|
|
|
|
if (hasTextContent(node)) {
|
|
w.write("f.textCount++");
|
|
}
|
|
|
|
const kindArg = kindMember ? kindMember.goParamName() : `Kind${kindName}`;
|
|
|
|
if (nodeFlagsMembers.length > 0) {
|
|
w.write(`node := f.newNode(${kindArg}, data.AsNode())`);
|
|
for (const m of nodeFlagsMembers) {
|
|
const param = m.goParamName();
|
|
if (m.bitmask) {
|
|
w.write(`node.Flags |= ${param} & ${m.bitmask}`);
|
|
}
|
|
else {
|
|
w.write(`node.Flags = ${param}`);
|
|
}
|
|
}
|
|
w.write("return node");
|
|
}
|
|
else {
|
|
w.write(`return f.newNode(${kindArg}, data.AsNode())`);
|
|
}
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateNewFactory(w: CodeWriter, node: NodeType) {
|
|
const structName = node.name;
|
|
const members = schemaMembers(node);
|
|
const kindMember = members.find(m => m.isKindParam());
|
|
const nodeFlagsMembers = members.filter(m => isNodeFlagsMember(m));
|
|
|
|
if (node.name === "Token") {
|
|
const tokenKinds = new Set(node.allKinds().map(kind => kind.formatGoConstant()));
|
|
w.write("func (f *NodeFactory) NewToken(kind TokenSyntaxKind) *Node {");
|
|
w.push();
|
|
w.write("switch kind {");
|
|
for (const [canonicalNode, kinds] of canonicalNodesByKind()) {
|
|
if (canonicalNode === node) continue;
|
|
const overlappingKinds = kinds.filter(kind => tokenKinds.has(kind));
|
|
if (overlappingKinds.length === 0) continue;
|
|
w.write(`case ${overlappingKinds.join(", ")}:`);
|
|
w.push();
|
|
if (canonicalNode.arena) {
|
|
w.write(`data := f.${api.uncapitalize(canonicalNode.name)}Arena.New()`);
|
|
}
|
|
else {
|
|
w.write(`data := &${canonicalNode.name}{}`);
|
|
}
|
|
w.write("return f.newNode(kind, data.AsNode())");
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write("data := f.tokenArena.New()");
|
|
w.write("return f.newNode(kind, data.AsNode())");
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
else {
|
|
emitNewFactory(w, `New${node.name}`, node.syntaxKindName, structName, node, members, kindMember, nodeFlagsMembers);
|
|
}
|
|
for (const alias of node.kindAliases) {
|
|
emitNewFactory(w, `New${alias}`, alias, structName, node, members, kindMember, nodeFlagsMembers);
|
|
}
|
|
}
|
|
|
|
// ── Generate Update*() factory methods ─────────────────────────────────────
|
|
|
|
function generateUpdateFactory(w: CodeWriter, node: NodeType) {
|
|
const structName = node.name;
|
|
const members = schemaMembers(node);
|
|
|
|
// All non-Kind members participate in Update (both child and data)
|
|
const updateMembers = members.filter(m => !m.isKindParam());
|
|
if (updateMembers.length === 0) return;
|
|
|
|
// Leaf nodes (no child AST node members) don't get Update methods.
|
|
if (!updateMembers.some(m => m.isChild())) return;
|
|
|
|
// Build parameter list
|
|
const params = [
|
|
`node *${structName}`,
|
|
...updateMembers.map(m => `${m.goParamName()} ${m.type.formatGoReference()}`),
|
|
].join(", ");
|
|
|
|
w.write(`func (f *NodeFactory) Update${node.name}(${params}) *Node {`);
|
|
w.push();
|
|
|
|
// Build comparison (all update members)
|
|
const comparisons = updateMembers.map(m => {
|
|
const type = m.type;
|
|
// Slices can't be compared with != in Go
|
|
if (type.kind === "list" && type.listKind === "raw") {
|
|
return `!core.Same(${m.goParamName()}, node.${m.name})`;
|
|
}
|
|
return `${m.goParamName()} != node.${m.name}`;
|
|
});
|
|
|
|
w.write(`if ${comparisons.join(" || ")} {`);
|
|
w.push();
|
|
|
|
// Call New with all members (Kind from original, everything else from params)
|
|
const newArgs = members.map(m => {
|
|
if (m.isKindParam()) {
|
|
return "node.Kind";
|
|
}
|
|
return m.goParamName();
|
|
}).join(", ");
|
|
|
|
if (node.kindAliases.length > 0) {
|
|
w.write("switch node.Kind {");
|
|
w.push();
|
|
w.write(`case ${api.kindType(`SyntaxKind.${node.syntaxKindName}`).formatGoConstant()}:`);
|
|
w.push();
|
|
w.write(`return updateNode(f.New${node.name}(${newArgs}), node.AsNode(), f.hooks)`);
|
|
w.pop();
|
|
for (const alias of node.kindAliases) {
|
|
w.write(`case Kind${alias}:`);
|
|
w.push();
|
|
w.write(`return updateNode(f.New${alias}(${newArgs}), node.AsNode(), f.hooks)`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write(`panic("unexpected kind in Update${node.name}: " + node.Kind.String())`);
|
|
w.pop();
|
|
w.pop();
|
|
w.write("}");
|
|
}
|
|
else {
|
|
w.write(`return updateNode(f.New${node.name}(${newArgs}), node.AsNode(), f.hooks)`);
|
|
}
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("return node.AsNode()");
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate ForEachChild() ────────────────────────────────────────────────
|
|
|
|
function generateForEachChild(w: CodeWriter, node: NodeType) {
|
|
const members = schemaMembers(node);
|
|
const childMembers = members.filter(m => m.isChild());
|
|
|
|
if (childMembers.length === 0) {
|
|
// No children - empty implementation (already provided by NodeDefault)
|
|
return;
|
|
}
|
|
|
|
const structName = node.name;
|
|
w.write(`func (node *${structName}) ForEachChild(v Visitor) bool {`);
|
|
w.push();
|
|
|
|
// Nodes with runtime-dependent child ordering delegate to a hand-written function.
|
|
if (node.handWrittenVisitor) {
|
|
w.write(`return forEachChild_${structName}(node, v)`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
return;
|
|
}
|
|
|
|
// Build visit chain: visit(v, node.Field) || visitNodeList(v, node.Field) || ...
|
|
const parts = childMembers.map(m => {
|
|
const access = `node.${m.name}`;
|
|
const type = m.type;
|
|
const listKind = m.listKind;
|
|
if (listKind === "raw") {
|
|
return `visitNodes(v, ${access})`;
|
|
}
|
|
if (listKind === "ModifierList") {
|
|
return `visitModifiers(v, ${access})`;
|
|
}
|
|
if (listKind === "NodeList") {
|
|
return `visitNodeList(v, ${access})`;
|
|
}
|
|
return `visit(v, ${access})`;
|
|
});
|
|
|
|
// Join with || across multiple lines if needed
|
|
if (parts.length <= 3) {
|
|
w.write(`return ${parts.join(" || ")}`);
|
|
}
|
|
else {
|
|
w.write(`return ${parts[0]} ||`);
|
|
for (let i = 1; i < parts.length - 1; i++) {
|
|
w.write(`\t${parts[i]} ||`);
|
|
}
|
|
w.write(`\t${parts[parts.length - 1]}`);
|
|
}
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate ForEachChild dispatch ─────────────────────────────────────────
|
|
|
|
// Determines whether a node has a meaningful ForEachChild implementation (i.e.
|
|
// one that is not the no-op inherited from NodeDefault). This is true for any
|
|
// generated node with child members, or for hand-written nodes (e.g. SourceFile)
|
|
// which define ForEachChild manually in ast.go.
|
|
function hasForEachChild(node: NodeType): boolean {
|
|
if (node.handWritten) return true;
|
|
return schemaMembers(node).some(m => m.isChild());
|
|
}
|
|
|
|
// Generates a (*Node).ForEachChild method that dispatches on node.Kind to the
|
|
// concrete node type's ForEachChild method.
|
|
//
|
|
// Dispatching through a Kind switch to a statically-resolved concrete method
|
|
// lets escape analysis prove the visitor does not escape, keeping caller
|
|
// closures on the stack. The integer switch over Kind also compiles to a jump
|
|
// table.
|
|
//
|
|
// Kinds whose node has no children fall through to `default` and return false,
|
|
// matching NodeDefault.ForEachChild.
|
|
function generateForEachChildDispatch(w: CodeWriter) {
|
|
w.write("func (n *Node) ForEachChild(v Visitor) bool {");
|
|
w.push();
|
|
w.write("switch n.Kind {");
|
|
for (const node of api.nodes()) {
|
|
if (!hasForEachChild(node)) continue;
|
|
const kinds = node.allKinds().map(k => k.formatGoConstant());
|
|
if (kinds.length === 0) continue;
|
|
w.write(`case ${kinds.join(", ")}:`);
|
|
w.push();
|
|
w.write(`return (*${node.name})(unsafe.Pointer(n)).ForEachChild(v)`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write("return false");
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateVisitEachChildDispatch(w: CodeWriter) {
|
|
w.write("func (n *Node) VisitEachChild(v *NodeVisitor) *Node {");
|
|
w.push();
|
|
w.write("switch n.Kind {");
|
|
for (const node of api.nodes()) {
|
|
if (!hasForEachChild(node)) continue;
|
|
const kinds = node.allKinds().map(k => k.formatGoConstant());
|
|
if (kinds.length === 0) continue;
|
|
w.write(`case ${kinds.join(", ")}:`);
|
|
w.push();
|
|
w.write(`return (*${node.name})(unsafe.Pointer(n)).VisitEachChild(v)`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write("return n");
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateSubtreeFactsDispatch(w: CodeWriter) {
|
|
w.write("func (n *Node) SubtreeFacts() SubtreeFacts {");
|
|
w.push();
|
|
w.write("switch n.Kind {");
|
|
w.write("case kindFlowSwitchClauseData:");
|
|
w.push();
|
|
w.write("return SubtreeFactsNone");
|
|
w.pop();
|
|
w.write("case kindFlowReduceLabelData:");
|
|
w.push();
|
|
w.write("return SubtreeFactsNone");
|
|
w.pop();
|
|
for (const [node, kinds] of canonicalNodesByKind()) {
|
|
w.write(`case ${kinds.join(", ")}:`);
|
|
w.push();
|
|
if (extendsBase(node.extendsKeys, "CompositeBase")) {
|
|
w.write(`data := (*${node.name})(unsafe.Pointer(n))`);
|
|
w.write("return data.subtreeFactsWorker(data.AsNode())");
|
|
}
|
|
else {
|
|
w.write(`return (*${node.name})(unsafe.Pointer(n)).computeSubtreeFacts()`);
|
|
}
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write('panic("Unhandled case in Node.SubtreeFacts: " + n.Kind.String())');
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generatePropagateSubtreeFactsDispatch(w: CodeWriter) {
|
|
const exclusions = new Map<string, string>([
|
|
["KindCatchClause", "SubtreeExclusionsCatchClause"],
|
|
["KindVariableDeclarationList", "SubtreeExclusionsVariableDeclarationList"],
|
|
["KindObjectBindingPattern", "SubtreeExclusionsBindingPattern"],
|
|
["KindArrayBindingPattern", "SubtreeExclusionsBindingPattern"],
|
|
["KindParameter", "SubtreeExclusionsParameter"],
|
|
["KindFunctionDeclaration", "SubtreeExclusionsFunction"],
|
|
["KindClassDeclaration", "SubtreeExclusionsClass"],
|
|
["KindClassExpression", "SubtreeExclusionsClass"],
|
|
["KindModuleDeclaration", "SubtreeExclusionsModule"],
|
|
["KindConstructor", "SubtreeExclusionsConstructor"],
|
|
["KindArrowFunction", "SubtreeExclusionsArrowFunction"],
|
|
["KindFunctionExpression", "SubtreeExclusionsFunction"],
|
|
["KindAsExpression", "SubtreeExclusionsOuterExpression"],
|
|
["KindSatisfiesExpression", "SubtreeExclusionsOuterExpression"],
|
|
["KindPropertyAccessExpression", "SubtreeExclusionsPropertyAccess"],
|
|
["KindElementAccessExpression", "SubtreeExclusionsElementAccess"],
|
|
["KindCallExpression", "SubtreeExclusionsCall"],
|
|
["KindNewExpression", "SubtreeExclusionsNew"],
|
|
["KindArrayLiteralExpression", "SubtreeExclusionsArrayLiteral"],
|
|
["KindObjectLiteralExpression", "SubtreeExclusionsObjectLiteral"],
|
|
["KindTypeAssertionExpression", "SubtreeExclusionsOuterExpression"],
|
|
]);
|
|
w.write("var nodePropagateSubtreeFactsExclusions = [KindCount]SubtreeFacts{");
|
|
w.push();
|
|
for (const [kind, exclusion] of exclusions) {
|
|
w.write(`${kind}: ${exclusion},`);
|
|
}
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
w.write("func (n *Node) propagateSubtreeFacts() SubtreeFacts {");
|
|
w.push();
|
|
w.write("facts := n.SubtreeFacts()");
|
|
w.write("switch n.Kind {");
|
|
w.write("case KindGetAccessor, KindSetAccessor:");
|
|
w.push();
|
|
w.write("return facts & ^SubtreeExclusionsAccessor | propagateSubtreeFacts(n.Name())");
|
|
w.pop();
|
|
w.write("case KindMethodDeclaration:");
|
|
w.push();
|
|
w.write("return facts & ^SubtreeExclusionsMethod | propagateSubtreeFacts(n.Name())");
|
|
w.pop();
|
|
w.write("case KindPropertyDeclaration:");
|
|
w.push();
|
|
w.write("return facts & ^SubtreeExclusionsProperty | propagateSubtreeFacts(n.Name())");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("kind := uint16(n.Kind)");
|
|
w.write("if kind >= uint16(len(nodePropagateSubtreeFactsExclusions)) {");
|
|
w.push();
|
|
w.write("return facts");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("return facts & ^nodePropagateSubtreeFactsExclusions[kind]");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateComputeSubtreeFactsDispatch(w: CodeWriter) {
|
|
generateNodeDataAccessorDispatch(w, "computeSubtreeFacts", "SubtreeFacts", () => true, "SubtreeFactsNone");
|
|
}
|
|
|
|
function generateNodeDataAccessorDispatch(
|
|
w: CodeWriter,
|
|
methodName: string,
|
|
returnType: string,
|
|
handlesNode: (node: NodeType) => boolean,
|
|
defaultValue: string,
|
|
) {
|
|
w.write(`func (n *Node) ${methodName}() ${returnType} {`);
|
|
w.push();
|
|
w.write("switch n.Kind {");
|
|
for (const [node, kinds] of canonicalNodesByKind()) {
|
|
if (!handlesNode(node)) continue;
|
|
w.write(`case ${kinds.join(", ")}:`);
|
|
w.push();
|
|
w.write(`return (*${node.name})(unsafe.Pointer(n)).${methodName}()`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write(`return ${defaultValue}`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateNodeOffsetAccessorDispatch(
|
|
w: CodeWriter,
|
|
methodName: string,
|
|
returnType: string,
|
|
selector: string | ((node: NodeType) => string),
|
|
handlesNode: (node: NodeType) => boolean,
|
|
indirect: boolean,
|
|
unsupported = "return nil",
|
|
directIndex = false,
|
|
handlesKind: (kind: string) => boolean = () => true,
|
|
) {
|
|
const offsetsName = `node${methodName}Offsets`;
|
|
const tableLength = directIndex ? "kindFlowReduceLabelData + 1" : "KindCount";
|
|
w.write(`var ${offsetsName} = [${tableLength}]uint16{`);
|
|
w.push();
|
|
for (const [node, kinds] of canonicalNodesByKind()) {
|
|
if (!handlesNode(node)) continue;
|
|
for (const kind of kinds) {
|
|
if (!handlesKind(kind)) continue;
|
|
const nodeSelector = typeof selector === "string" ? selector : selector(node);
|
|
w.write(`${kind}: uint16(unsafe.Offsetof(${node.name}{}.${nodeSelector})) + 1,`);
|
|
}
|
|
}
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
w.write(`func (n *Node) ${methodName}() ${returnType} {`);
|
|
w.push();
|
|
const kind = directIndex ? "n.Kind" : "kind";
|
|
if (!directIndex) {
|
|
w.write("kind := uint16(n.Kind)");
|
|
w.write(`if ${kind} >= uint16(len(${offsetsName})) {`);
|
|
w.push();
|
|
w.write(unsupported);
|
|
w.pop();
|
|
w.write("}");
|
|
}
|
|
w.write(`offset := ${offsetsName}[${kind}]`);
|
|
w.write("if offset == 0 {");
|
|
w.push();
|
|
w.write(unsupported);
|
|
w.pop();
|
|
w.write("}");
|
|
const pointer = "unsafe.Add(unsafe.Pointer(n), uintptr(offset-1))";
|
|
if (indirect) {
|
|
w.write(`return *(*${returnType})(${pointer})`);
|
|
}
|
|
else {
|
|
w.write(`return (${returnType})(${pointer})`);
|
|
}
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateNodeTextOffsets(w: CodeWriter) {
|
|
w.write("var nodeTextOffsets = [kindFlowReduceLabelData + 1]uint16{");
|
|
w.push();
|
|
for (const [node, kinds] of canonicalNodesByKind()) {
|
|
const hasText = schemaMembers(node).some(m =>
|
|
m.name === "Text" &&
|
|
m.type.kind === "primitive" &&
|
|
m.type.name === "string"
|
|
) && node.name !== "JsxText";
|
|
if (!hasText) continue;
|
|
for (const kind of kinds) {
|
|
w.write(`${kind}: uint16(unsafe.Offsetof(${node.name}{}.Text)) + 1,`);
|
|
}
|
|
}
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateCloneDispatch(w: CodeWriter) {
|
|
w.write("func (n *Node) Clone(f NodeFactoryCoercible) *Node {");
|
|
w.push();
|
|
w.write("switch n.Kind {");
|
|
w.write("case kindFlowSwitchClauseData, kindFlowReduceLabelData:");
|
|
w.push();
|
|
w.write("return nil");
|
|
w.pop();
|
|
for (const [node, kinds] of canonicalNodesByKind()) {
|
|
w.write(`case ${kinds.join(", ")}:`);
|
|
w.push();
|
|
w.write(`return (*${node.name})(unsafe.Pointer(n)).Clone(f)`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write('panic("Unhandled case in Node.Clone: " + n.Kind.String())');
|
|
w.pop();
|
|
w.write("}");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
function generateSetModifiersDispatch(w: CodeWriter) {
|
|
w.write("func (n *MutableNode) SetModifiers(modifiers *ModifierList) {");
|
|
w.push();
|
|
w.write("node := (*Node)(unsafe.Pointer(n))");
|
|
w.write("kind := uint16(node.Kind)");
|
|
w.write("if kind >= uint16(len(nodeModifiersOffsets)) {");
|
|
w.push();
|
|
w.write('panic("Cannot set modifiers on " + node.Kind.String())');
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("offset := nodeModifiersOffsets[kind]");
|
|
w.write("if offset == 0 {");
|
|
w.push();
|
|
w.write('panic("Cannot set modifiers on " + node.Kind.String())');
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("*(**ModifierList)(unsafe.Add(unsafe.Pointer(node), uintptr(offset-1))) = modifiers");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate VisitEachChild() ──────────────────────────────────────────────
|
|
|
|
function generateVisitEachChild(w: CodeWriter, node: NodeType) {
|
|
const members = schemaMembers(node);
|
|
const childMembers = members.filter(m => m.isChild());
|
|
|
|
if (childMembers.length === 0) return;
|
|
|
|
const structName = node.name;
|
|
w.write(`func (node *${structName}) VisitEachChild(v *NodeVisitor) *Node {`);
|
|
w.push();
|
|
|
|
// Nodes with runtime-dependent child ordering delegate to a hand-written function.
|
|
if (node.handWrittenVisitor) {
|
|
w.write(`return visitEachChild_${structName}(node, v)`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
return;
|
|
}
|
|
|
|
// For raw slice lists, emit local variables with core.SameMap
|
|
const updateMembers = members.filter(m => !m.isKindParam());
|
|
const rawListLocals: Map<string, string> = new Map();
|
|
for (const m of updateMembers) {
|
|
const type = m.type;
|
|
if (m.isChild() && type.kind === "list" && type.listKind === "raw") {
|
|
const localName = m.goParamName();
|
|
rawListLocals.set(m.name, localName);
|
|
w.write(`${localName} := core.SameMap(node.${m.name}, func(n *Node) *Node { return v.visitNode(n) })`);
|
|
}
|
|
}
|
|
|
|
const args = updateMembers.map(m => {
|
|
if (!m.isChild()) {
|
|
return `node.${m.name}`;
|
|
}
|
|
|
|
// Raw list: use the local variable
|
|
if (rawListLocals.has(m.name)) {
|
|
return rawListLocals.get(m.name)!;
|
|
}
|
|
|
|
const access = `node.${m.name}`;
|
|
const type = m.type;
|
|
const listKind = m.listKind;
|
|
|
|
if (m.visit) {
|
|
return `v.visit${api.capitalize(m.visit)}(${access})`;
|
|
}
|
|
if (listKind === "ModifierList") {
|
|
return `v.visitModifiers(${access})`;
|
|
}
|
|
if (listKind === "NodeList") {
|
|
return `v.visitNodes(${access})`;
|
|
}
|
|
if (type.kind === "primitive" && type.name === "Token") return `v.visitToken(${access})`;
|
|
return `v.visitNode(${access})`;
|
|
}).join(", ");
|
|
|
|
w.write(`return v.Factory.Update${node.name}(node, ${args})`);
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate Clone() ───────────────────────────────────────────────────────
|
|
|
|
function generateClone(w: CodeWriter, node: NodeType) {
|
|
const structName = node.name;
|
|
const members = schemaMembers(node);
|
|
|
|
w.write(`func (node *${structName}) Clone(f NodeFactoryCoercible) *Node {`);
|
|
w.push();
|
|
|
|
// Build args for New* call
|
|
// Clone is a struct method, so it can access private fields directly
|
|
const args = members.map(m => {
|
|
if (m.isKindParam()) {
|
|
return "node.Kind";
|
|
}
|
|
const type = m.declaredType;
|
|
|
|
if (m.inherited && type?.kind === "alias" && type.name === "ModifierLike") {
|
|
return "node.Modifiers()";
|
|
}
|
|
// Use direct field access (Clone is a struct method, can access private fields)
|
|
return `node.${m.name}`;
|
|
}).join(", ");
|
|
|
|
if (node.kindAliases.length > 0) {
|
|
w.write("switch node.Kind {");
|
|
w.push();
|
|
w.write(`case ${api.kindType(`SyntaxKind.${node.syntaxKindName}`).formatGoConstant()}:`);
|
|
w.push();
|
|
w.write(`return cloneNode(f.AsNodeFactory().New${node.name}(${args}), node.AsNode(), f.AsNodeFactory().hooks)`);
|
|
w.pop();
|
|
for (const alias of node.kindAliases) {
|
|
w.write(`case Kind${alias}:`);
|
|
w.push();
|
|
w.write(`return cloneNode(f.AsNodeFactory().New${alias}(${args}), node.AsNode(), f.AsNodeFactory().hooks)`);
|
|
w.pop();
|
|
}
|
|
w.write("default:");
|
|
w.push();
|
|
w.write(`panic("unexpected kind in ${structName}.Clone: " + node.Kind.String())`);
|
|
w.pop();
|
|
w.pop();
|
|
w.write("}");
|
|
}
|
|
else {
|
|
w.write(`return cloneNode(f.AsNodeFactory().New${node.name}(${args}), node.AsNode(), f.AsNodeFactory().hooks)`);
|
|
}
|
|
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ── Generate Is*() functions ───────────────────────────────────────────────
|
|
|
|
function generateIsFunction(w: CodeWriter, node: NodeType) {
|
|
const kindTypes = node.kindTypes();
|
|
if (node.kindType.kind === "typeParameter") {
|
|
w.write(`func Is${node.name}(node *Node) bool {`);
|
|
w.push();
|
|
w.write("switch node.Kind {");
|
|
w.write(`case ${kindTypes.map(kind => kind.formatGoConstant()).join(", ")}:`);
|
|
w.push();
|
|
w.write("return true");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("return false");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
return;
|
|
}
|
|
|
|
if (node.isMultiKind()) {
|
|
for (const kind of kindTypes) {
|
|
const kindName = kind.name;
|
|
w.write(`func Is${kindName}(node *Node) bool {`);
|
|
w.push();
|
|
w.write(`return node.Kind == ${kind.formatGoConstant()}`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
return;
|
|
}
|
|
|
|
w.write(`func Is${node.name}(node *Node) bool {`);
|
|
w.push();
|
|
w.write(`return node.Kind == ${api.kindType(`SyntaxKind.${node.syntaxKindName}`).formatGoConstant()}`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
for (const alias of node.kindAliases) {
|
|
w.write(`func Is${alias}(node *Node) bool {`);
|
|
w.push();
|
|
w.write(`return node.Kind == Kind${alias}`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
}
|
|
|
|
// ── Generate kind alias guards ─────────────────────────────────────────────
|
|
|
|
/** Go guard name: "IsTokenKind", "IsKeywordKind", etc. Capital I, no "Syntax". */
|
|
function goKindGuardName(aliasName: string): string {
|
|
const tsName = kindGuardName(aliasName); // e.g. "isTokenKind"
|
|
return tsName.charAt(0).toUpperCase() + tsName.slice(1); // e.g. "IsTokenKind"
|
|
}
|
|
|
|
function generateKindAliasGuards(w: CodeWriter) {
|
|
// JSDocSyntaxKind → "IsJSDocKind" conflicts with the hand-written IsJSDocKind in utilities.go
|
|
// which checks JSDoc NODE kinds (FirstJSDocNode..LastJSDocNode), a completely different semantic.
|
|
const skipGuards = new Set(["IsJSDocKind"]);
|
|
|
|
for (const guard of api.kindGuards()) {
|
|
const funcName = goKindGuardName(guard.aliasName);
|
|
if (skipGuards.has(funcName)) continue;
|
|
|
|
if (guard.type === "range") {
|
|
w.write(`func ${funcName}(kind Kind) bool {`);
|
|
w.push();
|
|
w.write(`return kind >= Kind${guard.first} && kind <= Kind${guard.last}`);
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
else {
|
|
// Enumerated: generate a switch statement.
|
|
// Expand sub-aliases to their concrete members.
|
|
const expanded = api.expandKindAliasMembers(guard.aliasName);
|
|
w.write(`func ${funcName}(kind Kind) bool {`);
|
|
w.push();
|
|
w.write("switch kind {");
|
|
w.push();
|
|
w.write(`case ${expanded.map(m => m.formatGoConstant()).join(", ")}:`);
|
|
w.push();
|
|
w.write("return true");
|
|
w.pop();
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("return false");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── Generate Name() accessor ───────────────────────────────────────────────
|
|
|
|
function generateNameAccessor(w: CodeWriter, node: NodeType) {
|
|
const members = schemaMembers(node);
|
|
const nameMember = members.find(m => m.name === "name");
|
|
if (!nameMember) return;
|
|
|
|
// Check if private (either directly or via trait)
|
|
const isPrivate = nameMember.inherited
|
|
? (nameMember.inheritedField?.private || false)
|
|
: (nameMember.private || false);
|
|
if (!isPrivate) return;
|
|
|
|
const structName = node.name;
|
|
w.write(`func (node *${structName}) Name() *DeclarationName {`);
|
|
w.push();
|
|
w.write("return node.name");
|
|
w.pop();
|
|
w.write("}");
|
|
w.write("");
|
|
}
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
// Main generation
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
|
|
function generate(): string {
|
|
const w = new CodeWriter();
|
|
|
|
generateHeader(w);
|
|
generateNodeFactoryStruct(w);
|
|
|
|
// Generate base struct definitions
|
|
generateBaseStructDefs(w);
|
|
|
|
// Generate node type aliases (FooNode = Node for every concrete node type)
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// Node type aliases");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
w.write("type (");
|
|
w.push();
|
|
for (const node of api.nodes()) {
|
|
w.write(`${node.name}Node = Node`);
|
|
}
|
|
// Instantiation aliases (e.g. EndOfFile = Node, AbstractKeyword = Node)
|
|
for (const node of api.nodes()) {
|
|
if (node.instantiationAliases.length === 0) continue;
|
|
for (const alias of node.instantiationAliases) {
|
|
w.write(`${alias.name} = Node`);
|
|
}
|
|
}
|
|
w.pop();
|
|
w.write(")");
|
|
w.write("");
|
|
|
|
// NodeList type aliases
|
|
const listAliases = api.listAliases();
|
|
if (listAliases.length > 0) {
|
|
w.write("type (");
|
|
w.push();
|
|
const maxLen = Math.max(...listAliases.map(alias => alias.name.length));
|
|
for (const listAlias of listAliases) {
|
|
const padding = " ".repeat(maxLen - listAlias.name.length);
|
|
w.write(`${listAlias.name}${padding} = NodeList // NodeList[*${listAlias.elementTypeName}]`);
|
|
}
|
|
w.pop();
|
|
w.write(")");
|
|
w.write("");
|
|
}
|
|
|
|
// Node union aliases (e.g. Statement = Node, BlockOrExpression = Node)
|
|
const nodeAliases = api.nodeAliases();
|
|
if (nodeAliases.length > 0) {
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// Node union aliases");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
w.write("type (");
|
|
w.push();
|
|
const maxAliasLen = Math.max(...nodeAliases.map(alias => alias.name.length));
|
|
for (const alias of nodeAliases) {
|
|
const padding = " ".repeat(maxAliasLen - alias.name.length);
|
|
let comment: string;
|
|
if (alias.isBaseAlias && alias.baseKey) {
|
|
comment = `Node with ${alias.baseKey}`;
|
|
}
|
|
else if (alias.isUnion) {
|
|
comment = alias.unionMemberNames.join(" | ");
|
|
}
|
|
else {
|
|
comment = "";
|
|
}
|
|
w.write(`${alias.name}${padding} = Node${comment ? ` // ${comment}` : ""}`);
|
|
}
|
|
w.pop();
|
|
w.write(")");
|
|
w.write("");
|
|
}
|
|
|
|
// Generate for each node
|
|
for (const node of api.nodes()) {
|
|
// Section comment
|
|
w.write(`// ──────────────────────────────────────────────────────────────────────`);
|
|
w.write(`// ${node.name}`);
|
|
w.write(`// ──────────────────────────────────────────────────────────────────────`);
|
|
w.write("");
|
|
|
|
if (!node.handWritten) {
|
|
generateStructDef(w, node);
|
|
generateNewFactory(w, node);
|
|
generateUpdateFactory(w, node);
|
|
generateForEachChild(w, node);
|
|
generateVisitEachChild(w, node);
|
|
generateClone(w, node);
|
|
generateSubtreeFacts(w, node);
|
|
generateNameAccessor(w, node);
|
|
}
|
|
generateIsFunction(w, node);
|
|
if (node.handWritten) {
|
|
w.write("");
|
|
w.write("// Struct and factory methods hand-written in ./ast.go");
|
|
}
|
|
}
|
|
|
|
// ForEachChild dispatch
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// ForEachChild dispatch");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
generateForEachChildDispatch(w);
|
|
|
|
// VisitEachChild dispatch
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// VisitEachChild dispatch");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
generateVisitEachChildDispatch(w);
|
|
|
|
// SubtreeFacts dispatch
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// SubtreeFacts dispatch");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
generateSubtreeFactsDispatch(w);
|
|
|
|
// Compute subtree facts dispatch
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// Compute subtree facts dispatch");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
generateComputeSubtreeFactsDispatch(w);
|
|
|
|
// Node data accessor dispatch
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// Node data accessor dispatch");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
generateNodeOffsetAccessorDispatch(w, "Name", "*DeclarationName", "name", node => schemaMembers(node).some(m => m.name === "name"), true);
|
|
generateNodeOffsetAccessorDispatch(w, "Modifiers", "*ModifierList", "modifiers", node => extendsBase(node.extendsKeys, "ModifiersBase"), true);
|
|
generateNodeOffsetAccessorDispatch(w, "FlowNodeData", "*FlowNodeBase", "FlowNodeBase", node => extendsBase(node.extendsKeys, "FlowNodeBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "DeclarationData", "*DeclarationBase", "DeclarationBase", node => extendsBase(node.extendsKeys, "DeclarationBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "LocalsContainerData", "*LocalsContainerBase", "LocalsContainerBase", node => extendsBase(node.extendsKeys, "LocalsContainerBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "FunctionLikeData", "*FunctionLikeBase", "FunctionLikeBase", node => extendsBase(node.extendsKeys, "FunctionLikeBase"), false);
|
|
generateNodeOffsetAccessorDispatch(
|
|
w,
|
|
"Expression",
|
|
"*Node",
|
|
"Expression",
|
|
node =>
|
|
schemaMembers(node).some(m => m.name === "Expression") &&
|
|
node.name !== "TypeParameterDeclaration" &&
|
|
node.name !== "SyntheticReferenceExpression",
|
|
true,
|
|
"return n.expressionUnsupported()",
|
|
true,
|
|
kind => kind !== "KindDefaultClause",
|
|
);
|
|
generateNodeOffsetAccessorDispatch(
|
|
w,
|
|
"Type",
|
|
"*Node",
|
|
node => schemaMembers(node).some(m => m.name === "Type") ? "Type" : "TypeExpression",
|
|
node =>
|
|
(schemaMembers(node).some(m => m.name === "Type") && node.name !== "SyntheticExpression" && node.name !== "JSDocVariadicType") ||
|
|
node.name === "JSDocParameterOrPropertyTag",
|
|
true,
|
|
);
|
|
generateNodeOffsetAccessorDispatch(w, "Initializer", "*Node", "Initializer", node => schemaMembers(node).some(m => m.name === "Initializer"), true, 'panic("Unhandled case in Node.Initializer")');
|
|
generateNodeTextOffsets(w);
|
|
generatePropagateSubtreeFactsDispatch(w);
|
|
generateNodeOffsetAccessorDispatch(w, "ExportableData", "*ExportableBase", "ExportableBase", node => extendsBase(node.extendsKeys, "ExportableBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "ClassLikeData", "*ClassLikeBase", "ClassLikeBase", node => extendsBase(node.extendsKeys, "ClassLikeBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "BodyData", "*BodyBase", "BodyBase", node => extendsBase(node.extendsKeys, "BodyBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "LiteralLikeData", "*LiteralLikeNodeBase", "LiteralLikeNodeBase", node => extendsBase(node.extendsKeys, "LiteralLikeNodeBase"), false);
|
|
generateNodeOffsetAccessorDispatch(w, "TemplateLiteralLikeData", "*TemplateLiteralLikeNodeBase", "TemplateLiteralLikeNodeBase", node => extendsBase(node.extendsKeys, "TemplateLiteralLikeNodeBase"), false);
|
|
generateCloneDispatch(w);
|
|
generateSetModifiersDispatch(w);
|
|
|
|
// As*() casts
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// As*() cast methods");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
const canonicalKinds = canonicalNodesByKind();
|
|
for (const node of api.nodes()) {
|
|
generateAsCast(w, node, canonicalKinds);
|
|
}
|
|
|
|
// Kind alias guards
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("// Kind alias guards");
|
|
w.write("// ──────────────────────────────────────────────────────────────────────");
|
|
w.write("");
|
|
generateKindAliasGuards(w);
|
|
|
|
return w.toString();
|
|
}
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
// Write output
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
// Kind generation
|
|
// ────────────────────────────────────────────────────────────────────────────
|
|
|
|
function generateKind(): string {
|
|
const w = new CodeWriter();
|
|
w.write("// Code generated by _scripts/generate-go-ast.ts. DO NOT EDIT.");
|
|
w.write("");
|
|
w.write("package ast");
|
|
w.write("");
|
|
w.write("//go:generate go tool golang.org/x/tools/cmd/stringer -type=Kind -output=kind_stringer_generated.go");
|
|
w.write("//go:generate npx dprint fmt kind_stringer_generated.go");
|
|
w.write("");
|
|
w.write("type Kind int16");
|
|
w.write("");
|
|
w.write("const (");
|
|
w.push();
|
|
|
|
const elements = api.kindElements();
|
|
const markers = api.kindMarkers();
|
|
const kindUnions = api.kindAliases();
|
|
|
|
// Build set of all defined kind names for validation
|
|
const definedKinds = new Set<string>();
|
|
for (const element of elements) {
|
|
if (element.name) {
|
|
definedKinds.add(element.name);
|
|
}
|
|
}
|
|
|
|
// Validate marker references (markers can reference kind names or other marker names)
|
|
const markerNames = new Set(markers.map(m => m.name));
|
|
for (const marker of markers) {
|
|
if (!definedKinds.has(marker.value) && !markerNames.has(marker.value)) {
|
|
throw new Error(`Kind marker ${marker.name} references undefined kind or marker "${marker.value}"`);
|
|
}
|
|
}
|
|
|
|
// Validate union references
|
|
for (const kindUnion of kindUnions) {
|
|
for (const m of kindUnion.members) {
|
|
if (!definedKinds.has(m) && !api.hasKindAlias(m)) {
|
|
throw new Error(`Kind union ${kindUnion.name} references undefined kind or union "${m}"`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Emit elements
|
|
let first = true;
|
|
for (const el of elements) {
|
|
if (!el.name) {
|
|
w.write(`// ${el.comment}`);
|
|
continue;
|
|
}
|
|
const trailing = el.comment ? ` // ${el.comment}` : "";
|
|
if (first) {
|
|
w.write(`Kind${el.name} Kind = iota${trailing}`);
|
|
first = false;
|
|
}
|
|
else {
|
|
w.write(`Kind${el.name}${trailing}`);
|
|
}
|
|
}
|
|
|
|
// KindCount
|
|
w.write("KindCount");
|
|
|
|
// Markers
|
|
const maxLen = Math.max(...markers.map(m => m.name.length));
|
|
for (const marker of markers) {
|
|
const padding = " ".repeat(maxLen - marker.name.length);
|
|
w.write(`Kind${marker.name}${padding} = Kind${marker.value}`);
|
|
}
|
|
|
|
w.pop();
|
|
w.write(")");
|
|
w.write("");
|
|
|
|
// Kind union type aliases
|
|
if (kindUnions.length > 0) {
|
|
w.write("type (");
|
|
w.push();
|
|
for (const { name, members } of kindUnions) {
|
|
function expandMembers(ms: string[]): string[] {
|
|
const result: string[] = [];
|
|
for (const m of ms) {
|
|
const alias = api.getKindAlias(m);
|
|
if (alias) {
|
|
result.push(...expandMembers(alias.members));
|
|
}
|
|
else {
|
|
result.push(m);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
const expanded = expandMembers(members);
|
|
const comment = expanded.map(m => `Kind${m}`).join(" | ");
|
|
w.write(`${name} = Kind // ${comment}`);
|
|
}
|
|
w.pop();
|
|
w.write(")");
|
|
w.write("");
|
|
}
|
|
|
|
return w.toString();
|
|
}
|
|
|
|
function writeAndFormat(filePath: string, content: string) {
|
|
fs.writeFileSync(filePath, content);
|
|
execaSync("dprint", ["fmt", filePath], { stdio: "inherit", cwd: ROOT });
|
|
console.log(`Wrote ${filePath}`);
|
|
}
|
|
|
|
export default function main() {
|
|
console.log("Generating Go AST code...");
|
|
|
|
const code = generate();
|
|
const outPath = path.join(ROOT, "internal/ast/ast_generated.go");
|
|
writeAndFormat(outPath, code + "\n");
|
|
|
|
const kindCode = generateKind();
|
|
const kindOutPath = path.join(ROOT, "internal/ast/kind_generated.go");
|
|
writeAndFormat(kindOutPath, kindCode + "\n");
|
|
}
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
main();
|
|
}
|