MCPcopy Create free account
hub / github.com/sourcebot-dev/sourcebot / transformTreeToIR

Function transformTreeToIR

packages/web/src/features/search/parser.ts:138–434  ·  view source on GitHub ↗
({
    tree,
    input,
    isCaseSensitivityEnabled,
    isRegexEnabled,
    onExpandSearchContext,
}: {
    tree: Tree;
    input: string;
    isCaseSensitivityEnabled: boolean;
    isRegexEnabled: boolean;
    onExpandSearchContext: (contextName: string) => Promise<string[]>;
})

Source from the content-addressed store, hash-verified

136 * Given a Lezer tree, transforms it into the query intermediate representation.
137 */
138const transformTreeToIR = async ({
139 tree,
140 input,
141 isCaseSensitivityEnabled,
142 isRegexEnabled,
143 onExpandSearchContext,
144}: {
145 tree: Tree;
146 input: string;
147 isCaseSensitivityEnabled: boolean;
148 isRegexEnabled: boolean;
149 onExpandSearchContext: (contextName: string) => Promise<string[]>;
150}): Promise<QueryIR> => {
151 const transformNode = async (node: SyntaxNode): Promise<QueryIR> => {
152 switch (node.type.id) {
153 case Program: {
154 // Program wraps the actual query - transform its child
155 const child = node.firstChild;
156 if (!child) {
157 // Empty query - match nothing
158 return { const: false, query: "const" };
159 }
160 return transformNode(child);
161 }
162 case AndExpr:
163 return {
164 and: {
165 children: await Promise.all(getChildren(node).map(c => transformNode(c)))
166 },
167 query: "and"
168 }
169
170 case OrExpr:
171 return {
172 or: {
173 children: await Promise.all(getChildren(node).map(c => transformNode(c)))
174 },
175 query: "or"
176 };
177
178 case NegateExpr: {
179 // Find the child after the negate token
180 const negateChild = node.getChild("PrefixExpr") || node.getChild("ParenExpr");
181 if (!negateChild) {
182 throw new Error("NegateExpr missing child");
183 }
184 return {
185 not: {
186 child: await transformNode(negateChild)
187 },
188 query: "not"
189 };
190 }
191 case ParenExpr: {
192 // Parentheses just group - transform the inner query
193 const innerQuery = node.getChild("query") || node.firstChild;
194 if (!innerQuery) {
195 return { const: false, query: "const" };

Callers 1

parseQuerySyntaxIntoIRFunction · 0.85

Calls 1

transformNodeFunction · 0.85

Tested by

no test coverage detected