UNPKG

rawsql-ts

Version:

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

49 lines 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SourceAliasExpressionParser = void 0; const Clause_1 = require("../models/Clause"); const Lexeme_1 = require("../models/Lexeme"); 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 & Lexeme_1.TokenType.Identifier) || (lexemes[idx].type & Lexeme_1.TokenType.Function))) { // Check for alias const table = lexemes[idx].value; idx++; if (idx < lexemes.length && (lexemes[idx].type & Lexeme_1.TokenType.OpenParen)) { // Check for column alias const columns = []; // Skip the open parenthesis idx++; while (idx < lexemes.length && (lexemes[idx].type & Lexeme_1.TokenType.Identifier)) { columns.push(lexemes[idx].value); idx++; if (idx < lexemes.length && (lexemes[idx].type & Lexeme_1.TokenType.Comma)) { idx++; } else { break; // Exit loop if not a comma } } if (lexemes[idx].type & Lexeme_1.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 Clause_1.SourceAliasExpression(table, columns), newIndex: idx }; } return { value: new Clause_1.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'}".`); } } exports.SourceAliasExpressionParser = SourceAliasExpressionParser; //# sourceMappingURL=SourceAliasExpressionParser.js.map