UNPKG

rawsql-ts

Version:

[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

110 lines 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenReaderManager = void 0; /** * Manages and coordinates multiple token readers */ class TokenReaderManager { constructor(input, position = 0) { this.cacheHits = 0; this.cacheMisses = 0; this.input = input; this.position = position; this.readers = []; this.tokenCache = new Map(); } /** * Register a token reader * @param reader The reader to register * @returns This manager instance for chaining */ register(reader) { this.readers.push(reader); return this; } /** * Register multiple token readers * @param readers The readers to register * @returns This manager instance for chaining */ registerAll(readers) { readers.forEach(reader => this.register(reader)); return this; } /** * Update the position for all readers */ setPosition(position) { this.position = position; for (const reader of this.readers) { reader.setPosition(position); } } /** * Try to read a token using all registered readers * @param position The position to read from * @param previous The previous token, if any * @returns The lexeme if a reader could read it, null otherwise */ tryRead(position, previous) { // Check cache - using position as the key if (this.tokenCache.has(position)) { // Cache hit this.cacheHits++; const lexeme = this.tokenCache.get(position) || null; return lexeme; } // Cache miss - create new entry this.cacheMisses++; this.setPosition(position); // Try to read with each reader let lexeme = null; for (const reader of this.readers) { lexeme = reader.tryRead(previous); if (lexeme) { this.position = reader.getPosition(); break; } } // Update all readers' positions for (const reader of this.readers) { reader.setPosition(this.position); } // Save to cache (even if null) this.tokenCache.set(position, lexeme); return lexeme; } /** * Get the maximum position among all readers */ getMaxPosition() { let maxPosition = this.position; for (const reader of this.readers) { const position = reader.getPosition(); if (position > maxPosition) { maxPosition = position; } } return maxPosition; } /** * Get the input string */ getInput() { return this.input; } /** * Get cache statistics */ getCacheStats() { const total = this.cacheHits + this.cacheMisses; const ratio = total > 0 ? this.cacheHits / total : 0; return { hits: this.cacheHits, misses: this.cacheMisses, ratio: ratio }; } } exports.TokenReaderManager = TokenReaderManager; //# sourceMappingURL=TokenReaderManager.js.map