parjs
Version:
A parser-combinator library for JavaScript.
46 lines (45 loc) • 1.45 kB
JavaScript
;
/**
* @module parjs
*/
/** */
Object.defineProperty(exports, "__esModule", { value: true });
const result_1 = require("../result");
const parser_1 = require("../parser");
const defaultFailure = {
kind: "Soft",
reason: "expecting a character fulfilling a predicate"
};
/**
* Returns a parser that parses a single character fulfilling `predicate`.
* @param predicate The predicate the character has to fulfill.
*/
function charWhere(predicate) {
return new class CharWhere extends parser_1.ParjserBase {
constructor() {
super(...arguments);
this.type = "charWhere";
this.expecting = "expecting a character matching a predicate";
}
_apply(ps) {
let { position, input } = ps;
if (position >= input.length) {
ps.kind = result_1.ResultKind.SoftFail;
ps.reason = "expecting a character";
return;
}
let curChar = input[position];
let 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;
}
}();
}
exports.charWhere = charWhere;
//# sourceMappingURL=char-where.js.map