parjs
Version:
Library for building parsers using combinators.
45 lines • 1.57 kB
JavaScript
;
/** Copyright © 2024, Oracle and/or its affiliates. */
/** @module parjs */
Object.defineProperty(exports, "__esModule", { value: true });
exports.caseString = void 0;
const parser_1 = require("../parser");
const result_1 = require("../result");
class CaseInsensitiveString extends parser_1.ParjserBase {
constructor(str) {
super();
this.str = str;
this.expecting = `expecting '${this.str}' (case insensitive)`;
this.type = "string";
}
_apply(ps) {
const { position, input } = ps;
const { str } = this;
if (position + str.length > input.length) {
ps.kind = result_1.ResultKind.SoftFail;
return;
}
// This should create a StringSlice object instead of actually
// copying a whole string.
const substr = input.slice(position, position + str.length);
// Equality test is very very fast.
if (substr.toLowerCase() !== str.toLowerCase()) {
ps.kind = result_1.ResultKind.SoftFail;
return;
}
ps.position += str.length;
ps.value = substr;
ps.kind = result_1.ResultKind.Ok;
}
}
/**
* Returns a parser that will parse the string `str` insensitive to its case and yield the text that
* was parsed. If it can't, it will fail softly without consuming input.
*
* @param str The string to parse case insensitively.
*/
function caseString(str) {
return new CaseInsensitiveString(str);
}
exports.caseString = caseString;
//# sourceMappingURL=case-string.js.map