rawsql-ts
Version:
[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
50 lines • 1.88 kB
JavaScript
import { KeywordMatchResult } from "../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.
export 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 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 KeywordMatchResult.Final;
}
if (this.hasEndProperty && this.hasMoreProperties) {
return KeywordMatchResult.PartialOrFinal;
}
return KeywordMatchResult.PartialOnly;
}
}
//# sourceMappingURL=KeywordTrie.js.map