@@ -9,37 +9,16 @@ import { Language, SyntaxNode } from 'web-tree-sitter';
9
9
import { NextRequest , NextResponse } from "next/server" ;
10
10
import { FalkorDB , Graph } from 'falkordb' ;
11
11
import { RESPOSITORIES } from './repositories' ;
12
+ import { Python } from '@/lib/languages/python' ;
12
13
13
14
const GraphOps = require ( './graph_ops' ) ;
14
15
const LIMITED_MODE = process . env . NEXT_PUBLIC_MODE ?. toLowerCase ( ) === 'limited' ;
15
16
16
17
//-----------------------------------------------------------------------------
17
18
// Tree-Sitter queries
18
19
//-----------------------------------------------------------------------------
19
-
20
20
let 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 ( ) ;
43
22
44
23
// Process Class declaration
45
24
async function processClassDeclaration
@@ -84,7 +63,7 @@ async function processFunctionDeclaration
84
63
const file_components = source_file . split ( "/" ) ;
85
64
parent = file_components [ file_components . length - 1 ] ;
86
65
} else {
87
- let identifier_matches = identifier_query . matches ( parent ) [ 0 ] . captures ;
66
+ let identifier_matches = language . identifier_query . matches ( parent ) [ 0 ] . captures ;
88
67
const identifierNode = identifier_matches [ 0 ] . node ;
89
68
parent = identifierNode . text ;
90
69
}
@@ -101,7 +80,7 @@ async function processFunctionDeclaration
101
80
if ( child_node . type == 'identifier' ) {
102
81
args . push ( child_node . text ) ;
103
82
} else {
104
- let identifier_matches = identifier_query . matches ( child_node )
83
+ let identifier_matches = language . identifier_query . matches ( child_node )
105
84
if ( identifier_matches . length == 0 ) {
106
85
console . log ( "Investigate!" ) ;
107
86
continue ;
@@ -162,15 +141,15 @@ async function processFirstPass
162
141
const tree = parser . parse ( src ) ;
163
142
164
143
// 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 ) ;
166
145
167
146
// Iterate over each matched Class
168
147
for ( let class_match of class_matches ) {
169
148
await processClassDeclaration ( source_file , graph , class_match ) ;
170
149
}
171
150
172
151
// 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 ) ;
174
153
for ( let function_match of function_matches ) {
175
154
await processFunctionDeclaration ( source_file , graph , function_match ) ;
176
155
}
@@ -193,7 +172,7 @@ async function processSecondPass
193
172
const tree = parser . parse ( src ) ;
194
173
195
174
// 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 ) ;
197
176
198
177
// Iterate over each matched Function
199
178
for ( let function_match of function_matches ) {
@@ -203,14 +182,14 @@ async function processSecondPass
203
182
let function_src_end = function_node . endPosition . row ;
204
183
205
184
// 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 ) ;
207
186
for ( let function_call_match of function_call_matches ) {
208
187
await processFunctionCall ( source_file , graph , function_name ,
209
188
function_src_start , function_src_end , function_call_match ) ;
210
189
}
211
190
212
191
// 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 ) ;
214
193
for ( let function_call_match of function_call_matches ) {
215
194
await processFunctionCall ( source_file , graph , function_name ,
216
195
function_src_start , function_src_end , function_call_match ) ;
@@ -275,19 +254,7 @@ async function InitializeTreeSitter() {
275
254
} ) ;
276
255
277
256
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 ) ;
291
258
}
292
259
293
260
export async function POST ( request : NextRequest ) {
0 commit comments