UNPKG

rawsql-ts

Version:

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

108 lines 5.42 kB
import { TokenType } from "../models/Lexeme"; import { ValuesQuery } from "../models/SelectQuery"; import { TupleExpression } from "../models/ValueComponent"; import { SqlTokenizer } from "./SqlTokenizer"; import { ValueParser } from "./ValueParser"; import { extractLexemeComments } from "./utils/LexemeCommentUtils"; export class ValuesQueryParser { static parse(query) { const tokenizer = new 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 VALUES clause is complete but there are additional tokens.`); } return result.value; } static parseFromLexeme(lexemes, index) { let idx = index; if (idx >= lexemes.length) { throw new Error(`Syntax error at position ${idx}: Expected 'VALUES' keyword but input ended early.`); } const valuesLexeme = lexemes[idx]; if (valuesLexeme.value.toLowerCase() !== 'values') { throw new Error(`Syntax error at position ${idx}: Expected 'VALUES' keyword but found "${valuesLexeme.value}". VALUES clauses must start with the VALUES keyword.`); } const valuesComments = extractLexemeComments(valuesLexeme); idx++; if (idx >= lexemes.length) { throw new Error(`Syntax error: Unexpected end of input after 'VALUES' keyword. The VALUES clause requires at least one tuple expression.`); } const tuples = []; const firstTuple = this.parseTuple(lexemes, idx); tuples.push(firstTuple.value); idx = firstTuple.newIndex; if (valuesComments.after.length > 0) { firstTuple.value.addPositionedComments('before', valuesComments.after); } while (idx < lexemes.length && (lexemes[idx].type & TokenType.Comma)) { idx++; const tuple = this.parseTuple(lexemes, idx); tuples.push(tuple.value); idx = tuple.newIndex; } const query = new ValuesQuery(tuples); if (valuesComments.before.length > 0) { query.headerComments = valuesComments.before; } return { value: query, newIndex: idx }; } static parseTuple(lexemes, index) { let idx = index; if (idx >= lexemes.length || lexemes[idx].type !== TokenType.OpenParen) { throw new Error(`Syntax error at position ${idx}: Expected opening parenthesis but found "${idx < lexemes.length ? lexemes[idx].value : "end of input"}". Tuple expressions in VALUES clause must be enclosed in parentheses.`); } const openingComments = extractLexemeComments(lexemes[idx]); idx++; const values = []; if (idx >= lexemes.length) { throw new Error(`Syntax error: Unexpected end of input after opening parenthesis in tuple expression.`); } if (lexemes[idx].type & TokenType.CloseParen) { const tuple = new TupleExpression([]); const closingComments = extractLexemeComments(lexemes[idx]); idx++; if (openingComments.before.length > 0) { tuple.addPositionedComments('before', openingComments.before); } if (closingComments.after.length > 0) { tuple.addPositionedComments('after', closingComments.after); } return { value: tuple, newIndex: idx }; } const firstValue = ValueParser.parseFromLexeme(lexemes, idx); values.push(firstValue.value); idx = firstValue.newIndex; while (idx < lexemes.length && (lexemes[idx].type & TokenType.Comma)) { idx++; if (idx >= lexemes.length) { throw new Error(`Syntax error: Unexpected end of input after comma in tuple expression.`); } const value = ValueParser.parseFromLexeme(lexemes, idx); values.push(value.value); idx = value.newIndex; } if (idx >= lexemes.length || lexemes[idx].type !== TokenType.CloseParen) { throw new Error(`Syntax error at position ${idx}: Expected closing parenthesis but found "${idx < lexemes.length ? lexemes[idx].value : "end of input"}". Tuple expressions in VALUES clause must be enclosed in parentheses.`); } const closingComments = extractLexemeComments(lexemes[idx]); idx++; const tuple = new TupleExpression(values); if (openingComments.before.length > 0) { tuple.addPositionedComments('before', openingComments.before); } if (openingComments.after.length > 0 && values.length > 0) { values[0].addPositionedComments('before', openingComments.after); } if (closingComments.before.length > 0 && values.length > 0) { values[values.length - 1].addPositionedComments('after', closingComments.before); } if (closingComments.after.length > 0) { tuple.addPositionedComments('after', closingComments.after); } return { value: tuple, newIndex: idx }; } } //# sourceMappingURL=ValuesQueryParser.js.map