UNPKG

rawsql-ts

Version:

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

38 lines 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LimitClauseParser = void 0; const Clause_1 = require("../models/Clause"); const SqlTokenizer_1 = require("./SqlTokenizer"); const ValueParser_1 = require("./ValueParser"); class LimitClauseParser { // Parse SQL string to AST (was: parse) 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 LIMIT clause is complete but there are additional tokens.`); } return result.value; } // Parse from lexeme array (was: parse) static parseFromLexeme(lexemes, index) { let idx = index; if (lexemes[idx].value !== 'limit') { throw new Error(`Syntax error at position ${idx}: Expected 'LIMIT' keyword but found "${lexemes[idx].value}". LIMIT clauses must start with the LIMIT keyword.`); } idx++; if (idx >= lexemes.length) { throw new Error(`Syntax error: Unexpected end of input after 'LIMIT' keyword. The LIMIT clause requires a numeric expression.`); } // Parse LIMIT value const limitItem = ValueParser_1.ValueParser.parseFromLexeme(lexemes, idx); idx = limitItem.newIndex; const clause = new Clause_1.LimitClause(limitItem.value); return { value: clause, newIndex: idx }; } } exports.LimitClauseParser = LimitClauseParser; //# sourceMappingURL=LimitClauseParser.js.map