/*-------------------------------------------------------------------------- @sinclair/parsebox The MIT License (MIT) Copyright (c) 2024 Haydn Paterson (sinclair) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------------*/ import * as Tokens from './token' import * as Types from './types' // ------------------------------------------------------------------ // Context // ------------------------------------------------------------------ // prettier-ignore type ContextParser = ( Parse extends [infer Context extends unknown, infer Rest extends string] ? Parse : [] ) // ------------------------------------------------------------------ // Array // ------------------------------------------------------------------ // prettier-ignore type ArrayParser = ( Parse extends [infer Value1 extends unknown, infer Rest extends string] ? ArrayParser : [Result, Code] ) // ------------------------------------------------------------------ // Const // ------------------------------------------------------------------ // prettier-ignore type ConstParser = ( Tokens.Const extends [infer Match extends Value, infer Rest extends string] ? [Match, Rest] : [] ) // ------------------------------------------------------------------ // Ident // ------------------------------------------------------------------ // prettier-ignore type IdentParser = ( Tokens.Ident extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : [] ) // ------------------------------------------------------------------ // Number // ------------------------------------------------------------------ // prettier-ignore type NumberParser = ( Tokens.Number extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : [] ) // ------------------------------------------------------------------ // Optional // ------------------------------------------------------------------ // prettier-ignore type OptionalParser = ( Parse extends [infer Value extends unknown, infer Rest extends string] ? [[Value], Rest] : [[], Code] ) // ------------------------------------------------------------------ // String // ------------------------------------------------------------------ // prettier-ignore type StringParser = ( Tokens.String extends [infer Match extends string, infer Rest extends string] ? [Match, Rest] : [] ) // ------------------------------------------------------------------ // Tuple // ------------------------------------------------------------------ // prettier-ignore type TupleParser = ( Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse extends [infer Value extends unknown, infer Rest extends string] ? TupleParser : [] : [Result, Code] ) // ------------------------------------------------------------------ // Union // ------------------------------------------------------------------ // prettier-ignore type UnionParser = ( Parsers extends [infer Left extends Types.IParser, ...infer Right extends Types.IParser[]] ? Parse extends [infer Value extends unknown, infer Rest extends string] ? [Value, Rest] : UnionParser : [] ) // ------------------------------------------------------------------ // Parse // ------------------------------------------------------------------ // prettier-ignore type ParseCode = ( Type extends Types.Context ? ContextParser : Type extends Types.Array ? ArrayParser : Type extends Types.Const ? ConstParser : Type extends Types.Ident ? IdentParser : Type extends Types.Number ? NumberParser : Type extends Types.Optional ? OptionalParser : Type extends Types.String ? StringParser : Type extends Types.Tuple ? TupleParser : Type extends Types.Union ? UnionParser : [] ) // prettier-ignore type ParseMapping = ( (Parser['mapping'] & { input: Result, context: Context })['output'] ) /** Parses code with the given parser */ // prettier-ignore export type Parse = ( ParseCode extends [infer L extends unknown, infer R extends string] ? [ParseMapping, R] : [] )