parjs
Version:
A parser-combinator library for JavaScript.
42 lines (41 loc) • 1.44 kB
JavaScript
;
/**
* @module parjs
*/
/** */
Object.defineProperty(exports, "__esModule", { value: true });
const result_1 = require("../result");
const parser_1 = require("../parser");
/**
* Returns a parser that will try to match the regular expression at the current
* position and yield the result set. If it can't, the parser will fail softly.
* The match must start at the current position. It can't skip any part of the
* input.
* @param origRegexp
*/
function regexp(origRegexp) {
let flags = [origRegexp.ignoreCase && "i", origRegexp.multiline && "m"].filter(x => x).join("");
let regexp = new RegExp(origRegexp.source, `${flags}y`);
this.reason = `expecting input matching /${origRegexp.source}/`;
return new class Regexp extends parser_1.ParjserBase {
constructor() {
super(...arguments);
this.type = "regexp";
this.expecting = `expecting input matching '${regexp.source}'`;
}
_apply(ps) {
let { input, position } = ps;
regexp.lastIndex = position;
let match = regexp.exec(input);
if (!match) {
ps.kind = result_1.ResultKind.SoftFail;
return;
}
ps.position += match[0].length;
ps.value = match.slice();
ps.kind = result_1.ResultKind.Ok;
}
}();
}
exports.regexp = regexp;
//# sourceMappingURL=regexp.js.map