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
| type ParserError<T extends string> = { error: true } & T type EatWhitespace<State extends string> = string extends State ? ParserError<"EatWhitespace got generic string type"> : State extends ` ${infer State}` | `\n${infer State}` ? EatWhitespace<State> : State type AddKeyValue<Memo extends Record<string, any>, Key extends string, Value extends any> = Memo & { [K in Key]: Value } type ParseJsonObject<State extends string, Memo extends Record<string, any> = {}> = string extends State ? ParserError<"ParseJsonObject got generic string type"> : EatWhitespace<State> extends `}${infer State}` ? [Memo, State] : EatWhitespace<State> extends `"${infer Key}"${infer State}` ? EatWhitespace<State> extends `:${infer State}` ? ParseJsonValue<State> extends [infer Value, `${infer State}`] ? EatWhitespace<State> extends `,${infer State}` ? ParseJsonObject<State, AddKeyValue<Memo, Key, Value>> : EatWhitespace<State> extends `}${infer State}` ? [AddKeyValue<Memo, Key, Value>, State] : ParserError<`ParseJsonObject received unexpected token: ${State}`> : ParserError<`ParseJsonValue returned unexpected value for: ${State}`> : ParserError<`ParseJsonObject received unexpected token: ${State}`> : ParserError<`ParseJsonObject received unexpected token: ${State}`> type ParseJsonArray<State extends string, Memo extends any[] = []> = string extends State ? ParserError<"ParseJsonArray got generic string type"> : EatWhitespace<State> extends `]${infer State}` ? [Memo, State] : ParseJsonValue<State> extends [infer Value, `${infer State}`] ? EatWhitespace<State> extends `,${infer State}` ? ParseJsonArray<EatWhitespace<State>, [...Memo, Value]> : EatWhitespace<State> extends `]${infer State}` ? [[...Memo, Value], State] : ParserError<`ParseJsonArray received unexpected token: ${State}`> : ParserError<`ParseJsonValue returned unexpected value for: ${State}`> type ParseJsonValue<State extends string> = string extends State ? ParserError<"ParseJsonValue got generic string type"> : EatWhitespace<State> extends `null${infer State}` ? [null, State] : EatWhitespace<State> extends `true${infer State}` ? [true, State] : EatWhitespace<State> extends `false${infer State}` ? [false, State] : EatWhitespace<State> extends `"${infer Value}"${infer State}` ? [Value, State] : EatWhitespace<State> extends `[${infer State}` ? ParseJsonArray<State> : EatWhitespace<State> extends `{${infer State}` ? ParseJsonObject<State> : ParserError<`ParseJsonValue received unexpected token: ${State}`> export type ParseJson<T extends string> = ParseJsonValue<T> extends infer Result ? Result extends [infer Value, string] ? Value : Result extends ParserError<any> ? Result : ParserError<"ParseJsonValue returned unexpected Result"> : ParserError<"ParseJsonValue returned uninferrable Result">
|