| 2443 | } |
| 2444 | |
| 2445 | const parse = ( |
| 2446 | input: ReadonlyArray<unknown>, |
| 2447 | options: ParseOptions |
| 2448 | ): Effect.Effect<unknown, SchemaIssue.Issue, any> => { |
| 2449 | const len = input.length |
| 2450 | const concurrency = options.concurrency === undefined ? 1 : resolveConcurrency(options.concurrency) |
| 2451 | const output: Array<unknown> = [] |
| 2452 | const refinement = concurrency === 1 ? itemRefinement : undefined |
| 2453 | let start = 0 |
| 2454 | let item: unknown |
| 2455 | if (refinement !== undefined) { |
| 2456 | // Leading items the refinement accepts are copied without a parser call |
| 2457 | while (start < len && refinement(item = input[start])) output[start++] = item |
| 2458 | if (start === len) return InternalParser.succeed(output) |
| 2459 | } |
| 2460 | const state: ArrayParserState = { |
| 2461 | ast, |
| 2462 | getParser, |
| 2463 | input, |
| 2464 | len, |
| 2465 | tailThreshold: Math.max(elementLen, len - tailLen), |
| 2466 | output, |
| 2467 | issues: undefined, |
| 2468 | options |
| 2469 | } |
| 2470 | if (refinement !== undefined) { |
| 2471 | // The rejected item goes to its parser here, so that it is read once |
| 2472 | const exit = rest![0].parser(item, options) as Exit.Exit<unknown, SchemaIssue.Issue> |
| 2473 | const terminal = stepArray(state, item, exit, start++) |
| 2474 | if (terminal) return terminal |
| 2475 | } |
| 2476 | const end = ast.rest.length === 0 ? elementLen : Math.max(len, elementLen + tailLen) |
| 2477 | const eff = concurrency === 1 |
| 2478 | ? parseArray(state, input, start, end) |
| 2479 | : parseArrayConcurrent(state, input, { concurrency, end }) |
| 2480 | if (!eff) return finish(state) |
| 2481 | if (effectIsExit(eff)) return Effect.flatMapEager(eff, () => finish(state)) |
| 2482 | // Reparse on later runs to avoid reusing mutable traversal state. |
| 2483 | let first = true |
| 2484 | return Effect.suspend(() => { |
| 2485 | if (!first) return parse(input, options) |
| 2486 | first = false |
| 2487 | return Effect.flatMap(eff, () => finish(state)) |
| 2488 | }) |
| 2489 | } |
| 2490 | |
| 2491 | return (input, options) => { |
| 2492 | if (input === InternalParser.missing) return InternalParser.missingExit |