parjs
Version:
A parser-combinator library for JavaScript.
49 lines (48 loc) • 1.77 kB
JavaScript
;
/**
* @module parjs/combinators
*/
/** */
Object.defineProperty(exports, "__esModule", { value: true });
const result_1 = require("../result");
const scalar_converter_1 = require("../scalar-converter");
const combinator_1 = require("./combinator");
const parser_1 = require("../parser");
function or(...alts) {
let resolvedAlts = alts.map(x => scalar_converter_1.ScalarConverter.convert(x));
return combinator_1.defineCombinator(source => {
resolvedAlts.splice(0, 0, source);
let altNames = resolvedAlts.map(x => x.type);
return new class Or extends parser_1.ParjserBase {
constructor() {
super(...arguments);
this.type = "or";
this.expecting = `expecting one of: ${altNames.join(", ")}`;
}
_apply(ps) {
let { position } = ps;
for (let i = 0; i < resolvedAlts.length; i++) {
// go over each alternative.
let cur = resolvedAlts[i];
// apply it on the current state.
cur.apply(ps);
if (ps.isOk) {
// if success, return. The PS records the result.
return;
}
else if (ps.isSoft) {
// backtrack to the original position and try again.
ps.position = position;
}
else {
// if failure, return false,
return;
}
}
ps.kind = result_1.ResultKind.SoftFail;
}
}();
});
}
exports.or = or;
//# sourceMappingURL=or.js.map