parjs
Version:
A parser-combinator library for JavaScript.
46 lines (45 loc) • 1.51 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: "any character fulfilling a predicate"
};
/**
* Returns a parser that parses one character, and checks its code fulfills `predicate`.
* @param predicate
* @param failure
*/
function charCodeWhere(predicate) {
return new class CharCodeWhere extends parser_1.ParjserBase {
constructor() {
super(...arguments);
this.type = "charCodeWhere";
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.charCodeAt(position);
let 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;
}
}();
}
exports.charCodeWhere = charCodeWhere;
//# sourceMappingURL=char-code-where.js.map