rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
86 lines (85 loc) • 3.29 kB
TypeScript
import { SelectQuery } from '../models/SelectQuery';
import { ParseAnalysisResult } from '../parsers/SelectQueryParser';
import { Lexeme } from '../models/Lexeme';
import { LineColumn } from './LexemeCursor';
/**
* Options for position-aware parsing
*/
export interface ParseToPositionOptions {
/** Enable error recovery to continue parsing after syntax errors */
errorRecovery?: boolean;
/** Insert missing tokens (e.g., missing FROM keywords) */
insertMissingTokens?: boolean;
/** Parse only up to the specified position */
parseToPosition?: {
line: number;
column: number;
} | number;
/** Maximum number of error recovery attempts */
maxRecoveryAttempts?: number;
}
/**
* Result of position-aware parsing
*/
export interface PositionParseResult extends ParseAnalysisResult {
/** Tokens that were parsed up to the cursor position */
parsedTokens?: Lexeme[];
/** Token immediately before the cursor position */
tokenBeforeCursor?: Lexeme;
/** Whether parsing stopped at the cursor position */
stoppedAtCursor?: boolean;
/** Number of error recovery attempts made */
recoveryAttempts?: number;
/** Partial AST even if parsing failed */
partialAST?: SelectQuery;
}
/**
* Position-aware SQL parser with error recovery for IntelliSense
*
* Extends the standard parser to handle incomplete SQL and provide context
* for IntelliSense scenarios where users are actively typing.
*
* @example
* ```typescript
* // Parse incomplete SQL with error recovery
* const sql = "SELECT user.name FROM users user WHERE user.";
* const result = PositionAwareParser.parseToPosition(sql, sql.length, {
* errorRecovery: true,
* insertMissingTokens: true
* });
*
* console.log(result.tokenBeforeCursor?.value); // "."
* console.log(result.success); // true (with recovery)
* ```
*/
export declare class PositionAwareParser {
/**
* Parse SQL text up to a specific position with error recovery
*
* @param sql - SQL text to parse
* @param cursorPosition - Character position to parse up to (0-based) or line/column
* @param options - Parsing options including error recovery
* @returns Parse result with position-specific information
*/
static parseToPosition(sql: string, cursorPosition: number | LineColumn, options?: ParseToPositionOptions): PositionParseResult;
/**
* Parse current query from multi-query text at cursor position
*
* @param sql - Complete SQL text (may contain multiple statements)
* @param cursorPosition - Cursor position
* @param options - Parsing options
* @returns Parse result for the current query only
*/
static parseCurrentQuery(sql: string, cursorPosition: number | LineColumn, options?: ParseToPositionOptions): PositionParseResult;
private static tryNormalParse;
private static tryErrorRecovery;
private static recoverWithTokenInsertion;
private static recoverWithTruncation;
private static recoverWithCompletion;
private static recoverWithMinimalSQL;
private static getAllTokens;
private static findTokenAtPosition;
private static findTokenBeforePosition;
private static findQueryBoundaries;
private static findQueryAtPosition;
}