Add a quick 'n dirty serializer for AST nodes
This commit is contained in:
parent
50cbb6db99
commit
3dcf91c520
2 changed files with 24 additions and 0 deletions
|
@ -4,6 +4,7 @@ import { TextSpan } from "./text"
|
||||||
import { Value } from "./evaluator"
|
import { Value } from "./evaluator"
|
||||||
import { Package } from "./package"
|
import { Package } from "./package"
|
||||||
import { Diagnostic } from "./diagnostics";
|
import { Diagnostic } from "./diagnostics";
|
||||||
|
import { serializeTag, serialize, JsonObject } from "./util";
|
||||||
|
|
||||||
let nextNodeId = 1;
|
let nextNodeId = 1;
|
||||||
|
|
||||||
|
@ -29,6 +30,17 @@ export abstract class Syntax {
|
||||||
this.id = nextNodeId++;
|
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() {
|
*preorder() {
|
||||||
const stack: Syntax[] = [ this as unknown as Syntax ] ;
|
const stack: Syntax[] = [ this as unknown as Syntax ] ;
|
||||||
while (stack.length > 0) {
|
while (stack.length > 0) {
|
||||||
|
|
12
src/ast.ts
12
src/ast.ts
|
@ -8,6 +8,8 @@ import { Package } from "./package";
|
||||||
|
|
||||||
import { Diagnostic } from "./diagnostics";
|
import { Diagnostic } from "./diagnostics";
|
||||||
|
|
||||||
|
import { serializeTag, serialize, JsonObject } from "./util";
|
||||||
|
|
||||||
let nextNodeId = 1;
|
let nextNodeId = 1;
|
||||||
|
|
||||||
export type ResolveSyntaxKind<K extends SyntaxKind> = Extract<Syntax, {
|
export type ResolveSyntaxKind<K extends SyntaxKind> = Extract<Syntax, {
|
||||||
|
@ -24,6 +26,16 @@ export abstract class SyntaxBase {
|
||||||
constructor(public span: TextSpan | null = null) {
|
constructor(public span: TextSpan | null = null) {
|
||||||
this.id = nextNodeId++;
|
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() {
|
*preorder() {
|
||||||
const stack: Syntax[] = [this as unknown as Syntax];
|
const stack: Syntax[] = [this as unknown as Syntax];
|
||||||
while (stack.length > 0) {
|
while (stack.length > 0) {
|
||||||
|
|
Loading…
Reference in a new issue