UNPKG

parjs

Version:

A parser-combinator library for JavaScript.

33 lines (32 loc) 1.03 kB
"use strict"; /** * @module parjs/combinators */ /** */ Object.defineProperty(exports, "__esModule", { value: true }); const result_1 = require("../result"); const combinator_1 = require("./combinator"); const parser_1 = require("../parser"); function maybe(val = undefined) { return combinator_1.defineCombinator(inner => { return new class MaybeCombinator extends parser_1.ParjserBase { constructor() { super(...arguments); this._inner = inner; this.expecting = "expecting anything"; this.type = "maybe"; } _apply(ps) { inner.apply(ps); if (ps.isSoft) { // on soft failure, set the value and result to OK ps.value = val; ps.kind = result_1.ResultKind.Ok; } // on ok/hard/fatal, propagate the result. } }(); }); } exports.maybe = maybe; //# sourceMappingURL=maybe.js.map