UNPKG

parjs

Version:

Library for building parsers using combinators.

41 lines 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.exactly = void 0; const combinated_1 = require("../combinated"); const result_1 = require("../result"); const wrap_implicit_1 = require("../wrap-implicit"); class Exactly extends combinated_1.Combinated { constructor(source, count) { super(source); this.count = count; this.type = "exactly"; this.expecting = this.source.expecting; } _apply(ps) { const arr = []; for (let i = 0; i < this.count; i++) { this.source.apply(ps); if (!ps.isOk) { if (ps.kind === result_1.ResultKind.SoftFail && i > 0) { ps.kind = result_1.ResultKind.HardFail; } // fail because the inner parser has failed. return; } arr.push(ps.value); } ps.value = arr; } } /** * Applies the source parser exactly `count` times, and yields all the results in an array. * * @param count The number of times to apply the source parser. */ function exactly(count) { return (source) => { return new Exactly((0, wrap_implicit_1.wrapImplicit)(source), count); }; } exports.exactly = exactly; //# sourceMappingURL=exactly.js.map