rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
130 lines (129 loc) • 4.47 kB
TypeScript
import { Lexeme } from '../models/Lexeme';
/**
* Line and column position (1-based indexing for editor integration)
*/
export interface LineColumn {
line: number;
column: number;
}
/**
* Utility class for cursor-to-lexeme mapping in SQL text.
*
* Provides functionality to find lexemes at specific cursor positions for IDE integration.
* Handles SQL parsing with proper comment and whitespace handling for editor features.
*
* @example Basic usage
* ```typescript
* const sql = "SELECT id FROM users WHERE active = true";
* const lexeme = LexemeCursor.findLexemeAtPosition(sql, 7); // position at 'id'
* console.log(lexeme?.value); // 'id'
* ```
*/
export declare class LexemeCursor {
private static readonly SQL_COMMANDS;
/**
* Find the lexeme at the specified line and column position.
*
* Designed for GUI editor integration where users select alias text.
* Uses 1-based line and column indexing to match editor conventions.
*
* @param sql - The SQL string to analyze
* @param position - Line and column position (1-based)
* @returns The lexeme at the position, or null if not found
*
* @example
* ```typescript
* const sql = "SELECT user_id FROM orders";
* const lexeme = LexemeCursor.findLexemeAtLineColumn(sql, { line: 1, column: 8 });
* console.log(lexeme?.value); // 'user_id'
* ```
*/
static findLexemeAtLineColumn(sql: string, position: LineColumn): Lexeme | null;
/**
* Find the lexeme at the specified cursor position.
*
* Performs intelligent SQL parsing with proper comment and whitespace handling.
* Returns null if cursor is in whitespace or comments.
*
* @param sql - The SQL string to analyze
* @param cursorPosition - The cursor position (0-based character offset)
* @returns The lexeme at the position, or null if not found
*
* @example
* ```typescript
* const sql = "SELECT user_id FROM orders";
* const lexeme = LexemeCursor.findLexemeAtPosition(sql, 7);
* console.log(lexeme?.value); // 'user_id'
* ```
*/
static findLexemeAtPosition(sql: string, cursorPosition: number): Lexeme | null;
/**
* Get all lexemes with position information from SQL text.
*
* Tokenizes the entire SQL string with precise position information.
* Useful for syntax highlighting, code analysis, and editor features.
*
* @param sql - The SQL string to tokenize
* @returns Array of lexemes with position information (excludes comments/whitespace)
*
* @example
* ```typescript
* const sql = "SELECT id FROM users";
* const lexemes = LexemeCursor.getAllLexemesWithPosition(sql);
* lexemes.forEach(l => console.log(`${l.value} at ${l.position.startPosition}`));
* ```
*/
static getAllLexemesWithPosition(sql: string): Lexeme[];
/**
* Skip whitespace and comments, returning new position
*/
private static skipWhitespaceAndComments;
/**
* Parse the next token starting at the given position
*/
private static parseNextToken;
/**
* Parse string literal tokens
*/
private static parseStringLiteral;
/**
* Parse operator tokens
*/
private static parseOperator;
/**
* Parse word tokens (identifiers, commands, functions)
*/
private static parseWordToken;
/**
* Determine the token type for operators
*/
private static getOperatorTokenType;
/**
* Determine the token type for word tokens
*/
private static getWordTokenType;
/**
* Check if token value should be lowercased
*/
private static shouldLowercase;
/**
* Create a lexeme with position information
*/
private static createLexeme;
/**
* Convert line and column position to character offset.
*
* @param sql - The SQL string
* @param position - Line and column position (1-based)
* @returns Character offset (0-based), or -1 if position is out of bounds
*/
private static lineColumnToCharOffset;
/**
* Convert character offset to line and column position.
*
* @param sql - The SQL string
* @param charOffset - Character offset (0-based)
* @returns Line and column position (1-based), or null if offset is out of bounds
*/
static charOffsetToLineColumn(sql: string, charOffset: number): LineColumn | null;
}