rawsql-ts
Version:
High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.
49 lines • 1.72 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, a Map-backed trie is retained while keeping terminal metadata on the node itself.
export 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 KeywordMatchResult.NotAKeyword;
}
this.currentNode = nextNode;
if (nextNode.isFinal) {
return nextNode.children.size === 0
? KeywordMatchResult.Final
: KeywordMatchResult.PartialOrFinal;
}
return KeywordMatchResult.PartialOnly;
}
}
//# sourceMappingURL=KeywordTrie.js.map