|
| 1 | +import { assert } from "./internal_assert.ts"; |
| 2 | +import { |
| 3 | + type Context, |
| 4 | + type Failure, |
| 5 | + failure, |
| 6 | + isFatal, |
| 7 | + type Parser, |
| 8 | + type Result, |
| 9 | + success, |
| 10 | +} from "./Parser.ts"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Nondeterministic combinators. |
| 14 | + * |
| 15 | + * These are closer to "recognizers" / tokenizers: they can return multiple |
| 16 | + * successful alternatives for the same input position. |
| 17 | + * |
| 18 | + * Important: |
| 19 | + * - Do not use these inside `many(...)` unless you are explicitly advancing |
| 20 | + * the cursor yourself (otherwise you can create non-terminating loops). |
| 21 | + * - When multiple matches exist, you must decide how (or whether) to advance. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * A single recognized match: a value and the context where that value ends. |
| 26 | + */ |
| 27 | +export type Recognition<T> = Readonly<{ |
| 28 | + value: T; |
| 29 | + ctx: Context; |
| 30 | +}>; |
| 31 | + |
| 32 | +type UnwrapParser<T> = T extends Parser<infer U> ? U : T; |
| 33 | + |
| 34 | +type UnwrapParsers<T extends [...unknown[]]> = T extends [ |
| 35 | + infer Head, |
| 36 | + ...infer Tail, |
| 37 | +] ? [UnwrapParser<Head>, ...UnwrapParsers<Tail>] |
| 38 | + : []; |
| 39 | + |
| 40 | +type ArrayUnion<T extends unknown[]> = T extends [infer Head, ...infer Tail] |
| 41 | + ? Head | ArrayUnion<Tail> |
| 42 | + : never; |
| 43 | + |
| 44 | +/** |
| 45 | + * Run all parsers at the same input position and return every successful match. |
| 46 | + * |
| 47 | + * - On success: returns an array of recognitions (value + end ctx) and does NOT |
| 48 | + * advance the returned success ctx (it stays at the starting ctx). |
| 49 | + * - If no parser matches: returns the failure of the parser that got furthest. |
| 50 | + * - Fatal failures propagate immediately (no backtracking). |
| 51 | + * |
| 52 | + * Matches are returned sorted by descending end index (longest match first), |
| 53 | + * stable for ties (preserves parser order). |
| 54 | + */ |
| 55 | +export const recognizeAt = <T extends [...Parser<unknown>[]]>( |
| 56 | + ...parsers: [...T] |
| 57 | +): Parser<Recognition<ArrayUnion<UnwrapParsers<T>>>[]> => { |
| 58 | + return (ctx) => { |
| 59 | + if (parsers.length === 0) { |
| 60 | + return failure(ctx, "recognizeAt: expected at least one parser"); |
| 61 | + } |
| 62 | + |
| 63 | + const matches: Recognition<ArrayUnion<UnwrapParsers<T>>>[] = []; |
| 64 | + let furthestFailure: Failure | undefined; |
| 65 | + |
| 66 | + for (const parser of parsers) { |
| 67 | + const res = parser(ctx) as Result<ArrayUnion<UnwrapParsers<T>>>; |
| 68 | + |
| 69 | + if (!res.success && isFatal(res)) return res; |
| 70 | + |
| 71 | + if (res.success) { |
| 72 | + matches.push({ value: res.value, ctx: res.ctx }); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + if (!furthestFailure || furthestFailure.ctx.index < res.ctx.index) { |
| 77 | + furthestFailure = res; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (matches.length === 0) { |
| 82 | + assert(furthestFailure); |
| 83 | + return furthestFailure; |
| 84 | + } |
| 85 | + |
| 86 | + // Longest match first; stable for equal indexes. |
| 87 | + const sorted = matches |
| 88 | + .map((m, i) => ({ m, i })) |
| 89 | + .sort((a, b) => (b.m.ctx.index - a.m.ctx.index) || (a.i - b.i)) |
| 90 | + .map((x) => x.m); |
| 91 | + |
| 92 | + return success(ctx, sorted); |
| 93 | + }; |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Try all parsers and collect all successful matches that consume the most |
| 98 | + * input, then return them as an array and advance to that furthest position. |
| 99 | + * |
| 100 | + * If none match, returns the failure result of the parser that consumed the |
| 101 | + * most input. |
| 102 | + * |
| 103 | + * Fatal errors are propagated immediately. |
| 104 | + */ |
| 105 | +export const furthestAll = <T extends [...Parser<unknown>[]]>( |
| 106 | + ...parsers: [...T] |
| 107 | +): Parser<ArrayUnion<UnwrapParsers<T>>[]> => { |
| 108 | + return (ctx) => { |
| 109 | + if (parsers.length === 0) { |
| 110 | + return failure( |
| 111 | + ctx, |
| 112 | + "furthestAll: expected at least one parser", |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + let bestIndex = -1; |
| 117 | + let bestCtx: Context | undefined; |
| 118 | + let bestValues: ArrayUnion<UnwrapParsers<T>>[] = []; |
| 119 | + |
| 120 | + let furthestFailure: Failure | undefined; |
| 121 | + |
| 122 | + for (const parser of parsers) { |
| 123 | + const res = parser(ctx) as Result<ArrayUnion<UnwrapParsers<T>>>; |
| 124 | + |
| 125 | + // Fatal errors propagate immediately - no backtracking |
| 126 | + if (!res.success && isFatal(res)) { |
| 127 | + return res; |
| 128 | + } |
| 129 | + |
| 130 | + if (res.success) { |
| 131 | + const idx = res.ctx.index; |
| 132 | + if (idx > bestIndex) { |
| 133 | + bestIndex = idx; |
| 134 | + bestCtx = res.ctx; |
| 135 | + bestValues = [res.value]; |
| 136 | + } else if (idx === bestIndex) { |
| 137 | + bestValues.push(res.value); |
| 138 | + } |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + if (!furthestFailure || furthestFailure.ctx.index < res.ctx.index) { |
| 143 | + furthestFailure = res; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (bestCtx) { |
| 148 | + return success(bestCtx, bestValues); |
| 149 | + } |
| 150 | + |
| 151 | + assert(furthestFailure); |
| 152 | + return furthestFailure; |
| 153 | + }; |
| 154 | +}; |
| 155 | + |
| 156 | +/** |
| 157 | + * Try all parsers and collect all successful matches (even if they consume |
| 158 | + * different amounts of input). The returned context advances to the furthest |
| 159 | + * successful match (highest index), so parsing can continue from the longest |
| 160 | + * match. |
| 161 | + * |
| 162 | + * If none match, returns the failure result of the parser that consumed the |
| 163 | + * most input. |
| 164 | + * |
| 165 | + * Fatal errors are propagated immediately. |
| 166 | + */ |
| 167 | +export const allMatches = <T extends [...Parser<unknown>[]]>( |
| 168 | + ...parsers: [...T] |
| 169 | +): Parser<ArrayUnion<UnwrapParsers<T>>[]> => { |
| 170 | + return (ctx) => { |
| 171 | + if (parsers.length === 0) { |
| 172 | + return failure( |
| 173 | + ctx, |
| 174 | + "allMatches: expected at least one parser", |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + let bestSuccessCtx: Context | undefined; |
| 179 | + const values: ArrayUnion<UnwrapParsers<T>>[] = []; |
| 180 | + |
| 181 | + let furthestFailure: Failure | undefined; |
| 182 | + |
| 183 | + for (const parser of parsers) { |
| 184 | + const res = parser(ctx) as Result<ArrayUnion<UnwrapParsers<T>>>; |
| 185 | + |
| 186 | + // Fatal errors propagate immediately - no backtracking |
| 187 | + if (!res.success && isFatal(res)) { |
| 188 | + return res; |
| 189 | + } |
| 190 | + |
| 191 | + if (res.success) { |
| 192 | + values.push(res.value); |
| 193 | + if (!bestSuccessCtx || bestSuccessCtx.index < res.ctx.index) { |
| 194 | + bestSuccessCtx = res.ctx; |
| 195 | + } |
| 196 | + continue; |
| 197 | + } |
| 198 | + |
| 199 | + if (!furthestFailure || furthestFailure.ctx.index < res.ctx.index) { |
| 200 | + furthestFailure = res; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + if (bestSuccessCtx) { |
| 205 | + return success(bestSuccessCtx, values); |
| 206 | + } |
| 207 | + |
| 208 | + assert(furthestFailure); |
| 209 | + return furthestFailure; |
| 210 | + }; |
| 211 | +}; |
0 commit comments