module /** */ import { ResultKind } from "../result" ; import { ParsingState } from "../state" ; import { ImplicitParjser , ParjsCombinator } from "../../index" ; import { composeCombinator , defineCombinator } from "./combinator" ; import { ParjserBase } from "../parser" ; import { ScalarConverter } from "../scalar-converter" ; import { map } from "./map" ; param next export function qthen < T > ( next ) { return composeCombinator ( then ( next ) , map ( arr => arr [ 1 ] ) ) ; } param next export function thenq < T > ( next ) { return composeCombinator ( then ( next ) , map ( arr => arr [ 0 ] ) ) ; } param next export function then < A , B > ( next ) ; param next1 param next2 export function then < A , B , C > ( next1 , next2 ) ; param next1 param next2 param next3 export function then < A , B , C , D > ( next1 , next2 , next3 ) ; param next1 param next2 param next3 param next4 export function then < A , B , C , D , E > ( next1 , next2 , next3 , next4 ) ; export function then ( ... parsers ) { let resolvedParsers = parsers . map ( x => ScalarConverter . convert ( x ) as any as ParjserBase ) ; return defineCombinator ( source => { resolvedParsers . splice ( 0 , 0 , source ) ; return new class Then extends ParjserBase { type = "then" ; expecting = source . expecting ; _apply ( ps ) { let results = [ ] as any [ ] ; let origPos = ps . position ; for ( let i = 0 ; i < resolvedParsers . length ; i ++ ) { let cur = resolvedParsers [ i ] ; cur . apply ( ps ) ; if ( ps . isOk ) { results . push ( ps . value ) ; } else if ( ps . isSoft && origPos === ps . position ) { return ; } else if ( ps . isSoft ) { ps . kind = ResultKind . HardFail ; return ; } else { return ; } } ps . value = results ; ps . kind = ResultKind . Ok ; } } ( ) ; } ) ; }