@@ -5,41 +5,21 @@ import http from 'isomorphic-git/http/node';
55import Parser from 'web-tree-sitter' ;
66
77import { promises as fs } from 'fs' ;
8- import { Language , SyntaxNode } from 'web-tree-sitter' ;
8+ import { SyntaxNode } from 'web-tree-sitter' ;
99import { NextRequest , NextResponse } from "next/server" ;
1010import { FalkorDB , Graph } from 'falkordb' ;
1111import { RESPOSITORIES } from './repositories' ;
12+ import Language from '@/lib/languages/language' ;
13+ import Python from '@/lib/languages/python'
1214
1315const GraphOps = require ( './graph_ops' ) ;
1416const LIMITED_MODE = process . env . NEXT_PUBLIC_MODE ?. toLowerCase ( ) === 'limited' ;
1517
1618//-----------------------------------------------------------------------------
1719// Tree-Sitter queries
1820//-----------------------------------------------------------------------------
19-
2021let 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 ;
22+ let language : Language ;
4323
4424// Process Class declaration
4525async function processClassDeclaration
@@ -84,7 +64,7 @@ async function processFunctionDeclaration
8464 const file_components = source_file . split ( "/" ) ;
8565 parent = file_components [ file_components . length - 1 ] ;
8666 } else {
87- let identifier_matches = identifier_query . matches ( parent ) [ 0 ] . captures ;
67+ let identifier_matches = language . identifier_query . matches ( parent ) [ 0 ] . captures ;
8868 const identifierNode = identifier_matches [ 0 ] . node ;
8969 parent = identifierNode . text ;
9070 }
@@ -101,7 +81,7 @@ async function processFunctionDeclaration
10181 if ( child_node . type == 'identifier' ) {
10282 args . push ( child_node . text ) ;
10383 } else {
104- let identifier_matches = identifier_query . matches ( child_node )
84+ let identifier_matches = language . identifier_query . matches ( child_node )
10585 if ( identifier_matches . length == 0 ) {
10686 console . log ( "Investigate!" ) ;
10787 continue ;
@@ -162,15 +142,15 @@ async function processFirstPass
162142 const tree = parser . parse ( src ) ;
163143
164144 // Match all Class definitions
165- let class_matches = class_definition_query . matches ( tree . rootNode ) ;
145+ let class_matches = language . class_definition_query . matches ( tree . rootNode ) ;
166146
167147 // Iterate over each matched Class
168148 for ( let class_match of class_matches ) {
169149 await processClassDeclaration ( source_file , graph , class_match ) ;
170150 }
171151
172152 // Match all function definition within the current class
173- let function_matches = function_definition_query . matches ( tree . rootNode ) ;
153+ let function_matches = language . function_definition_query . matches ( tree . rootNode ) ;
174154 for ( let function_match of function_matches ) {
175155 await processFunctionDeclaration ( source_file , graph , function_match ) ;
176156 }
@@ -193,7 +173,7 @@ async function processSecondPass
193173 const tree = parser . parse ( src ) ;
194174
195175 // Match all Function definitions
196- let function_matches = function_definition_query . matches ( tree . rootNode ) ;
176+ let function_matches = language . function_definition_query . matches ( tree . rootNode ) ;
197177
198178 // Iterate over each matched Function
199179 for ( let function_match of function_matches ) {
@@ -203,14 +183,14 @@ async function processSecondPass
203183 let function_src_end = function_node . endPosition . row ;
204184
205185 // Match all function calls: `f()` within the current function
206- let function_call_matches = function_call_query . matches ( function_node ) ;
186+ let function_call_matches = language . function_call_query . matches ( function_node ) ;
207187 for ( let function_call_match of function_call_matches ) {
208188 await processFunctionCall ( source_file , graph , function_name ,
209189 function_src_start , function_src_end , function_call_match ) ;
210190 }
211191
212192 // Match all function calls: `Obj.foo()` within the current function
213- function_call_matches = function_attr_call_query . matches ( function_node ) ;
193+ function_call_matches = language . function_attr_call_query . matches ( function_node ) ;
214194 for ( let function_call_match of function_call_matches ) {
215195 await processFunctionCall ( source_file , graph , function_name ,
216196 function_src_start , function_src_end , function_call_match ) ;
@@ -267,27 +247,9 @@ async function BuildGraph
267247}
268248
269249async function InitializeTreeSitter ( ) {
270- // Initialize Tree-Sitter parser
271- await Parser . init ( {
272- locateFile ( scriptName : string , scriptDirectory : string ) {
273- return path . join ( process . cwd ( ) , 'app/parsers' , scriptName ) ;
274- } ,
275- } ) ;
276-
277250 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)` ) ;
251+ language = new Python ( ) ;
252+ parser . setLanguage ( language . language ) ;
291253}
292254
293255export async function POST ( request : NextRequest ) {
0 commit comments