parjs
Version:
Library for building parsers using combinators.
57 lines • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.many1 = void 0;
const combinated_1 = require("../combinated");
const issues_1 = require("../issues");
const result_1 = require("../result");
const wrap_implicit_1 = require("../wrap-implicit");
class Many1 extends combinated_1.Combinated {
constructor(source, _maxIterations) {
super(source);
this._maxIterations = _maxIterations;
this.type = "many1";
this.expecting = this.source.expecting;
}
_apply(ps) {
let { position } = ps;
const arr = [];
let i = 0;
for (;;) {
this.source.apply(ps);
if (!ps.isOk)
break;
if (i >= this._maxIterations)
break;
if (this._maxIterations === Infinity && ps.position === position) {
issues_1.Issues.guardAgainstInfiniteLoop(this.type);
}
position = ps.position;
arr.push(ps.value);
i++;
}
if (ps.atLeast(result_1.ResultKind.HardFail)) {
return;
}
if (i === 0) {
ps.kind = result_1.ResultKind.SoftFail;
ps.reason = "expected at least one match";
return;
}
ps.value = arr;
// recover from the last failure.
ps.position = position;
ps.kind = result_1.ResultKind.Ok;
}
}
/**
* Applies the source parser 1 or more times until it fails softly. Yields all of its results in an
* array.
*
* @param maxIterations Optionally, the maximum number of times to apply the source parser. Defaults
* to `Infinity`.
*/
function many1(maxIterations = Infinity) {
return source => new Many1((0, wrap_implicit_1.wrapImplicit)(source), maxIterations);
}
exports.many1 = many1;
//# sourceMappingURL=many1.js.map