rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
64 lines • 2.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParameterTokenReader = void 0;
const BaseTokenReader_1 = require("./BaseTokenReader");
const Lexeme_1 = require("../models/Lexeme");
const charLookupTable_1 = require("../utils/charLookupTable");
/**
* Reads SQL parameter tokens (@param, :param, $param, ?, ${param})
*/
class ParameterTokenReader extends BaseTokenReader_1.BaseTokenReader {
constructor(input) {
super(input);
}
/**
* Try to read a parameter token
*/
tryRead(previous) {
if (this.isEndOfInput()) {
return null;
}
// parameter with suffix (${param}) - check this first
if (this.canRead(1) && this.input[this.position] === '$' && this.input[this.position + 1] === '{') {
this.position += 2; // Skip ${
const start = this.position;
while (this.canRead() && this.input[this.position] !== '}') {
this.position++;
}
if (this.isEndOfInput()) {
throw new Error(`Unexpected end of input. Expected closing '}' for parameter at position ${start}`);
}
const identifier = this.input.slice(start, this.position);
if (identifier.length === 0) {
throw new Error('Empty parameter name is not allowed: found ${} at position ' + (start - 2));
}
this.position++; // Skip }
return this.createLexeme(Lexeme_1.TokenType.Parameter, '${' + identifier + '}');
}
const char = this.input[this.position];
// named parameter (@param, :param, $param)
if (charLookupTable_1.CharLookupTable.isNamedParameterPrefix(char)) {
// However, do not recognize as a parameter if the next character is an operator symbol
// To avoid postgres `::`
if (this.canRead(1) && charLookupTable_1.CharLookupTable.isOperatorSymbol(this.input[this.position + 1])) {
return null;
}
this.position++;
// Read the identifier part after the prefix
const start = this.position;
while (this.canRead() && !charLookupTable_1.CharLookupTable.isDelimiter(this.input[this.position])) {
this.position++;
}
const identifier = this.input.slice(start, this.position);
return this.createLexeme(Lexeme_1.TokenType.Parameter, char + identifier);
}
// nameless parameter (?)
if (char === '?') {
this.position++;
return this.createLexeme(Lexeme_1.TokenType.Parameter, char);
}
return null;
}
}
exports.ParameterTokenReader = ParameterTokenReader;
//# sourceMappingURL=ParameterTokenReader.js.map