UNPKG

parjs

Version:

Library for building parsers using combinators.

73 lines 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.manySepBy = exports.getArrayWithSeparators = void 0; const combinated_1 = require("../combinated"); const issues_1 = require("../issues"); const result_1 = require("../result"); const wrap_implicit_1 = require("../wrap-implicit"); function getArrayWithSeparators(things, separators) { const array = things; array.separators = separators; return array; } exports.getArrayWithSeparators = getArrayWithSeparators; class ManySepBy extends combinated_1.Combinated { constructor(source, _options) { super(source); this._options = _options; this.type = "manySepBy"; this.expecting = this.source.expecting; } _apply(ps) { const results = getArrayWithSeparators([], []); const { source, _options: { delimeter, max = Infinity } } = this; source.apply(ps); if (ps.atLeast(result_1.ResultKind.HardFail)) { return; } else if (ps.isSoft) { ps.value = results; ps.kind = result_1.ResultKind.Ok; return; } let { position } = ps; results.push(ps.value); let i = 1; for (;;) { if (i >= max) break; delimeter.apply(ps); if (ps.isSoft) { break; } else if (ps.atLeast(result_1.ResultKind.HardFail)) { return; } results.separators.push(ps.value); source.apply(ps); if (ps.isSoft) { break; } else if (ps.atLeast(result_1.ResultKind.HardFail)) { return; } if (max >= Infinity && ps.position === position) { issues_1.Issues.guardAgainstInfiniteLoop("manySepBy"); } results.push(ps.value); position = ps.position; i++; } ps.kind = result_1.ResultKind.Ok; ps.position = position; ps.value = results; } } function manySepBy(implDelimeter, max = Infinity) { const delimeter = (0, wrap_implicit_1.wrapImplicit)(implDelimeter); return (source) => { return new ManySepBy((0, wrap_implicit_1.wrapImplicit)(source), { delimeter, max }); }; } exports.manySepBy = manySepBy; //# sourceMappingURL=many-sep-by.js.map