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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
export enum TokenType {
OPEN_PAREN = "(",
CLOSE_PAREN = ")",
SPACE = " ",
SEMICOLON = ";",
COMMA = ",",
NAME = "NAME",
}
export type Token<
Type extends TokenType = TokenType,
Name extends string = string
> = {
type: Type;
name: Name;
};
export type LexerCtx = {
next: string;
nameCollection: string;
tokens: readonly Token[];
};
export enum NodeType {
INT = "INT",
EXT = "EXT",
PARSER_ERROR = "PARSER_ERROR",
}
export type ASTNode<
Type extends NodeType = NodeType,
Name extends string = string,
Value extends any = any,
Children extends readonly ASTNode[] = readonly ASTNode<
NodeType,
string,
any,
any
>[]
> = {
type: Type;
name: Name;
value: Value;
children: Children;
};
export type ParserCtx = {
remainingTokens: readonly Token[];
lastToken: Token | null;
stack: readonly ASTNode[];
};
export interface StackFrame<
Bindings extends Record<ASTNode["name"], any> = Record<ASTNode["name"], any>
> {
bindings: Bindings;
}
export type EmptyStackFrame = StackFrame<{}>;
export type MergeStackFrames<
OldFrame extends StackFrame,
NewFrame extends StackFrame
> = StackFrame<{
[K in
| keyof OldFrame["bindings"]
| keyof NewFrame["bindings"]]: K extends keyof NewFrame["bindings"]
? NewFrame["bindings"][K]
: OldFrame["bindings"][K];
}>;
|