parjs
Version:
Library for building parsers using combinators.
34 lines • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringLen = void 0;
const parser_1 = require("../parser");
const result_1 = require("../result");
class StringLen extends parser_1.ParjserBase {
constructor(length) {
super();
this.length = length;
this.type = "stringLen";
this.expecting = `expecting ${this.length} characters`;
}
_apply(ps) {
const { position, input } = ps;
const length = this.length;
if (input.length < position + length) {
ps.kind = result_1.ResultKind.SoftFail;
return;
}
ps.position += length;
ps.value = input.substring(position, position + length);
ps.kind = result_1.ResultKind.Ok;
}
}
/**
* Returns a parser that parses exactly `length` characters and yields the text that was parsed.
*
* @param length The number of characters to parse.
*/
function stringLen(length) {
return new StringLen(length);
}
exports.stringLen = stringLen;
//# sourceMappingURL=string-len.js.map