module /** */ import { ParsingState } from "../state" ; import { ResultKind } from "../result" ; import { ParjsCombinator } from "../.." ; import { ImplicitParjser , ScalarConverter } from "../scalar-converter" ; import { defineCombinator } from "./combinator" ; import { ParjserBase } from "../parser" ; param alt1 export function or < T1 , T2 > ( alt1 ) ; param alt1 param alt2 export function or < T1 , T2 , T3 > ( alt1 , alt2 ) ; param alt1 param alt2 param alt3 export function or < T1 , T2 , T3 , T4 > ( alt1 , alt2 , alt3 ) ; param alt1 param alt2 param alt3 param alt4 export function or < T1 , T2 , T3 , T4 , T5 > ( alt1 , alt2 , alt3 , alt4 ) ; export function or ( ... alts ) { let resolvedAlts = alts . map ( x => ScalarConverter . convert ( x ) as any as ParjserBase ) ; return defineCombinator ( source => { resolvedAlts . splice ( 0 , 0 , source ) ; let altNames = resolvedAlts . map ( x => x . type ) ; return new class Or extends ParjserBase { type = "or" ; expecting = `expecting one of: ${ altNames . join ( ", " ) }` ; _apply ( ps ) { let { position } = ps ; for ( let i = 0 ; i < resolvedAlts . length ; i ++ ) { let cur = resolvedAlts [ i ] ; cur . apply ( ps ) ; if ( ps . isOk ) { return ; } else if ( ps . isSoft ) { ps . position = position ; } else { return ; } } ps . kind = ResultKind . SoftFail ; } } ( ) ; } ) ; }