UNPKG

rawsql-ts

Version:

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

82 lines 2.45 kB
import { TokenType } from '../models/Lexeme'; import { StringUtils } from '../utils/stringUtils'; /** * Base class for token readers */ export 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 === TokenType.Command || type === TokenType.Operator || type === 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.getDebugPositionInfo(this.input, errPosition); } } //# sourceMappingURL=BaseTokenReader.js.map