module /** */ import { ParsingState } from "../state" ; import { FailureInfo , ResultKind } from "../result" ; import { Parjser , ParjsProjection , ParjsValidator } from "../parjser" ; import { ParjserBase } from "../parser" ; import defaults from "lodash/defaults" ; const defaultFailure = { kind : "Soft" , reason : "expecting a character fulfilling a predicate" } ; param predicate export function charWhere ( predicate ) { return new class CharWhere extends ParjserBase { type = "charWhere" ; expecting = "expecting a character matching a predicate" ; _apply ( ps ) { let { position , input } = ps ; if ( position >= input . length ) { ps . kind = ResultKind . SoftFail ; ps . reason = "expecting a character" ; return ; } let curChar = input [ position ] ; let result = predicate ( curChar , ps . userState ) ; if ( result !== true ) { ps . reason = result . reason || this . expecting ; ps . kind = result . kind || "Soft" ; return ; } ps . value = curChar ; ps . position ++ ; ps . kind = ResultKind . Ok ; } } ( ) ; }