parjs
Version:
Library for building parsers using combinators.
47 lines • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fail = exports.nope = void 0;
/** @module parjs */
const utils_1 = require("../../utils");
const parser_1 = require("../parser");
const defaultFailure = {
kind: "Hard",
reason: "User initiated failure."
};
/**
* Returns a parser that fails softly with a given reason.
*
* @param reason The reason for the failure.
*/
function nope(reason) {
return fail({
kind: "Soft",
reason
});
}
exports.nope = nope;
class Fail extends parser_1.ParjserBase {
constructor(failure) {
super();
this.failure = failure;
this.type = "fail";
this.expecting = this.failure.reason;
}
_apply(ps) {
ps.kind = this.failure.kind;
ps.reason = this.expecting;
}
}
/**
* Returns a parser that will always fail with the given failure info.
*
* @param pFailure How the parser should fail.
*/
function fail(pFailure) {
const failure = typeof pFailure === "string"
? { kind: "Hard", reason: pFailure }
: (0, utils_1.defaults)(pFailure, defaultFailure);
return new Fail(failure);
}
exports.fail = fail;
//# sourceMappingURL=fail.js.map