parjs
Version:
Library for building parsers using combinators.
33 lines • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybe = void 0;
const combinated_1 = require("../combinated");
const result_1 = require("../result");
const wrap_implicit_1 = require("../wrap-implicit");
class MaybeCombinator extends combinated_1.Combinated {
constructor(source, _val) {
super(source);
this._val = _val;
this.type = "maybe";
this.expecting = "expecting anything";
}
_apply(ps) {
this.source.apply(ps);
if (ps.isSoft) {
// on soft failure, set the value and result to OK
ps.value = this._val;
ps.kind = result_1.ResultKind.Ok;
}
// on ok/hard/fatal, propagate the result.
}
}
/**
* Applies the source parser. If it fails softly, succeeds and yields `val`.
*
* @param val
*/
function maybe(val) {
return source => new MaybeCombinator((0, wrap_implicit_1.wrapImplicit)(source), val);
}
exports.maybe = maybe;
//# sourceMappingURL=maybe.js.map