UNPKG

rawsql-ts

Version:

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

48 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LiteralParser = void 0; const ValueComponent_1 = require("../models/ValueComponent"); const LiteralTokenReader_1 = require("../tokenReaders/LiteralTokenReader"); class LiteralParser { static parseFromLexeme(lexemes, index) { // Process literal value let idx = index; const valueText = lexemes[idx].value; let parsedValue; const lex = LiteralTokenReader_1.literalKeywordParser.parse(valueText.toLowerCase(), 0); if (lex) { const value = new ValueComponent_1.RawString(lex.keyword); idx++; return { value, newIndex: idx }; } // Check if it is a number if (/^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(valueText)) { parsedValue = Number(valueText); idx++; const value = new ValueComponent_1.LiteralValue(parsedValue); return { value, newIndex: idx }; } // Otherwise, treat it as a string else { // Check if it's a dollar-quoted string or regular quoted string if (/^\$[^$]*\$[\s\S]*\$[^$]*\$$/.test(valueText)) { // For dollar-quoted strings, store the entire string including tags parsedValue = valueText; } else if (valueText.startsWith("'") && valueText.endsWith("'")) { // Remove single quotes if enclosed parsedValue = valueText.slice(1, -1); } else { parsedValue = valueText; } // Check if it was originally a quoted string literal const isStringLiteral = valueText.startsWith("'") && valueText.endsWith("'"); idx++; const value = new ValueComponent_1.LiteralValue(parsedValue, undefined, isStringLiteral); return { value, newIndex: idx }; } } } exports.LiteralParser = LiteralParser; //# sourceMappingURL=LiteralParser.js.map