Skip to content
# Class: LexemeCursor

Defined in: packages/core/src/utils/LexemeCursor.ts:25

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 ​

typescript
const sql = "SELECT id FROM users WHERE active = true";
const lexeme = LexemeCursor.findLexemeAtPosition(sql, 7); // position at 'id'
console.log(lexeme?.value); // 'id'

Constructors ​

Constructor ​

new LexemeCursor(): LexemeCursor

Returns ​

LexemeCursor

Methods ​

findLexemeAtLineColumn() ​

static findLexemeAtLineColumn(sql, position): Lexeme | null

Defined in: packages/core/src/utils/LexemeCursor.ts:48

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.

Parameters ​

sql ​

string

The SQL string to analyze

position ​

LineColumn

Line and column position (1-based)

Returns ​

Lexeme | null

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'

findLexemeAtPosition() ​

static findLexemeAtPosition(sql, cursorPosition): Lexeme | null

Defined in: packages/core/src/utils/LexemeCursor.ts:73

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.

Parameters ​

sql ​

string

The SQL string to analyze

cursorPosition ​

number

The cursor position (0-based character offset)

Returns ​

Lexeme | null

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'

getAllLexemesWithPosition() ​

static getAllLexemesWithPosition(sql): Lexeme[]

Defined in: packages/core/src/utils/LexemeCursor.ts:107

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.

Parameters ​

sql ​

string

The SQL string to tokenize

Returns ​

Lexeme[]

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}`));

charOffsetToLineColumn() ​

static charOffsetToLineColumn(sql, charOffset): LineColumn | null

Defined in: packages/core/src/utils/LexemeCursor.ts:323

Convert character offset to line and column position.

Parameters ​

sql ​

string

The SQL string

charOffset ​

number

Character offset (0-based)

Returns ​

LineColumn | null

Line and column position (1-based), or null if offset is out of bounds

Released under the MIT License.