UNPKG

parjs

Version:

Library for building parsers using combinators.

42 lines 1.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.charCodeWhere = void 0; const parser_1 = require("../parser"); const result_1 = require("../result"); class CharCodeWhere extends parser_1.ParjserBase { constructor(predicate) { super(); this.predicate = predicate; this.type = "charCodeWhere"; 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.charCodeAt(position); const result = predicate(curChar, ps.userState); if (result !== true) { ps.kind = result.kind || "Soft"; ps.reason = result.reason || "expecting a character matching a predicate"; return; } ps.value = String.fromCharCode(curChar); ps.position++; ps.kind = result_1.ResultKind.Ok; } } /** * Returns a parser that parses one character, and checks its code fulfills `predicate`. * * @param predicate */ function charCodeWhere(predicate) { return new CharCodeWhere(predicate); } exports.charCodeWhere = charCodeWhere; //# sourceMappingURL=char-code-where.js.map