-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
330 lines (303 loc) · 19.8 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// "Simple" Lisp parser
// https://www.typescriptlang.org/play?ts=4.1.0-pr-40336-88#code/PTAECIGUEsFsAcA2BTcoAy0DO9TwIYBOWyhAsAFCUAuAnvMhgPYDupAgovABb4BGyaqAC8oAAb4xoAD7i+U2WIDGC8QBNVY5JoBmmgOabum6JoBWmgNabEm2JoB2mppviaAjpsKasm6poBXTQA3TRZNAA9NWk0ALzEAbhp6RgBVeAZCTh5+QRFxdk0AIU0AYU0AEU0AUU0AMU0AcU0ACU0ASU0AKU0AaU10TQBZTQA5TQB5TQAFTQBFTQAlTUhNABVNVM0AN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5LeZ85-Z-a3Q+ZzW15z899eZ750uyLVZ3u8dUF7m0w-myEyTVZ5exF+g6FyFA+xF+AJCEy6tVZ0WxF0DkDmZ8TZZ53RbWq0DlUyfZXcl42-7Sjjfekwk9NcZ9dVE3l2V6E3kw11lzFG5xF63PgEDpQ3TUAA
type LowerAlphabet = `a` | `b` | `c` | `d` | `e` | `f` | `g` | `h` | `i` | `j` | `k` | `l` | `m` | `n` | `o` | `p` | `q` | `r` | `s` | `t` | `u` | `v` | `w` | `x` | `y` | `z`;
type UpperAlphabet = `A` | `B` | `C` | `D` | `E` | `F` | `G` | `H` | `I` | `J` | `K` | `L` | `M` | `N` | `O` | `P` | `Q` | `R` | `S` | `T` | `U` | `V` | `W` | `X` | `Y` | `Z`;
type Letter = LowerAlphabet | UpperAlphabet;
type Digit = `1` | `2` | `3` | `4` | `5` | `6` | `7` | `8` | `9` | `0`;
type IdentifierSign = `-` | `_` | `.` | `+` | `*` | `/` | `!` | `@` | `\$` | `%` | `^` | `&` | `~` | `=` | `{` | `}` | `[` | `]` | `:` | `<` | `>` | `?` | `\\`;
type WhiteSpace = ` ` | `\n`;
type TrimLeft<T extends string> =
T extends `${WhiteSpace}${infer R}`
? TrimLeft<R>
: T
type Prepend<Head, Tail> = Tail extends unknown[] ? [Head, ...Tail] : ParseError<{ head: Head, tail: Tail }, `prepend failed`>;
type Append<Init, Last> = Init extends unknown[] ? [...Init, Last] : ParseError<{ init: Init, last: Last}, `append failed`>;
type IsNumber<T>
= T extends `.`
? false
: T extends `${infer _0}.${infer _1}.${infer _2}`
? false
: T extends `${infer _0}${ExpSign}${infer _1}${ExpSign}${infer _2}`
? false
: T extends `${infer _0}${ExpSign}${infer _1}.${infer _2}`
? false
: T extends `${infer _}${ExpSign}` | `${infer _}${ExpSign}+` | `${infer _}${ExpSign}-`
? false
: T extends | `+${ExpSign}${infer _}` | `-${ExpSign}${infer _}` | `.${ExpSign}${infer _}` | `+.${ExpSign}${infer _}` | `-.${ExpSign}${infer _}`
? false
: T extends `${infer _0}${WhiteSpace}${infer _1}`
? false
: T extends `+${infer Rest}` | `-${infer Rest}`
? IsNumber_Sub<Rest>
: IsNumber_Sub<T>;
type IsNumber_Sub<T>
= T extends ``
? true
: T extends `${Digit | `.` | ExpSign}${infer Rest}`
? Rest extends `+${infer NewRest}` | `-${infer NewRest}`
? IsNumber_Sub<NewRest>
: IsNumber_Sub<Rest>
: false;
type ExpSign = `e` | `E`;
type IsValidIdenfitier<T>
= T extends `()`
? true
: T extends `${infer _0}${WhiteSpace}${infer _1}`
? false
: T extends `${Letter | Digit | IdentifierSign}${infer R}`
? IsValidIdenfitier_Sub<R>
: false;
type IsValidIdenfitier_Sub<T>
= T extends ``
? true
: T extends `${Letter | Digit | IdentifierSign}${infer R}`
? IsValidIdenfitier_Sub<R>
: false;
type Parse<Arg extends string> = SExpressionRoot<RemoveComments<Arg> extends infer A ? A extends string ? A : never : never>;
type RemoveComments<Arg extends string>
= Arg extends `${infer Before};${infer _}\n${infer After}`
? RemoveComments<`${Before}\n${After}`>
: Arg;
type SExpressionRoot<Arg extends string>
= SExpression<Arg, [], false, true, ``, false> extends infer Node
? Node extends ListNode<infer Exprs, ``>
? Exprs
: Node extends PairNode<infer Return_Expr1, infer Return_Expr2, infer _>
? [Pair<``, Return_Expr1, Return_Expr2>]
: Node extends AtomNode<infer Return_AtomName, infer _>
? [Atom<``, Return_AtomName>]
: ParseError<Node, `parse error`>
: never;
type SExpression<
Arg,
InitExprs extends unknown[],
PairFlag extends boolean,
InRoot extends boolean,
Prefix extends string,
AfterEnd = false,
>
= Arg extends ``
? PairFlag extends true
? InitExprs extends [unknown, unknown]
? PairNode<InitExprs[0], InitExprs[1], ``>
: ParseError<[Arg, InitExprs], `invalid pair notation`>
: Prefix extends ``
? ListNode<InitExprs, ``>
: ParseError<[Arg, Prefix], `invalid single-quote notation`>
: Arg extends `${infer Char}${infer Rest}`
? Char extends `.`
? AfterEnd extends true
? PairStartSub<Arg, Rest, InitExprs, PairFlag, InRoot, Prefix>
: Rest extends `${Letter | Digit | IdentifierSign}${infer _}`
? IdentifierSub<Rest, Char, InitExprs, PairFlag, InRoot, Prefix>
: PairStartSub<Arg, Rest, InitExprs, PairFlag, InRoot, Prefix>
: Char extends Letter | Digit | IdentifierSign
? IdentifierSub<Rest, Char, InitExprs, PairFlag, InRoot, Prefix>
: Char extends WhiteSpace
? SExpression<TrimLeft<Rest>, InitExprs, PairFlag, InRoot, Prefix>
: Char extends `"`
? StringLiteralSub<Arg, InitExprs, PairFlag, InRoot, Prefix>
: Char extends `(`
? ListSub<Rest, InitExprs, PairFlag, InRoot, Prefix>
: Char extends `)`
? InitExprs extends []
? Prefix extends ``
? AtomNode<`()`, Rest>
: ParseError<[Arg, Prefix], `invalid single-quote notation`>
: PairFlag extends true
? InitExprs extends [unknown, unknown]
? PairNode<InitExprs[0], InitExprs[1], Rest>
: ParseError<[Arg, InitExprs], `invalid pair notation`>
: ListNode<InitExprs, Rest>
: Char extends `'`
? SExpression<Rest, InitExprs, PairFlag, InRoot, `${Prefix}'`>
: ParseError<[Arg, InitExprs], `unexpected string`>
: never;
type PairStartSub<Arg, Rest, InitExprs extends unknown[], PairFlag extends boolean, InRoot extends boolean, Prefix extends string>
= InRoot extends true
? ParseError<[Arg, InitExprs], `pair notation not allowed in root`>
: PairFlag extends true
? ParseError<[Arg, InitExprs], `too many pair notation`>
: Prefix extends ``
? InitExprs extends [unknown]
? SExpression<Rest, InitExprs, true, InRoot, ``>
: ParseError<[Arg, InitExprs], `unexpected pair notation`>
: ParseError<[Arg, InitExprs, Prefix], `wrong pair and single-quote notation`>;
type IdentifierSub<Rest, Char extends string, InitExprs extends unknown[], PairFlag extends boolean, InRoot extends boolean, Prefix extends string>
= ConsumeAtom<Rest, Char> extends AtomNode<infer Return_AtomName, infer Return_Rest>
? SExpression<Return_Rest, Append<InitExprs, Atom<Prefix, Return_AtomName>>, PairFlag, InRoot, ``>
: ParseError<[Rest, Char, ConsumeAtom<Rest, Char>], `invalid Node`>
type StringLiteralSub<Arg, InitExprs extends unknown[], PairFlag extends boolean, InRoot extends boolean, Prefix extends string>
= Arg extends `"${infer AtomValue}"${infer NewRest}`
? SExpression<NewRest, Append<InitExprs, Atom<Prefix, `"${AtomValue}"`>>, PairFlag, InRoot, ``>
: ParseError<[Arg], `invalid Node`>
type ListSub<Rest, InitExprs extends unknown[], PairFlag extends boolean, InRoot extends boolean, Prefix extends string>
= SExpression<Rest, [], false, false, ``> extends infer Node
? Node extends ListNode<infer Return_ChildExprs, infer Return_Rest>
? SExpression<Return_Rest, Append<InitExprs, List<Prefix, Return_ChildExprs>>, PairFlag, InRoot, ``, true>
: Node extends PairNode<infer Return_Expr1, infer Return_Expr2, infer Return_Rest>
? SExpression<Return_Rest, Append<InitExprs, Pair<Prefix, Return_Expr1, Return_Expr2>>, PairFlag, InRoot, ``, true>
: Node extends AtomNode<infer Return_AtomName, infer Return_Rest>
? SExpression<Return_Rest, Append<InitExprs, Atom<Prefix, Return_AtomName>>, PairFlag, InRoot, ``, true>
: ParseError<[Rest, Node], `invalid Node`>
: never
type AtomNode<Name extends string, RestString extends string> = { _: `atom-node`, v: [Name, RestString] };
type ListNode<Exprs extends unknown[], RestString extends string> = { _: `list-node`, v: [Exprs, RestString] };
type PairNode<Expr1, Expr2, RestString extends string> = { _: `pair-node`, v: [{ 0: Expr1, 1: Expr2 }, RestString] };
type ConsumeAtom<ConsumingString, InitString extends string>
= ConsumingString extends ``
? AtomNode<InitString, ``>
: ConsumingString extends `${infer Char}${infer Rest}`
? Char extends Letter | Digit | IdentifierSign
? Char extends `.`
? Rest extends `(${infer _}` | `"${infer _}`
? AtomNode<InitString, `${Char}${Rest}`> // keep sought item
: ConsumeAtom<Rest, `${InitString}${Char}`>
: ConsumeAtom<Rest, `${InitString}${Char}`>
: AtomNode<InitString, `${Char}${Rest}`> // keep sought item
: ParseError<[ConsumingString, InitString], `pop string failed`>
type Atom<Prefix, T extends string>
= T extends ``
? ParseError<[Prefix, T], `atom failed`>
: T extends `"${infer _}"`
? StringAtom<Prefix, T>
: IsNumber<T> extends true
? NumberAtom<Prefix, T>
: IsValidIdenfitier<T> extends true
? SymbolAtom<Prefix, T>
: ParseError<[Prefix, T], `atom failed`>;
type StringAtom<Prefix, String> = { prefix: Prefix, string: String };
type NumberAtom<Prefix, Number> = { prefix: Prefix, number: Number };
type SymbolAtom<Prefix, SymbolName> = { prefix: Prefix, symbol: SymbolName };
type Pair<Prefix, S1, S2> = { prefix: Prefix, 0: S1, 1: S2 };
type List<Prefix, S extends unknown[]> = { prefix: Prefix, list: S};
type ParseError<Args, Message = undefined> = { message: Message, args: Args };
// check
type T0_00 = Parse<``>;
type T1_00 = Parse<`()`>;
type T1_01 = Parse<`t`>;
type T1_02 = Parse<`foo`>;
type T1_03 = Parse<`nil`>;
type T1_04 = Parse<`1`>;
type T1_05 = Parse<`12345678901234`>;
type T1_06 = Parse<`"a"`>;
type T1_07 = Parse<`"Hello World"`>;
type T1_08 = Parse<`"1234567890123456789012"`>;
type T1_09 = Parse<`-1.2`>;
type T1_10 = Parse<`1.2e10`>;
type T1_11 = Parse<`-1.2e-10`>;
type T1_12 = Parse<`.e1`>;
// type T1_xx = Parse<`1234567890123456789012`>; // need counter reset
type T2_00 = Parse<`1.2`>;
type T2_01 = Parse<`1.2 3`>;
type T2_02 = Parse<`1 2.3`>;
type T2_03 = Parse<`1.2.3`>;
type T2_04 = Parse<`1 2 . 3`>; // invalid
type T2_05 = Parse<`1 . 2 . 3`>; // invalid
type T2_06 = Parse<`1 . . 3`>; // invalid
type T2_07 = Parse<`.`>; // invalid
type T2_08 = Parse<`. 1`>; // invalid
type T2_09 = Parse<`.1`>;
type T2_10 = Parse<`1 .`>; // invalid
type T2_11 = Parse<`1.`>;
type T2_12 = Parse<`"".""`>; // invalid
type T2_13 = Parse<`().1`>; // invalid
type T2_14 = Parse<`1.()`>; // invalid
type T2_15 = Parse<`1.1.()`>; // invalid
type T2_16 = Parse<`1.nil`>;
type T2_17 = Parse<`1 .nil`>;
type T2_18 = Parse<`1. nil`>;
type T2_19 = Parse<`nil.1`>;
type T2_20 = Parse<`nil .1`>;
type T2_21 = Parse<`nil. 1`>;
type T2_22 = Parse<`.wr-le..+--`>;
type T3_00 = Parse<`(a)`>;
type T3_01 = Parse<`(+ 1 2)`>;
type T3_02 = Parse<`(1)`>;
type T3_03 = Parse<`(12 "sb")`>;
type T3_04 = Parse<`( 12 "sb" )`>;
type T3_05 = Parse<`("A B" "C")`>;
type T3_06 = Parse<`("A B" "C D" "E F")`>;
type T3_07 = Parse<`(+ 1 2 3 4)`>;
type T3_08 = Parse<`(1 2 . 3)`>; // invalid
type T3_09 = Parse<`(1 . 2 . 3)`>; // invalid
type T3_10 = Parse<`(1 . . 3)`>; // invalid
type T3_11 = Parse<`(.)`>; // invalid
type T3_12 = Parse<`(. 1)`>; // invalid
type T3_13 = Parse<`(.1)`>;
type T3_14 = Parse<`(1 .)`>; // invalid
type T3_15 = Parse<`(1.)`>;
type T3_16 = Parse<`(""."")`>;
type T3_17 = Parse<`(().1)`>;
type T3_18 = Parse<`(1.())`>;
type T3_19 = Parse<`(1.1.())`>;
type T3_20 = Parse<`(1."s")`>;
type T3_21 = Parse<`( 1 . "s" )`>;
type T4_00 = Parse<`1 2`>;
type T4_01 = Parse<`1 (2)`>;
type T4_02 = Parse<`+ 1 2 3`>;
type T4_03 = Parse<`1(2)`>;
type T4_04 = Parse<` 1 2 3 `>;
type T4_05 = Parse<`a "b c" "d e" f`>;
type T4_06 = Parse<`a "b c"`>;
type T4_07 = Parse<`a "b c" d`>;
type T4_08 = Parse<`aa 12`>;
type T4_09 = Parse<`"Hello World" "Hello World" "Hello World"`>;
type T4_10 = Parse<`1"a"`>;
type T5_00 = Parse<`0 (1 (2 3) 4) 5`>;
type T5_01 = Parse<`0(1(2 3)4)5`>;
type T5_02 = Parse<` 0 ( 1 ( 2 3 ) 4 ) 5 `>;
type T5_03 = Parse<`0 (1 2) (3 4) 5`>;
type T5_04 = Parse<`0(1 2)(3 4)5`>;
type T5_05 = Parse<` 0 ( 1 2 ) ( 3 4 ) 5 `>;
type T5_06 = Parse<`((+ 1 2) 3 4)`>;
type T5_07 = Parse<`(((+ 1 2) 3 4) 5 6)`>;
type T5_08 = Parse<`((1 . 2) . 3)`>;
type T5_09 = Parse<`((((1))))`>;
type T5_10 = Parse<`(((1)) ((2)))`>;
type T5_11 = Parse<`(1 (2 3) (4 5))`>;
type T5_12 = Parse<`(1 (2 (3 4) 5))`>;
type T5_13 = Parse<`(("a"))`>;
type T5_14 = Parse<`(list 1 2 (quote foo))`>;
type T5_15 = Parse<`(list (quote foo))`>;
type T5_16 = Parse<`(list 1 (quote foo) 2)`>;
type T5_17 = Parse<`(1.(2 . nil)))`>;
type T5_18 = Parse<`(1 .(2 . nil))`>;
// type T5_xx = Parse<`(1 . (2 . (3 . nil)))`>; // need counter reset
type T6_00 = Parse<
`(
white-line
"Hello World"
)`>;
type T6_01 = Parse<
`(
white-line
1
)`>;
type T6_02 = Parse<
`(
;white-line
read;comment
foo;;bar;;
1
)`>;
type T6_03 = Parse<
`(if nil
; true
(list 1 "foo")
; false
(list 2 "bar"))`>;
type T7_00 = Parse<`1 '()`>;
type T7_01 = Parse<`1 '(1)`>;
type T7_02 = Parse<`((1))`>;
type T7_03 = Parse<`'((1))`>;
type T7_04 = Parse<`('(1))`>;
type T7_05 = Parse<`'('(1))`>;
type T7_06 = Parse<`'('(1 . 2) . 3)`>;
type T7_07 = Parse<`'1`>;
type T7_08 = Parse<`'"1"`>;
type T7_09 = Parse<`'''1 '2`>
type T7_10 = Parse<`1 (')`>; // invalid
type T7_11 = Parse<`'`>; // invalid
type T7_12 = Parse<`(''')`>; // invalid
type T7_13 = Parse<`'(a'a)`>;