UNPKG

rawsql-ts

Version:

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

45 lines 2.37 kB
import { SourceAliasExpression } from "../models/Clause"; import { TokenType } from "../models/Lexeme"; export class SourceAliasExpressionParser { // Parse from lexeme array (was: parse) static parseFromLexeme(lexemes, index) { var _a; let idx = index; // If there is a column alias, it may be detected as a function, so functions are also processed. if (idx < lexemes.length && ((lexemes[idx].type & TokenType.Identifier) || (lexemes[idx].type & TokenType.Function))) { // Check for alias const table = lexemes[idx].value; idx++; if (idx < lexemes.length && (lexemes[idx].type & TokenType.OpenParen)) { // Check for column alias const columns = []; // Skip the open parenthesis idx++; while (idx < lexemes.length && (lexemes[idx].type & TokenType.Identifier)) { columns.push(lexemes[idx].value); idx++; if (idx < lexemes.length && (lexemes[idx].type & TokenType.Comma)) { idx++; } else { break; // Exit loop if not a comma } } if (lexemes[idx].type & TokenType.CloseParen) { // Skip the closing parenthesis idx++; } else { throw new Error(`Syntax error at position ${idx}: Missing closing parenthesis ')' for column alias list. Each opening parenthesis must have a matching closing parenthesis.`); } if (columns.length === 0) { throw new Error(`Syntax error at position ${index}: No column aliases found. Column alias declarations must contain at least one column name.`); } return { value: new SourceAliasExpression(table, columns), newIndex: idx }; } return { value: new SourceAliasExpression(table, null), newIndex: idx }; } throw new Error(`Syntax error at position ${index}: Expected an identifier for table alias but found "${((_a = lexemes[index]) === null || _a === void 0 ? void 0 : _a.value) || 'end of input'}".`); } } //# sourceMappingURL=SourceAliasExpressionParser.js.map