-
Notifications
You must be signed in to change notification settings - Fork 3
/
parsenode.ts
48 lines (41 loc) · 1010 Bytes
/
parsenode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Scanner, TokenError } from "./deps.ts";
import { Visitor } from "./visitor.ts";
/**
* The base return type that all `Node.toJSON()` calls will return.
*/
export interface ParseNodeJSON {
type: string;
start: [number, number];
end: [number, number];
[key: string]: unknown;
}
/**
* The base class for all Nodes in the AST.
*/
export class ParseNode {
constructor(
/**
* The starting [line, column]
*/
public start: [number, number] = [0, 0],
/**
* The ending [line, column]
*/
public end: [number, number] = [0, 0],
) {}
get type(): string {
return this.constructor.name;
}
static async parse(scanner: Scanner, syntax: 2 | 3 = 3): Promise<ParseNode> {
throw new TokenError(scanner, await scanner.scan());
}
toProto(syntax: 2 | 3 = 3): string {
return ``;
}
toJSON(): ParseNodeJSON {
return { type: "ParseNode", start: this.start, end: this.end };
}
accept(visitor: Visitor) {
visitor.visit?.(this);
}
}