/
api.ts
104 lines (93 loc) · 2.03 KB
/
api.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import type { Fn2 } from "@thi.ng/api";
export interface SyntaxOpts {
/**
* An array of string pairs defining possible S-expression / scope
* delimiters [open, close]. By default only the classic ["(",")"]
* pair is defined. Only single character delimiters are supported.
*
* {@link DEFAULT_SYNTAX}
*/
scopes: string[][];
/**
* Regex to identify whitespace a single whitespace character.
* Default: space, tab, newline, comma
*/
whiteSpace: RegExp;
/**
* Single character string to delineate string values.
* Default: `"`
*/
string: string;
}
export interface Token {
/**
* Token value
*/
value: string;
/**
* Token's start line
*/
line: number;
/**
* Token's start column
*/
col: number;
}
export type NodeType = "root" | "expr" | "sym" | "str" | "num";
export type ASTNode = Root | Expression | Sym | Str | Numeric;
/**
* Base interface for custom AST nodes
*/
export interface INode {
type: string;
parent?: INode;
}
export interface Root extends INode {
type: "root";
children: ASTNode[];
}
/**
* AST Node defining an S-expression.
*/
export interface Expression extends INode {
type: "expr";
/**
* Child nodes
*/
children: ASTNode[];
/**
* Scope type char (as per configured syntax)
*/
value: string;
}
/**
* AST symbol node. Merely holds symbol name.
*/
export interface Sym extends INode {
type: "sym";
value: string;
}
/**
* AST string node. Merely wraps string value.
*/
export interface Str extends INode {
type: "str";
value: string;
}
/**
* AST numeric node. Merely wraps parsed numeric value.
*/
export interface Numeric extends INode {
type: "num";
value: number;
}
/**
* Type hinted runtime implementations.
*/
export interface Implementations<ENV, RES> {
root: Fn2<Root, ENV, RES>;
expr: Fn2<Expression, ENV, RES>;
sym: Fn2<Sym, ENV, RES>;
str: Fn2<Str, ENV, RES>;
num: Fn2<Numeric, ENV, RES>;
}