UNPKG

parjs

Version:

A parser-combinator library for JavaScript.

46 lines (45 loc) 1.5 kB
"use strict"; /** * @module parjs/combinators */ /** */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const combinator_1 = require("./combinator"); const parser_1 = require("../parser"); const defaults_1 = __importDefault(require("lodash/defaults")); const defaultFailure = { reason: "succeeded without capturing input", kind: "Hard" }; /** * Applies the source parser and makes sure it captured some input. * @param pFailure The failure info. */ function mustCapture(pFailure) { let failure = defaults_1.default(pFailure, defaultFailure); return combinator_1.defineCombinator(source => { return new class MustCapture extends parser_1.ParjserBase { constructor() { super(...arguments); this.expecting = `expecting internal parser ${source.type} to consume input`; this.type = "mustCapture"; } _apply(ps) { let { position } = ps; source.apply(ps); if (!ps.isOk) { return; } if (position === ps.position) { ps.kind = failure.kind; ps.reason = failure.reason; } } }(); }); } exports.mustCapture = mustCapture; //# sourceMappingURL=must-capture.js.map