This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ts
25 lines (25 loc) · 8.87 KB
/
parser.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
type the<a extends unknown, b extends a> = b
type plus<a extends number, b extends number> = [[0, 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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [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], [27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [42, 43, 44, 45, 46, 47, 48, 49, 50], [43, 44, 45, 46, 47, 48, 49, 50], [44, 45, 46, 47, 48, 49, 50], [45, 46, 47, 48, 49, 50], [46, 47, 48, 49, 50], [47, 48, 49, 50], [48, 49, 50], [49, 50], [50]][a][b]
type record_fix<a extends unknown> = the<unknown, { [key in keyof a]: a[key] }>
type record_empty = the<unknown, { }>
type record_singleton<key extends string, value extends unknown> = the<unknown, { [k in key]: value }>
type record_merge<a extends unknown, b extends unknown> = the<unknown, (a & b)>
type space = the<string, " " | "\n">
type unit = the<unknown, "mk-unit">
type mk_unit = the<unit, "mk-unit">
type result<a extends unknown> = the<unknown, ["ok", unknown, a, string] | ["fail", unknown]>
type ok<a extends unknown, value extends a, rest extends string> = the<result<a>, ["ok", a, value, rest]>
type fail<a extends unknown> = the<result<a>, ["fail", a]>
type parse_string<str extends string, s extends string> = the<result<unit>, s extends `${str}${(infer s)}` ? ok<unit, mk_unit, s> : s extends unknown ? fail<unit> : never>
type parse_space<s extends string> = the<result<unit>, s extends `${space}${(infer s)}` ? parse_space<s> : s extends unknown ? ok<unit, mk_unit, s> : never>
type parse_name<s extends string> = the<result<string>, s extends `${(infer name)}:${(infer s)}` ? ok<string, name, `:${s}`> : s extends unknown ? fail<string> : never>
type parse_type<s extends string> = the<result<unknown>, s extends `number${(infer s)}` ? ok<unknown, number, s> : s extends `string${(infer s)}` ? ok<unknown, string, s> : s extends `boolean${(infer s)}` ? ok<unknown, boolean, s> : s extends unknown ? parse_record<s> : never>
type parse_field<s extends string> = the<result<unknown>, parse_name<s> extends fail<string> ? fail<unknown> : parse_name<s> extends ok<string, the<string, (infer name)>, (infer s)> ? parse_string<": ", s> extends fail<unit> ? fail<unknown> : parse_string<": ", s> extends ok<unit, mk_unit, (infer s)> ? parse_type<s> extends fail<unknown> ? fail<unknown> : parse_type<s> extends ok<unknown, (infer type_), (infer s)> ? ok<unknown, record_singleton<name, type_>, s> : never : never : never>
type parse_fields<s extends string> = the<result<unknown>, parse_field<s> extends fail<unknown> ? ok<unknown, record_empty, s> : parse_field<s> extends ok<unknown, (infer field), (infer s)> ? parse_string<",", s> extends fail<unit> ? ok<unknown, field, s> : parse_string<",", s> extends ok<unit, mk_unit, (infer s)> ? parse_space<s> extends fail<unit> ? fail<unknown> : parse_space<s> extends ok<unit, mk_unit, (infer s)> ? parse_fields<s> extends fail<unknown> ? fail<unknown> : parse_fields<s> extends ok<unknown, (infer fields), (infer s)> ? ok<unknown, record_merge<field, fields>, s> : never : never : never : never>
type parse_record<s extends string> = the<result<unknown>, parse_string<"{", s> extends fail<unit> ? fail<unknown> : parse_string<"{", s> extends ok<unit, mk_unit, (infer s)> ? parse_space<s> extends fail<unit> ? fail<unknown> : parse_space<s> extends ok<unit, mk_unit, (infer s)> ? parse_fields<s> extends fail<unknown> ? fail<unknown> : parse_fields<s> extends ok<unknown, (infer fields), (infer s)> ? parse_space<s> extends fail<unit> ? fail<unknown> : parse_space<s> extends ok<unit, mk_unit, (infer s)> ? parse_string<"}", s> extends fail<unit> ? fail<unknown> : parse_string<"}", s> extends ok<unit, mk_unit, (infer s)> ? ok<unknown, record_fix<fields>, s> : never : never : never : never : never>
type run_type_result<result extends result<unknown>> = the<unknown, result extends ok<unknown, (infer value), ""> ? value : result extends unknown ? never : never>
type result_number = the<unknown, run_type_result<parse_type<"number">>>
type result_record = the<unknown, run_type_result<parse_type<`{
a: number, e: boolean,
b: { c: string, d: string }
}`>>>