parjs
Version:
Library for building parsers using combinators.
70 lines • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.then = exports.thenq = exports.qthen = void 0;
const result_1 = require("../result");
const combinated_1 = require("../combinated");
const wrap_implicit_1 = require("../wrap-implicit");
const combinator_1 = require("./combinator");
const map_1 = require("./map");
/**
* Applies the source parser followed by `next`. Yields the result of `next`.
*
* @param next
*/
function qthen(next) {
return (0, combinator_1.composeCombinator)(then(next), (0, map_1.map)(arr => arr[1]));
}
exports.qthen = qthen;
/**
* Applies the source parser followed by `next`. Yields the result of the source parser.
*
* @param next
*/
function thenq(next) {
return (0, combinator_1.composeCombinator)(then(next), (0, map_1.map)(arr => arr[0]));
}
exports.thenq = thenq;
class Then extends combinated_1.Combinated {
constructor(source, _rest) {
super(source);
this._rest = _rest;
this.type = "then";
this.expecting = this.source.expecting;
this._seq = [this.source, ...this._rest];
}
_apply(ps) {
const results = [];
const { _seq } = this;
const origPos = ps.position;
for (const cur of _seq) {
cur.apply(ps);
if (ps.isOk) {
results.push(ps.value);
}
else if (ps.isSoft && origPos === ps.position) {
// if the first parser failed softly then we propagate a soft failure.
return;
}
else if (ps.isSoft) {
ps.kind = result_1.ResultKind.HardFail;
// if a i > 0 parser failed softly, this is a hard fail for us.
// also, propagate the internal expectation.
return;
}
else {
// ps failed hard or fatally. The same severity.
return;
}
}
ps.value = results;
ps.kind = result_1.ResultKind.Ok;
}
}
function then(...parsers) {
const resolvedParsers = parsers.map(wrap_implicit_1.wrapImplicit);
return source => {
return new Then((0, wrap_implicit_1.wrapImplicit)(source), resolvedParsers);
};
}
exports.then = then;
//# sourceMappingURL=then.js.map