UNPKG

rawsql-ts

Version:

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

86 lines 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseTokenReader = void 0; const Lexeme_1 = require("../models/Lexeme"); const stringUtils_1 = require("../utils/stringUtils"); /** * Base class for token readers */ class BaseTokenReader { constructor(input, position = 0) { this.input = input; this.position = position; } /** * Get the current position in the input */ getPosition() { return this.position; } /** * Set the position in the input */ setPosition(position) { this.position = position; } /** * Check if we've reached the end of input */ isEndOfInput(shift = 0) { return this.position + shift >= this.input.length; } /** * Check if we can read more characters */ canRead(shift = 0) { return !this.isEndOfInput(shift); } /** * Read an expected character */ read(expectChar) { if (this.isEndOfInput()) { throw new Error(`Unexpected character. expect: ${expectChar}, actual: EndOfInput, position: ${this.position}`); } const char = this.input[this.position]; if (char !== expectChar) { throw new Error(`Unexpected character. expect: ${expectChar}, actual: ${char}, position: ${this.position}`); } this.position++; return char; } /** * Create a lexeme with the specified type and value */ createLexeme(type, value, comments = null, startPosition, endPosition) { const lexeme = { type, value: (type === Lexeme_1.TokenType.Command || type === Lexeme_1.TokenType.Operator || type === Lexeme_1.TokenType.Function) ? value.toLowerCase() : value, comments: comments, }; // Add position information if provided if (startPosition !== undefined && endPosition !== undefined) { lexeme.position = { startPosition, endPosition, }; } return lexeme; } /** * Create a lexeme with automatic position tracking */ createLexemeWithPosition(type, value, startPos, comments = null) { return this.createLexeme(type, value, comments, startPos, startPos + value.length); } /** * Get debug info for error reporting */ getDebugPositionInfo(errPosition) { return stringUtils_1.StringUtils.getDebugPositionInfo(this.input, errPosition); } } exports.BaseTokenReader = BaseTokenReader; //# sourceMappingURL=BaseTokenReader.js.map