parjs
Version:
A parser-combinator library for JavaScript.
72 lines (71 loc) • 2.5 kB
JavaScript
;
/**
* @module parjs/combinators
*/
/** */
Object.defineProperty(exports, "__esModule", { value: true });
const result_1 = require("../result");
const combinator_1 = require("./combinator");
const parser_1 = require("../parser");
const scalar_converter_1 = require("../scalar-converter");
const map_1 = require("./map");
/**
* Applies the source parser followed by `next`. Yields the result of
* `next`.
* @param next
*/
function qthen(next) {
return combinator_1.composeCombinator(then(next), 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 combinator_1.composeCombinator(then(next), map_1.map(arr => arr[0]));
}
exports.thenq = thenq;
function then(...parsers) {
let resolvedParsers = parsers.map(x => scalar_converter_1.ScalarConverter.convert(x));
return combinator_1.defineCombinator(source => {
resolvedParsers.splice(0, 0, source);
return new class Then extends parser_1.ParjserBase {
constructor() {
super(...arguments);
this.type = "then";
this.expecting = source.expecting;
}
_apply(ps) {
let results = [];
let origPos = ps.position;
for (let i = 0; i < resolvedParsers.length; i++) {
let cur = resolvedParsers[i];
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;
}
}();
});
}
exports.then = then;
//# sourceMappingURL=then.js.map