UNPKG

parjs

Version:

Library for building parsers using combinators.

38 lines 1.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.recover = void 0; const combinated_1 = require("../combinated"); const wrap_implicit_1 = require("../wrap-implicit"); class Soft extends combinated_1.Combinated { constructor(source, recoverFunction) { super(source); this.recoverFunction = recoverFunction; this.type = "recover"; this.expecting = this.source.expecting; } _apply(ps) { this.source.apply(ps); if (ps.isOk || ps.isFatal) return; const result = this.recoverFunction({ userState: ps.userState, kind: ps.kind, reason: ps.reason // the error is guaranteed to be non-null }); if (!result) return; ps.kind = result.kind || ps.kind; if (result.kind === "OK") { ps.value = result.value; } else { ps.reason = result.reason || ps.reason; } } } /** Reduces Hard failures to Soft ones and behaves in the same way on success. */ function recover(recoverFunction) { return source => new Soft((0, wrap_implicit_1.wrapImplicit)(source), recoverFunction); } exports.recover = recover; //# sourceMappingURL=recover.js.map