UNPKG

parjs

Version:

Library for building parsers using combinators.

42 lines 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.charWhere = void 0; const parser_1 = require("../parser"); const result_1 = require("../result"); class CharWhere extends parser_1.ParjserBase { constructor(predicate) { super(); this.predicate = predicate; this.type = "charWhere"; this.expecting = "expecting a character matching a predicate"; } _apply(ps) { const { position, input } = ps; const predicate = this.predicate; if (position >= input.length) { ps.kind = result_1.ResultKind.SoftFail; ps.reason = "expecting a character"; return; } const curChar = input[position]; const result = predicate(curChar, ps.userState); if (result !== true) { ps.reason = result.reason || this.expecting; ps.kind = result.kind || "Soft"; return; } ps.value = curChar; ps.position++; ps.kind = result_1.ResultKind.Ok; } } /** * Returns a parser that parses a single character fulfilling `predicate`. * * @param predicate The predicate the character has to fulfill. */ function charWhere(predicate) { return new CharWhere(predicate); } exports.charWhere = charWhere; //# sourceMappingURL=char-where.js.map