|
1 | | -/*-------------------------------------------------------------------------- |
2 | 1 |
|
3 | | -ParseBox |
4 | | -
|
5 | | -The MIT License (MIT) |
6 | | -
|
7 | | -Copyright (c) 2024-2025 Haydn Paterson |
8 | | -
|
9 | | -Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | -of this software and associated documentation files (the "Software"), to deal |
11 | | -in the Software without restriction, including without limitation the rights |
12 | | -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | -copies of the Software, and to permit persons to whom the Software is |
14 | | -furnished to do so, subject to the following conditions: |
15 | | -
|
16 | | -The above copyright notice and this permission notice shall be included in |
17 | | -all copies or substantial portions of the Software. |
18 | | -
|
19 | | -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | -THE SOFTWARE. |
26 | | -
|
27 | | ----------------------------------------------------------------------------*/ |
28 | | - |
29 | | -// deno-fmt-ignore-file |
30 | | - |
31 | | -import { Token } from '../token/index.ts' |
32 | | -import * as Types from './types.ts' |
33 | | - |
34 | | - |
35 | | -// ------------------------------------------------------------------ |
36 | | -// Ident |
37 | | -// ------------------------------------------------------------------ |
38 | | - |
39 | | -// ------------------------------------------------------------------ |
40 | | -// Number |
41 | | -// ------------------------------------------------------------------ |
42 | | - |
43 | | -// ------------------------------------------------------------------ |
44 | | -// Optional |
45 | | -// ------------------------------------------------------------------ |
46 | | - |
47 | | -// ------------------------------------------------------------------ |
48 | | -// String |
49 | | -// ------------------------------------------------------------------ |
50 | | - |
51 | | -// ------------------------------------------------------------------ |
52 | | -// Tuple |
53 | | -// ------------------------------------------------------------------ |
54 | | - |
55 | | -// ------------------------------------------------------------------ |
56 | | -// Union |
57 | | -// ------------------------------------------------------------------ |
58 | | - |
59 | | -// ------------------------------------------------------------------ |
60 | | -// Until |
61 | | -// ------------------------------------------------------------------ |
62 | | - |
63 | | -// ------------------------------------------------------------------ |
64 | | -// UntilNonEmpty |
65 | | -// ------------------------------------------------------------------ |
| 2 | +import type { ParseArray, Array } from './array.ts' |
| 3 | +import type { ParseConst, Const } from './const.ts' |
| 4 | +import type { ParseIdent, Ident } from './ident.ts' |
| 5 | +import type { ParseNumber, Number } from './number.ts' |
| 6 | +import type { ParseOptional, Optional } from './optional.ts' |
| 7 | +import type { ParseString, String } from './string.ts' |
| 8 | +import type { ParseTuple, Tuple } from './tuple.ts' |
| 9 | +import type { ParseUnion, Union } from './union.ts' |
| 10 | +import type { ParseUntil_1, Until_1 } from './until_1.ts' |
| 11 | +import type { ParseUntil, Until } from './until.ts' |
| 12 | + |
| 13 | +import type { IParser } from './parser.ts' |
| 14 | + |
| 15 | +// ------------------------------------------------------------------ |
| 16 | +// ParseInput |
| 17 | +// ------------------------------------------------------------------ |
| 18 | +type ParseInput<Input extends string, Parser extends IParser> = ( |
| 19 | + Parser extends Array<infer Parser extends IParser> ? ParseArray<Parser, Input> : |
| 20 | + Parser extends Const<infer Const extends string> ? ParseConst<Const, Input> : |
| 21 | + Parser extends Ident ? ParseIdent<Input> : |
| 22 | + Parser extends Number ? ParseNumber<Input> : |
| 23 | + Parser extends Optional<infer Parser extends IParser> ? ParseOptional<Parser, Input> : |
| 24 | + Parser extends String ? ParseString<Input> : |
| 25 | + Parser extends Tuple<infer Parsers extends IParser[]> ? ParseTuple<Parsers, Input> : |
| 26 | + Parser extends Union<infer Parsers extends IParser[]> ? ParseUnion<Parsers, Input> : |
| 27 | + Parser extends Until<infer End extends string[]> ? ParseUntil<End, Input> : |
| 28 | + Parser extends Until_1<infer End extends string[]> ? ParseUntil_1<End, Input> : |
| 29 | + [] |
| 30 | +) |
| 31 | +// ------------------------------------------------------------------ |
| 32 | +// ParseMapping |
| 33 | +// ------------------------------------------------------------------ |
| 34 | +type ParseMapping<Parser extends IParser, ParseResult extends unknown = unknown> = ( |
| 35 | + (Parser['mapping'] & { input: ParseResult })['output'] |
| 36 | +) |
| 37 | +// ------------------------------------------------------------------ |
| 38 | +// Parse |
| 39 | +// ------------------------------------------------------------------ |
| 40 | +/** Parses code with the given parser */ |
| 41 | +export type Parse<Parser extends IParser, Input extends string> = ( |
| 42 | + ParseInput<Input, Parser> extends [infer ParseResult extends unknown, infer InputRest extends string] |
| 43 | + ? [ParseMapping<Parser, ParseResult>, InputRest] |
| 44 | + : [] |
| 45 | +) |
0 commit comments