rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
54 lines • 2.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeywordTrie = void 0;
const KeywordParser_1 = require("../parsers/KeywordParser");
// Note: An object-based trie (string-keyed object) was tested, but benchmark results showed no improvement and sometimes worse performance for long queries.
// Therefore, the original Map-based implementation is retained for best stability and speed.
class KeywordTrie {
constructor(keywords) {
this.root = new Map();
// cache properties
this.hasEndProperty = false;
this.hasMoreProperties = false;
// initialize root node
for (const keyword of keywords) {
this.addKeyword(keyword);
}
// set current node to root
this.currentNode = this.root;
}
addKeyword(keyword) {
let node = this.root;
for (const word of keyword) {
if (!node.has(word)) {
node.set(word, new Map());
}
node = node.get(word);
}
node.set("__end__", true);
}
reset() {
this.currentNode = this.root;
this.hasEndProperty = false;
this.hasMoreProperties = false;
}
pushLexeme(lexeme) {
if (!this.currentNode.has(lexeme)) {
return KeywordParser_1.KeywordMatchResult.NotAKeyword;
}
// move to next node
this.currentNode = this.currentNode.get(lexeme);
// Cache property checks to avoid repeated operations
this.hasEndProperty = this.currentNode.has("__end__");
this.hasMoreProperties = this.currentNode.size > (this.hasEndProperty ? 1 : 0);
if (this.hasEndProperty && !this.hasMoreProperties) {
return KeywordParser_1.KeywordMatchResult.Final;
}
if (this.hasEndProperty && this.hasMoreProperties) {
return KeywordParser_1.KeywordMatchResult.PartialOrFinal;
}
return KeywordParser_1.KeywordMatchResult.PartialOnly;
}
}
exports.KeywordTrie = KeywordTrie;
//# sourceMappingURL=KeywordTrie.js.map