UNPKG

parjs

Version:

Library for building parsers using combinators.

43 lines 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceState = void 0; const utils_1 = require("../../utils"); const combinated_1 = require("../combinated"); const parser_1 = require("../parser"); const wrap_implicit_1 = require("../wrap-implicit"); class IsolateState extends combinated_1.Combinated { constructor(source, innerStateOrCtor) { super(source); this.innerStateOrCtor = innerStateOrCtor; this.type = "replaceState"; this.expecting = this.source.expecting; } _apply(ps) { const state = ps.userState; const { innerStateOrCtor, source } = this; if (typeof innerStateOrCtor === "function") { ps.userState = (0, utils_1.defaults)(new parser_1.ParserUserState(), innerStateOrCtor(state)); } else { ps.userState = (0, utils_1.defaults)(new parser_1.ParserUserState(), innerStateOrCtor); } source.apply(ps); ps.userState = state; } } /** * When the source parser is applied, the user state will be switched for a different object. After * it has finished, the previous user state will be restored. This effectively isolates the source * parser's user state. * * If the given paramter is a function, it will be called on the pre-existing user state to * determine the new user state. If it's a non-fuction object, it will be used as the user state * instead. * * @param innerStateOrCtor The new internal user state or a projection on the existing user state. */ function replaceState(innerStateOrCtor) { return source => new IsolateState((0, wrap_implicit_1.wrapImplicit)(source), innerStateOrCtor); } exports.replaceState = replaceState; //# sourceMappingURL=replace-state.js.map