ast_generator/ast_generator.ts

196 lines
6.2 KiB
TypeScript
Raw Permalink Normal View History

2024-07-21 20:56:42 +01:00
import {
Grammar,
Parser,
} from "https://deno.land/x/nearley@2.19.7-deno/mod.ts";
import compiledParserGrammar from "./parser.out.ts";
2024-07-24 11:31:06 +01:00
import {
2024-07-29 01:54:28 +01:00
CodeBlock,
2024-07-24 11:31:06 +01:00
Enum,
Name,
Node,
Param,
Param_Named,
Statement,
Type,
} from "./ast.out.ts";
2024-07-21 20:56:42 +01:00
class TypescriptGenerator {
private result = "";
public generate(ast: Statement[]): string {
this.result += "// Generated file by ast_generator\n";
for (const statement of ast) {
switch (statement.kind) {
2024-07-29 01:54:28 +01:00
case "CodeBlock":
this.generateCodeBlock(statement[0]);
break;
2024-07-21 20:56:42 +01:00
case "Enum":
2024-07-21 22:35:34 +01:00
this.generateEnum(statement[0]);
2024-07-21 20:56:42 +01:00
break;
case "Node":
2024-07-21 22:35:34 +01:00
this.generateNode(statement[0]);
2024-07-21 20:56:42 +01:00
break;
}
2024-07-24 11:31:06 +01:00
this.result += "\n";
2024-07-21 20:56:42 +01:00
}
return this.result;
}
2024-07-29 01:54:28 +01:00
private generateCodeBlock(block: CodeBlock) {
this.result += `${block[0]}\n`;
}
2024-07-21 20:56:42 +01:00
private generateEnum(enum_: Enum) {
2024-07-24 11:31:06 +01:00
if (this.isShallowEnum(enum_)) {
return this.generateShallowEnumBody(enum_);
} else {
return this.generateEnumBody(enum_);
}
}
private isShallowEnum(enum_: Enum): boolean {
return !enum_.nodes.some((node) => node.params.length > 0);
}
private generateShallowEnumBody(enum_: Enum) {
const name = this.makeName(enum_.name);
const fields = enum_.nodes.map((node) => this.makeName(node.name)).map(
(name) => `${name}: "${name}"`,
).join(", ");
this.result += `export const ${name} = { ${fields} } as const;\n`;
this.result += `export type ${name} = keyof typeof ${name};\n`;
}
private generateEnumBody(enum_: Enum) {
2024-07-21 20:56:42 +01:00
const enumName = this.makeName(enum_.name);
2024-07-24 11:31:06 +01:00
const nodeNames: [string, string][] = [];
2024-07-21 20:56:42 +01:00
for (const node of enum_.nodes) {
2024-07-24 11:31:06 +01:00
const name = this.makeName(node.name);
const fullName = `${enumName}_${name}`;
nodeNames.push([name, fullName]);
this.result += `export type ${fullName} = {\n`;
2024-07-29 01:54:28 +01:00
this.result += ` type: "${enumName}",\n`;
2024-07-24 11:31:06 +01:00
this.result += ` kind: "${name}",\n`;
this.result += node.params.map((p, i) => this.makeParamField(p, i))
.map((field) => ` ${field},\n`).join("");
this.result += `};\n`;
const fnParams = this.makeFnParams(node.params);
const fields = this.makeFnFields(node.params);
this.result += `export const ${fullName}` +
` = (${fnParams}): ${fullName}` +
2024-07-29 01:54:28 +01:00
` => ({ type: "${enumName}", kind: "${name}", ${fields} });\n`;
2024-07-21 20:56:42 +01:00
}
2024-07-24 11:31:06 +01:00
const typeList = nodeNames
.map(([_, fullName]) => fullName)
.join(" | ");
this.result += `export type ${enumName} = ${typeList};\n`;
const enumFieldFns = nodeNames
.map(([name, fullName]) => `${name}: ${fullName}`)
.join(", ");
this.result +=
`export const ${enumName} = { ${enumFieldFns} } as const;\n`;
2024-07-21 20:56:42 +01:00
}
2024-07-24 11:31:06 +01:00
private makeParamField(param: Param, index: number): string {
switch (param.kind) {
case "Named":
return this.makeNamedParam(param);
case "Unnamed":
return `[${index}]: ${this.makeType(param[0])}`;
}
2024-07-21 20:56:42 +01:00
}
2024-07-24 11:31:06 +01:00
private generateNode(
2024-07-21 20:56:42 +01:00
node: Node,
) {
2024-07-24 11:31:06 +01:00
const name = this.makeName(node.name);
2024-07-21 20:56:42 +01:00
this.result += `export type ${name} = {\n`;
2024-07-29 01:54:28 +01:00
this.result += ` type: "${name}";\n`;
2024-07-21 22:35:34 +01:00
for (
const [param, index] of node.params
.map((v, i) => [v, i] as const)
) {
switch (param.kind) {
case "Named":
this.result += ` ${this.makeNamedParam(param)};\n`;
break;
2024-07-24 11:31:06 +01:00
case "Unnamed": {
const type_ = this.makeType(param[0]);
this.result += ` [${index}]: ${type_};\n`;
break;
}
2024-07-21 22:35:34 +01:00
}
2024-07-21 20:56:42 +01:00
}
this.result += "};\n";
2024-07-24 11:31:06 +01:00
const fnParams = this.makeFnParams(node.params);
const fields = this.makeFnFields(node.params);
this.result += `export const ${name}` +
` = (${fnParams}): ${name}` +
2024-07-29 01:54:28 +01:00
` => ({ type: "${name}", ${fields} });\n`;
2024-07-24 11:31:06 +01:00
}
private makeFnParams(params: Param[]): string {
return params
2024-07-21 22:35:34 +01:00
.map((param, index) => {
switch (param.kind) {
case "Named":
return this.makeNamedParam(param);
case "Unnamed":
return `v${index}: ${this.makeType(param[0])}`;
}
})
.join(", ");
2024-07-21 20:56:42 +01:00
}
2024-07-24 11:31:06 +01:00
private makeFnFields(params: Param[]): string {
return params.map((param, index) => {
switch (param.kind) {
case "Named":
return this.makeName(param.name);
case "Unnamed":
return `[${index}]: v${index}`;
}
})
.join(", ");
}
private makeNamedParam(param: Param_Named): string {
2024-07-21 20:56:42 +01:00
const name = this.makeName(param.name);
const type_ = this.makeType(param.type_);
return `${name}: ${type_}`;
}
private makeType(type_: Type): string {
switch (type_.kind) {
case "Name":
2024-07-21 22:35:34 +01:00
return this.makeName(type_[0]);
2024-07-21 20:56:42 +01:00
case "Optional":
2024-07-21 22:35:34 +01:00
return `${this.makeType(type_[0])} | null`;
2024-07-21 20:56:42 +01:00
case "Multiple":
2024-07-21 22:35:34 +01:00
return `${this.makeType(type_[0])}[]`;
2024-07-21 20:56:42 +01:00
}
}
private makeName(name: Name): string {
2024-07-21 22:35:34 +01:00
return name[0];
2024-07-21 20:56:42 +01:00
}
}
const parser = new Parser(Grammar.fromCompiled(compiledParserGrammar));
if (Deno.args.length < 1) {
console.log("generate_ast <file>");
throw new Error("not enough args");
}
if (["-h", "--help"].includes(Deno.args[0])) {
console.log("generate_ast <file>");
Deno.exit(0);
}
const text = await Deno.readTextFile(Deno.args[0]);
parser.feed(text);
const ast = parser.results[0];
// console.log(JSON.stringify(ast, null, "│ "));
const ts = new TypescriptGenerator().generate(ast);
console.log(ts);