JSON Parser
提出詳細
type Pure<T> = { [P in keyof T]: T[P] extends object ? Pure<T[P]> : T[P] } type SetProperty<T, K extends PropertyKey, V> = { [P in (keyof T) | K]: P extends K ? V : P extends keyof T ? T[P] : never } type WhiteSpace = " " | "\n" | "\t" type StringLiteral = `"${string}"` type Token = null | boolean | "[" | "]" | "{" | "}" | "," | ":" | StringLiteral type SkipWhiteSpace<S extends string> = S extends `${WhiteSpace}${infer S2}` ? SkipWhiteSpace<S2> : S type Tokenize< S extends string, Tokens extends Token[] = [], TrimedString extends string = SkipWhiteSpace<S> > = TrimedString extends "" ? Tokens : TrimedString extends `${Token}${infer Rest}` ? TrimedString extends `${infer T extends Token}${Rest}` ? Tokenize<Rest, [...Tokens, T]> : never // never reached : never // unknown token error type EscapeCharacterDict = { "\\": "\\", "/": "\/", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", } type ParseEscapeCharacters<S extends string, Acc extends string=""> = S extends `${infer S1}${infer S2}` ? S1 extends "\\" ? S2 extends `${infer C extends keyof EscapeCharacterDict}${infer S3}` ? ParseEscapeCharacters<S3, `${Acc}${EscapeCharacterDict[C]}`> : never : ParseEscapeCharacters<S2, `${Acc}${S1}`> : Acc type ParseArray<T extends Token[]> = T extends [ "]", ...infer Rest ] ? [[], Rest] : ParseArrayElements<T> type ParseArrayElements<T extends Token[], Values extends readonly unknown[] = []> = ParseValue<T> extends [ infer V, infer Rest extends Token[] ] ? Rest extends [ infer R1 extends Token, ...infer R2 extends Token[] ] ? R1 extends "," ? ParseArrayElements<R2, [...Values, V]> : R1 extends "]" ? [[...Values, V], R2] : never : never : never type ParseObject<T extends Token[]> = T extends [ "}", ...infer Rest ] ? [{}, Rest] : ParseMembers<T> type ParseMembers<T extends Token[], Obj extends object = {}> = ParseMember<T> extends [ infer K extends PropertyKey, infer V, infer Rest extends Token[] ] ? Rest extends [ infer R1 extends Token, ...infer R2 extends Token[] ] ? R1 extends "," ? ParseMembers<R2, SetProperty<Obj, K, V>> : R1 extends "}" ? [SetProperty<Obj, K, V>, R2] : never : never : never type ParseMember<T extends Token[]> = T extends [ `"${infer Key}"`, ":", ...infer Rest extends Token[] ] ? ParseValue<Rest> extends [ infer V, infer Rest] ? [ ParseEscapeCharacters<Key>, V, Rest ] : never : never type ParseValue<T extends Token[]> = T extends [infer T1, ...infer T2 extends Token[]] ? T1 extends boolean | null ? [T1, T2] : T1 extends `"${infer S}"` ? [ParseEscapeCharacters<S>, T2] : T1 extends "[" ? ParseArray<T2> : T1 extends "{" ? ParseObject<T2> : never : never type Parse<T extends string> = Pure<ParseValue<Tokenize<T>>[0]>
提出日時 | 2023-09-20 15:53:30 |
---|---|
問題 | JSON Parser |
ユーザー | sankantsu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<( Parse<` { "a": "b", "b": false, "c": [true, false, "hello", { "a": "b", "b": false }], "nil": null } `> ), ( { nil: null c: [true, false, 'hello', { a: 'b' b: false }] b: false a: 'b' } )>>, Expect<Equal<Parse<'{}'>, {}>>, Expect<Equal<Parse<'[]'>, []>>, Expect<Equal<Parse<'[1]'>, never>>, Expect<Equal<Parse<'true'>, true>>, Expect<Equal< Parse<'["Hello", true, false, null]'>, ['Hello', true, false, null] >>, Expect<Equal< ( Parse<` { "hello\\r\\n\\b\\f": "world" }`> ), ( { 'hello\r\n\b\f': 'world' } ) >>, Expect<Equal<Parse<'{ 1: "world" }'>, never>>, Expect<Equal<Parse<`{ "hello world": 123 }`>, never>>, ]