module /** */ import { ParjsCombinator , UserState } from "../../index" ; import { defineCombinator } from "./combinator" ; import { ParjserBase , ParserUserState } from "../parser" ; import { ParsingState } from "../state" ; import cloneDeep from "lodash/cloneDeep" ; import defaults from "lodash/defaults" ; /**
 * A user state object or a projection to the external user state.
 */ export type UserStateOrProjection = UserState | ( ( externalState : UserState ) => UserState ) ; param innerStateOrCtor export function replaceState < T > ( innerStateOrCtor ) { return defineCombinator ( source => { return new class IsolateState extends ParjserBase { type = "replaceState" ; expecting = source . expecting ; _apply ( ps ) { let state = ps . userState ; if ( typeof innerStateOrCtor === "function" ) { ps . userState = defaults ( new ParserUserState ( ) , innerStateOrCtor ( state ) ) ; } else { ps . userState = defaults ( new ParserUserState ( ) , innerStateOrCtor ) ; } source . apply ( ps ) ; ps . userState = state ; } } ( ) ; } ) ; }