-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathpython-parser.ts
More file actions
174 lines (155 loc) · 4.2 KB
/
Copy pathpython-parser.ts
File metadata and controls
174 lines (155 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { AbstractParser, EnclosingContext } from "../../constants";
import * as Parser from "tree-sitter";
import * as Python from "tree-sitter-python";
interface TreeSitterNode {
startPosition: { row: number; column: number };
endPosition: { row: number; column: number };
type: string;
children: TreeSitterNode[];
}
// Add interface to match expected Node structure
interface LocationData {
line: number;
column: number;
}
interface NodeLocation {
start: LocationData;
end: LocationData;
}
interface ASTNode {
type: string;
loc: NodeLocation;
// Add other required properties from babel Node interface
leadingComments?: any[] | null;
innerComments?: any[] | null;
trailingComments?: any[] | null;
start?: number | null;
end?: number | null;
range?: [number, number];
extra?: Record<string, unknown>;
}
const processNode = (
node: TreeSitterNode,
lineStart: number,
lineEnd: number,
largestSize: number,
largestEnclosingContext: TreeSitterNode | null
) => {
const startLine = node.startPosition.row + 1;
const endLine = node.endPosition.row + 1;
if (startLine <= lineStart && lineEnd <= endLine) {
const size = endLine - startLine;
if (size > largestSize) {
largestSize = size;
largestEnclosingContext = node;
}
}
return { largestSize, largestEnclosingContext };
};
export class PythonParser implements AbstractParser {
private parser: Parser;
constructor() {
this.parser = new Parser();
this.parser.setLanguage(Python);
}
findEnclosingContext(
file: string,
lineStart: number,
lineEnd: number
): EnclosingContext {
try {
const tree = this.parser.parse(file);
let largestEnclosingContext: TreeSitterNode = null;
let largestSize = 0;
const traverseNode = (node: TreeSitterNode) => {
if (
[
"function_definition",
"class_definition",
"if_statement",
"for_statement",
"while_statement",
"try_statement",
"with_statement",
"async_function_definition",
"async_for_statement",
].includes(node.type)
) {
({ largestSize, largestEnclosingContext } = processNode(
node,
lineStart,
lineEnd,
largestSize,
largestEnclosingContext
));
}
if (node.children) {
node.children.forEach(traverseNode);
}
};
traverseNode(tree.rootNode as unknown as TreeSitterNode);
//ok
// Convert TreeSitterNode to expected Node format
const convertedNode: ASTNode | null = largestEnclosingContext
? {
type: largestEnclosingContext.type,
loc: {
start: {
line: largestEnclosingContext.startPosition.row + 1,
column: largestEnclosingContext.startPosition.column,
},
end: {
line: largestEnclosingContext.endPosition.row + 1,
column: largestEnclosingContext.endPosition.column,
},
},
// Add required properties
leadingComments: null,
innerComments: null,
trailingComments: null,
start: null,
end: null,
}
: null;
return {
enclosingContext: convertedNode,
} as EnclosingContext;
} catch (error) {
console.error("Error parsing Python code:", error);
return { enclosingContext: null };
}
}
dryRun(file: string): { valid: boolean; error: string } {
try {
const tree = this.parser.parse(file);
if (this.hasErrors(tree.rootNode)) {
return {
valid: false,
error: "Syntax error in Python code",
};
}
return {
valid: true,
error: "",
};
} catch (exc) {
return {
valid: false,
error: exc.toString(),
};
}
}
private hasErrors(node: any): boolean {
if (node.type === "ERROR") {
return true;
}
if (node.children) {
for (const child of node.children) {
if (this.hasErrors(child)) {
return true;
}
}
}
return false;
}
}