module /** */ import { Issues } from "../issues" ; import { ParsingState } from "../state" ; import { ResultKind } from "../result" ; import { ParjsCombinator } from "../../index" ; import { defineCombinator } from "./combinator" ; import { ParjserBase } from "../parser" ; param maxIterations export function many < T > ( maxIterations ? ) ; export function many ( maxIterations = Infinity ) { return defineCombinator ( source => { return new class Many extends ParjserBase { type = "many" ; expecting = source . expecting ; _apply ( ps ) { let { position } = ps ; let arr = [ ] as any [ ] ; let i = 0 ; while ( true ) { source . apply ( ps ) ; if ( ! ps . isOk ) break ; if ( i >= maxIterations ) break ; if ( maxIterations === Infinity && ps . position === position ) { Issues . guardAgainstInfiniteLoop ( "many" ) ; } position = ps . position ; arr . push ( ps . value ) ; i ++ ; } if ( ps . atLeast ( ResultKind . HardFail ) ) { return ; } ps . value = arr ; ps . position = position ; ps . kind = ResultKind . Ok ; } } ( ) ; } ) ; }