UNPKG

tell-me-when

Version:
55 lines (52 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParseState = void 0; exports.toRegExp = toRegExp; class ParseState { index = 0; start; end; flags; constructor(input, { start = 0, end = input.length, flags = 'g' } = {}) { this.input = input; this.start = start; this.end = end; this.flags = flags; } get done() { return this.index >= this.end; } /** * If pattern matches the input at the current index, returns the match, * but doesn't advance the index. */ peek(pattern) { if (typeof pattern === 'string') pattern = toRegExp(pattern); pattern = new RegExp(pattern.source, `${this.flags}${pattern.flags.replace(new RegExp(`[${this.flags}]`, 'g'), '')}`); pattern.lastIndex = this.index; const match = pattern.exec(this.input); return (match === null || match === void 0 ? void 0 : match.index) === this.index && match.index + match[0].length <= this.end ? match : undefined; } /** * If pattern matches the input at the current index, returns the match, * and advances the index to the end of the match. */ match(pattern) { const match = this.peek(pattern); if (match) { this.index += match[0].length; } return match; } } exports.ParseState = ParseState; function toRegExp(s, flags = '') { return new RegExp(s.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&'), flags); } //# sourceMappingURL=ParseState.js.map