Skip to content

Commit 74f7c86

Browse files
committed
take python out of route.ts
1 parent 9097d85 commit 74f7c86

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

app/api/repo/route.ts

+10-43
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,16 @@ import { Language, SyntaxNode } from 'web-tree-sitter';
99
import { NextRequest, NextResponse } from "next/server";
1010
import { FalkorDB, Graph } from 'falkordb';
1111
import { RESPOSITORIES } from './repositories';
12+
import { Python } from '@/lib/languages/python';
1213

1314
const GraphOps = require('./graph_ops');
1415
const LIMITED_MODE = process.env.NEXT_PUBLIC_MODE?.toLowerCase() === 'limited';
1516

1617
//-----------------------------------------------------------------------------
1718
// Tree-Sitter queries
1819
//-----------------------------------------------------------------------------
19-
2020
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();
4322

4423
// Process Class declaration
4524
async 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

293260
export async function POST(request: NextRequest) {

app/parsers/tree-sitter-java.wasm

435 KB
Binary file not shown.

app/parsers/tree-sitter-rust.wasm

1000 KB
Binary file not shown.
1.33 MB
Binary file not shown.

lib/languages/python.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import path from 'path';
2+
import Parser from 'web-tree-sitter';
3+
import { Language } from 'web-tree-sitter';
4+
5+
const PYTHON_LANG = await Parser.Language.load(path.join(process.cwd(), 'app/parsers/tree-sitter-python.wasm'));
6+
7+
//-----------------------------------------------------------------------------
8+
// Tree-Sitter queries
9+
//-----------------------------------------------------------------------------
10+
export class Python {
11+
12+
public language: Language;
13+
14+
// class definition tree-sitter query
15+
// responsible for matching class definition, in addition to extracting the class name
16+
public class_definition_query: Parser.Query;
17+
18+
// function definition tree-sitter query
19+
// responsible for matching function definition, in addition to extracting the function name
20+
public function_definition_query: Parser.Query;
21+
22+
// function call tree-sitter query
23+
// responsible for matching function calls, in addition to extracting the callee function name
24+
public function_call_query: Parser.Query;
25+
26+
// function call tree-sitter query
27+
// responsible for matching function calls of type self.f()
28+
// in addition to extracting the callee function name
29+
public function_attr_call_query: Parser.Query;
30+
31+
// identifier tree-sitter query
32+
// responsible for matching Identifier nodes
33+
public identifier_query: Parser.Query;
34+
35+
constructor() {
36+
this.language = PYTHON_LANG;
37+
this.class_definition_query = PYTHON_LANG.query(`(class_definition name: (identifier) @class-name) @class-definition`);
38+
this.function_definition_query = PYTHON_LANG.query(`((function_definition name: (identifier) @function-name parameters: (parameters) @parameters) @function-definition)`);
39+
this.function_call_query = PYTHON_LANG.query(`((call function: (identifier) @function-name) @function-call)`);
40+
this.function_attr_call_query = PYTHON_LANG.query(`((call function: (attribute object: (identifier) attribute: (identifier) @function-name )) @function-call)`);
41+
this.identifier_query = PYTHON_LANG.query(`((identifier) @identifier)`);
42+
43+
}
44+
45+
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "es2017",
44
"lib": ["dom", "dom.iterable", "esnext"],
55
"allowJs": true,
66
"skipLibCheck": true,

0 commit comments

Comments
 (0)