parjs
Version:
Library for building parsers using combinators.
44 lines • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.anyStringOf = void 0;
const parser_1 = require("../parser");
const result_1 = require("../result");
class StringOf extends parser_1.ParjserBase {
constructor(strs) {
super();
this.strs = strs;
this.type = "anyStringOf";
this.expecting = `expecting any of ${this.strs.map(x => `'${x}'`).join(", ")}`;
}
_apply(ps) {
const { position, input } = ps;
const { strs } = this;
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < strs.length; i++) {
const curStr = strs[i];
if (input.length - position < curStr.length)
continue;
const curSubstr = input.slice(position, position + curStr.length);
if (curSubstr === curStr) {
// this means we did not contiue strLoop so curStr passed our tests
ps.position = position + curStr.length;
ps.value = curStr;
ps.kind = result_1.ResultKind.Ok;
return;
}
}
ps.kind = result_1.ResultKind.SoftFail;
}
}
/**
* Returns a parser that will parse any of the strings in `strs` and yield the text that was parsed.
* If it can't, it will fail softly without consuming input.
*
* @param strs A set of string options to parse. In typescript, you can also use a constant tuple if
* you pass it in using the spread operator (`...`).
*/
function anyStringOf(...strs) {
return new StringOf(strs);
}
exports.anyStringOf = anyStringOf;
//# sourceMappingURL=string-of.js.map