UNPKG

parjs

Version:

A parser-combinator library for JavaScript.

44 lines (43 loc) 1.34 kB
"use strict"; /** * @module parjs/combinators */ /** */ Object.defineProperty(exports, "__esModule", { value: true }); const combinator_1 = require("./combinator"); const parser_1 = require("../parser"); /** * Reduces Hard failures to Soft ones and behaves in the same way on success. */ function recover(recoverFunction) { return combinator_1.defineCombinator(source => { return new class Soft extends parser_1.ParjserBase { constructor() { super(...arguments); this.type = "recover"; this.expecting = source.expecting; } _apply(ps) { source.apply(ps); if (ps.isOk || ps.isFatal) return; let result = recoverFunction({ userState: ps.userState, kind: ps.kind, reason: ps.reason }); if (!result) return; ps.kind = result.kind || ps.kind; if (result.kind === "OK") { ps.value = result.value; } else { ps.reason = result.reason || ps.reason; } } }(); }); } exports.recover = recover; //# sourceMappingURL=recover.js.map