UNPKG

rawsql-ts

Version:

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

44 lines 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OverExpressionParser = void 0; const Lexeme_1 = require("../models/Lexeme"); const ValueComponent_1 = require("../models/ValueComponent"); const SqlTokenizer_1 = require("./SqlTokenizer"); const WindowExpressionParser_1 = require("./WindowExpressionParser"); class OverExpressionParser { static parse(query) { const tokenizer = new SqlTokenizer_1.SqlTokenizer(query); // Initialize tokenizer const lexemes = tokenizer.readLexmes(); // Get tokens // Parse const result = this.parseFromLexeme(lexemes, 0); // Error if there are remaining tokens if (result.newIndex < lexemes.length) { throw new Error(`Syntax error: Unexpected token "${lexemes[result.newIndex].value}" at position ${result.newIndex}. The OVER expression is complete but there are additional tokens.`); } return result.value; } static parseFromLexeme(lexemes, index) { let idx = index; if (lexemes[idx].value !== 'over') { throw new Error(`Syntax error at position ${idx}: Expected 'OVER' keyword but found "${lexemes[idx].value}". OVER expressions must start with the OVER keyword.`); } idx++; if (idx >= lexemes.length) { throw new Error(`Syntax error: Unexpected end of input after 'OVER' keyword. Expected either a window name or an opening parenthesis '('.`); } if (lexemes[idx].type & Lexeme_1.TokenType.Identifier) { // named window frame const name = lexemes[idx].value; idx++; return { value: new ValueComponent_1.IdentifierString(name), newIndex: idx }; } if (lexemes[idx].type & Lexeme_1.TokenType.OpenParen) { // Delegate processing to WindowFrameExpressionParser const result = WindowExpressionParser_1.WindowExpressionParser.parseFromLexeme(lexemes, idx); return result; } throw new Error(`Syntax error at position ${idx}: Expected a window name or opening parenthesis '(' after OVER keyword, but found "${lexemes[idx].value}".`); } } exports.OverExpressionParser = OverExpressionParser; //# sourceMappingURL=OverExpressionParser.js.map