Add a quick 'n dirty serializer for AST nodes

This commit is contained in:
Sam Vervaeck 2020-06-07 00:18:02 +02:00
parent 50cbb6db99
commit 3dcf91c520
2 changed files with 24 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import { TextSpan } from "./text"
import { Value } from "./evaluator"
import { Package } from "./package"
import { Diagnostic } from "./diagnostics";
import { serializeTag, serialize, JsonObject } from "./util";
let nextNodeId = 1;
@ -29,6 +30,17 @@ export abstract class Syntax {
this.id = nextNodeId++;
}
[serializeTag]() {
const result: JsonObject = {};
for (const key of Object.keys(this)) {
if (key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') {
continue;
}
result[key] = serialize((this as any)[key]);
}
return result;
}
*preorder() {
const stack: Syntax[] = [ this as unknown as Syntax ] ;
while (stack.length > 0) {

View file

@ -8,6 +8,8 @@ import { Package } from "./package";
import { Diagnostic } from "./diagnostics";
import { serializeTag, serialize, JsonObject } from "./util";
let nextNodeId = 1;
export type ResolveSyntaxKind<K extends SyntaxKind> = Extract<Syntax, {
@ -24,6 +26,16 @@ export abstract class SyntaxBase {
constructor(public span: TextSpan | null = null) {
this.id = nextNodeId++;
}
[serializeTag]() {
const result: JsonObject = {};
for (const key of Object.keys(this)) {
if (key === 'parentNode' || key === 'errors' || key === 'type' || key === 'id') {
continue;
}
result[key] = serialize((this as any)[key]);
}
return result;
}
*preorder() {
const stack: Syntax[] = [this as unknown as Syntax];
while (stack.length > 0) {