rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
53 lines • 1.92 kB
JavaScript
;
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, a Map-backed trie is retained while keeping terminal metadata on the node itself.
class KeywordTrie {
constructor(keywords) {
this.root = this.createNode();
// Build the keyword trie once so parse-time matching only walks existing nodes.
for (let i = 0; i < keywords.length; i++) {
this.addKeyword(keywords[i]);
}
this.currentNode = this.root;
}
createNode() {
return {
children: new Map(),
isFinal: false,
};
}
addKeyword(keyword) {
let node = this.root;
for (let i = 0; i < keyword.length; i++) {
const word = keyword[i];
let nextNode = node.children.get(word);
if (!nextNode) {
nextNode = this.createNode();
node.children.set(word, nextNode);
}
node = nextNode;
}
node.isFinal = true;
}
reset() {
this.currentNode = this.root;
}
pushLexeme(lexeme) {
const nextNode = this.currentNode.children.get(lexeme);
if (!nextNode) {
return KeywordParser_1.KeywordMatchResult.NotAKeyword;
}
this.currentNode = nextNode;
if (nextNode.isFinal) {
return nextNode.children.size === 0
? KeywordParser_1.KeywordMatchResult.Final
: KeywordParser_1.KeywordMatchResult.PartialOrFinal;
}
return KeywordParser_1.KeywordMatchResult.PartialOnly;
}
}
exports.KeywordTrie = KeywordTrie;
//# sourceMappingURL=KeywordTrie.js.map