import { Token } from '../Lexer' ; import { Parser , ParseResult , ParserOutput } from './ParserInterface' ; import { seq } from './SequencialParser' ; export function apply < TKind , TFrom , TTo > ( p , callback ) { return { parse ( token ) { const output = p . parse ( token ) ; if ( output . successful ) { return { candidates : output . candidates . map ( ( value ) => { return { firstToken : token , nextToken : value . nextToken , result : callback ( value . result , [ token , value . nextToken ] ) } ; } ) , successful : true , error : output . error } ; } else { return output ; } } } ; } export function kleft < TKind , T1 , T2 > ( p1 , p2 ) { return apply ( seq ( p1 , p2 ) , ( value ) => { return value [ 0 ] ; } ) ; } export function kright < TKind , T1 , T2 > ( p1 , p2 ) { return apply ( seq ( p1 , p2 ) , ( value ) => { return value [ 1 ] ; } ) ; } export function kmid < TKind , T1 , T2 , T3 > ( p1 , p2 , p3 ) { return apply ( seq ( p1 , p2 , p3 ) , ( value ) => { return value [ 1 ] ; } ) ; }