UNPKG

rawsql-ts

Version:

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

59 lines 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeTokenReader = void 0; const BaseTokenReader_1 = require("./BaseTokenReader"); const Lexeme_1 = require("../models/Lexeme"); const stringUtils_1 = require("../utils/stringUtils"); const KeywordTrie_1 = require("../models/KeywordTrie"); const KeywordParser_1 = require("../parsers/KeywordParser"); // Use KeywordTrie to identify type names composed of multiple words. const trie = new KeywordTrie_1.KeywordTrie([ // type ["double", "precision"], ["character", "varying"], ["time", "without", "time", "zone"], ["time", "with", "time", "zone"], ["timestamp", "without", "time", "zone"], ["timestamp", "with", "time", "zone"], ]); const typeParser = new KeywordParser_1.KeywordParser(trie); /** * Reads SQL identifier tokens */ class TypeTokenReader extends BaseTokenReader_1.BaseTokenReader { /** * Try to read an identifier token */ tryRead(previous) { if (this.isEndOfInput()) { return null; } // Check for keyword identifiers const keyword = typeParser.parse(this.input, this.position); if (keyword !== null) { this.position = keyword.newPosition; return this.createLexeme(Lexeme_1.TokenType.Type, keyword.keyword); } // check pervious token if (previous === null) { return null; } const result = stringUtils_1.StringUtils.tryReadRegularIdentifier(this.input, this.position); if (!result) { return null; } this.position = result.newPosition; // type cast command if (previous.type & Lexeme_1.TokenType.Command && previous.value === "as") { // If the previous token is the `as` keyword, it could be a type cast or an identifier return this.createLexeme(Lexeme_1.TokenType.Identifier | Lexeme_1.TokenType.Type, result.identifier); } // postgres type conversion if (previous.type & Lexeme_1.TokenType.Operator && previous.value === "::") { return this.createLexeme(Lexeme_1.TokenType.Type, result.identifier); } return null; } } exports.TypeTokenReader = TypeTokenReader; //# sourceMappingURL=TypeTokenReader.js.map