@@ -9,37 +9,16 @@ import { Language, SyntaxNode } from 'web-tree-sitter';
99import { NextRequest , NextResponse } from "next/server" ;
1010import { FalkorDB , Graph } from 'falkordb' ;
1111import { RESPOSITORIES } from './repositories' ;
12+ import { Python } from '@/lib/languages/python' ;
1213
1314const GraphOps = require ( './graph_ops' ) ;
1415const LIMITED_MODE = process . env . NEXT_PUBLIC_MODE ?. toLowerCase ( ) === 'limited' ;
1516
1617//-----------------------------------------------------------------------------
1718// Tree-Sitter queries
1819//-----------------------------------------------------------------------------
19-
2020let parser : Parser ;
21- let Python : Language ;
22-
23- // class definition tree-sitter query
24- // responsible for matching class definition, in addition to extracting the class name
25- let class_definition_query : Parser . Query ;
26-
27- // function definition tree-sitter query
28- // responsible for matching function definition, in addition to extracting the function name
29- let function_definition_query : Parser . Query ;
30-
31- // function call tree-sitter query
32- // responsible for matching function calls, in addition to extracting the callee function name
33- let function_call_query : Parser . Query ;
34-
35- // function call tree-sitter query
36- // responsible for matching function calls of type self.f()
37- // in addition to extracting the callee function name
38- let function_attr_call_query : Parser . Query ;
39-
40- // identifier tree-sitter query
41- // responsible for matching Identifier nodes
42- let identifier_query : Parser . Query ;
21+ let language : Python = new Python ( ) ;
4322
4423// Process Class declaration
4524async function processClassDeclaration
@@ -84,7 +63,7 @@ async function processFunctionDeclaration
8463 const file_components = source_file . split ( "/" ) ;
8564 parent = file_components [ file_components . length - 1 ] ;
8665 } else {
87- let identifier_matches = identifier_query . matches ( parent ) [ 0 ] . captures ;
66+ let identifier_matches = language . identifier_query . matches ( parent ) [ 0 ] . captures ;
8867 const identifierNode = identifier_matches [ 0 ] . node ;
8968 parent = identifierNode . text ;
9069 }
@@ -101,7 +80,7 @@ async function processFunctionDeclaration
10180 if ( child_node . type == 'identifier' ) {
10281 args . push ( child_node . text ) ;
10382 } else {
104- let identifier_matches = identifier_query . matches ( child_node )
83+ let identifier_matches = language . identifier_query . matches ( child_node )
10584 if ( identifier_matches . length == 0 ) {
10685 console . log ( "Investigate!" ) ;
10786 continue ;
@@ -162,15 +141,15 @@ async function processFirstPass
162141 const tree = parser . parse ( src ) ;
163142
164143 // Match all Class definitions
165- let class_matches = class_definition_query . matches ( tree . rootNode ) ;
144+ let class_matches = language . class_definition_query . matches ( tree . rootNode ) ;
166145
167146 // Iterate over each matched Class
168147 for ( let class_match of class_matches ) {
169148 await processClassDeclaration ( source_file , graph , class_match ) ;
170149 }
171150
172151 // Match all function definition within the current class
173- let function_matches = function_definition_query . matches ( tree . rootNode ) ;
152+ let function_matches = language . function_definition_query . matches ( tree . rootNode ) ;
174153 for ( let function_match of function_matches ) {
175154 await processFunctionDeclaration ( source_file , graph , function_match ) ;
176155 }
@@ -193,7 +172,7 @@ async function processSecondPass
193172 const tree = parser . parse ( src ) ;
194173
195174 // Match all Function definitions
196- let function_matches = function_definition_query . matches ( tree . rootNode ) ;
175+ let function_matches = language . function_definition_query . matches ( tree . rootNode ) ;
197176
198177 // Iterate over each matched Function
199178 for ( let function_match of function_matches ) {
@@ -203,14 +182,14 @@ async function processSecondPass
203182 let function_src_end = function_node . endPosition . row ;
204183
205184 // Match all function calls: `f()` within the current function
206- let function_call_matches = function_call_query . matches ( function_node ) ;
185+ let function_call_matches = language . function_call_query . matches ( function_node ) ;
207186 for ( let function_call_match of function_call_matches ) {
208187 await processFunctionCall ( source_file , graph , function_name ,
209188 function_src_start , function_src_end , function_call_match ) ;
210189 }
211190
212191 // Match all function calls: `Obj.foo()` within the current function
213- function_call_matches = function_attr_call_query . matches ( function_node ) ;
192+ function_call_matches = language . function_attr_call_query . matches ( function_node ) ;
214193 for ( let function_call_match of function_call_matches ) {
215194 await processFunctionCall ( source_file , graph , function_name ,
216195 function_src_start , function_src_end , function_call_match ) ;
@@ -275,19 +254,7 @@ async function InitializeTreeSitter() {
275254 } ) ;
276255
277256 parser = new Parser ( ) ;
278- Python = await Parser . Language . load ( path . join ( process . cwd ( ) , 'app/parsers/tree-sitter-python.wasm' ) ) ;
279-
280- parser . setLanguage ( Python ) ;
281-
282- //-------------------------------------------------------------------------
283- // Tree-Sitter AST queries
284- //-------------------------------------------------------------------------
285-
286- identifier_query = Python . query ( `((identifier) @identifier)` ) ;
287- function_call_query = Python . query ( `((call function: (identifier) @function-name) @function-call)` ) ;
288- function_attr_call_query = Python . query ( `((call function: (attribute object: (identifier) attribute: (identifier) @function-name )) @function-call)` ) ;
289- class_definition_query = Python . query ( `(class_definition name: (identifier) @class-name) @class-definition` ) ;
290- function_definition_query = Python . query ( `((function_definition name: (identifier) @function-name parameters: (parameters) @parameters) @function-definition)` ) ;
257+ parser . setLanguage ( language . language ) ;
291258}
292259
293260export async function POST ( request : NextRequest ) {
0 commit comments