UNPKG

parjs

Version:

A parser-combinator library for JavaScript.

43 lines (42 loc) 1.51 kB
"use strict"; /** * @module parjs */ /** */ Object.defineProperty(exports, "__esModule", { value: true }); const result_1 = require("../result"); const parser_1 = require("../parser"); /** * 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. */ function anyStringOf(...strs) { return new class StringOf extends parser_1.ParjserBase { constructor() { super(...arguments); this.type = "anyStringOf"; this.expecting = `expecting any of ${strs.map(x => `'${x}'`).join(", ")}`; } _apply(ps) { let { position, input } = ps; for (let i = 0; i < strs.length; i++) { let curStr = strs[i]; if (input.length - position < curStr.length) continue; let 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; } }(); } exports.anyStringOf = anyStringOf; //# sourceMappingURL=string-of.js.map