Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit e8698f7

Browse files
committed
update PythonASTVisitor
1 parent b871233 commit e8698f7

File tree

1 file changed

+86
-118
lines changed

1 file changed

+86
-118
lines changed

src/main/java/ca/dal/treefactor/util/PythonASTVisitor.java

Lines changed: 86 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public void visit(ASTUtil.ASTNode node) {
3030
processModule(node);
3131
break;
3232
case "class_definition":
33+
case "decorated_definition":
3334
processClass(node);
3435
break;
3536
case "function_definition":
3637
processMethod(node);
37-
break;
38+
// Don't visit children here - method handles its own body
39+
return;
3840
case "assignment":
3941
processField(node);
4042
break;
@@ -61,10 +63,17 @@ protected void processModule(ASTUtil.ASTNode node) {
6163

6264
@Override
6365
protected void processClass(ASTUtil.ASTNode node) {
64-
System.out.println("in processClass()");
65-
String className = extractClassName(node);
66+
System.out.println("Processing class node");
67+
68+
ASTUtil.ASTNode classNode = node.type.equals("decorated_definition") ?
69+
findChildByType(node, "class_definition") : node;
70+
if (classNode == null) return;
71+
72+
String className = extractClassName(classNode);
6673
if (className == null) return;
6774

75+
System.out.println("Found class: " + className);
76+
6877
LocationInfo locationInfo = new LocationInfo(
6978
filePath,
7079
node.startPoint,
@@ -75,41 +84,40 @@ protected void processClass(ASTUtil.ASTNode node) {
7584
UMLClass previousClass = currentClass;
7685
currentClass = new UMLClass(extractModuleName(filePath), className, locationInfo);
7786

78-
// Process decorators
79-
List<ASTUtil.ASTNode> decorators = findDecorators(node);
80-
if (!decorators.isEmpty()) {
81-
System.out.println("Found " + decorators.size() + " decorators");
82-
processDecorators(decorators, currentClass);
87+
// Process decorators if present
88+
if (node.type.equals("decorated_definition")) {
89+
processDecorators(findDecorators(node), currentClass);
8390
}
8491

8592
// Process inheritance
86-
processInheritance(node, currentClass);
93+
processInheritance(classNode, currentClass);
8794

8895
// Process docstring
89-
processDocstring(node, currentClass);
96+
processDocstring(classNode, currentClass);
9097

98+
// Add to scope and process body
9199
currentScope.add(className);
92-
93-
// Process class body
94-
ASTUtil.ASTNode body = findChildByType(node, "block");
100+
ASTUtil.ASTNode body = findChildByType(classNode, "block");
95101
if (body != null) {
96102
for (ASTUtil.ASTNode child : body.children) {
97103
visit(child);
98104
}
99105
}
100-
101106
currentScope.remove(currentScope.size() - 1);
107+
108+
// Finalize class processing
102109
model.addClass(currentClass);
103110
currentClass = previousClass;
111+
112+
System.out.println("Finished processing class: " + className);
104113
}
105114

106115
@Override
107116
protected void processMethod(ASTUtil.ASTNode node) {
108117
String functionName = extractFunctionName(node);
109118
if (functionName == null) return;
110119

111-
// Add method name to scope
112-
currentScope.add("def " + functionName);
120+
System.out.println("Processing method: " + functionName);
113121

114122
LocationInfo locationInfo = new LocationInfo(
115123
filePath,
@@ -118,140 +126,100 @@ protected void processMethod(ASTUtil.ASTNode node) {
118126
CodeElementType.METHOD_DECLARATION
119127
);
120128

121-
122-
// Create operation using builder pattern
123129
UMLOperation.Builder builder = UMLOperation.builder(functionName, locationInfo);
124-
125-
// Set visibility based on name convention
126130
builder.visibility(determineVisibility(functionName));
127131

132+
// Add method to scope before processing body
133+
currentScope.add("def " + functionName);
134+
128135
// Process parameters
129136
processParameters(node, builder);
130137

131-
// Process decorators
132-
processDecorators(findDecorators(node), builder);
133-
134-
// Process docstring
135-
processDocstring(node, builder);
136-
137-
// Process return type annotation if present
138+
// Process return type
138139
processReturnType(node, builder);
139140

140-
// Process function body
141+
// Process body
141142
ASTUtil.ASTNode body = findChildByType(node, "block");
142143
if (body != null) {
143144
builder.body(body.getText(sourceCode));
145+
// Visit body nodes to process attributes
146+
for (ASTUtil.ASTNode child : body.children) {
147+
visit(child);
148+
}
144149
}
145150

146-
// Set special method flags
147-
if (functionName.equals("__init__")) {
148-
builder.setConstructor(true);
149-
}
150-
151+
// Build and add operation
151152
UMLOperation operation = builder.build();
152-
153-
// Add to current class if we're in a class context
154153
if (currentClass != null) {
155154
operation.setClassName(currentClass.getName());
156155
currentClass.addOperation(operation);
157-
System.out.println("Added operation to class: " + currentClass.getName()); // for debug
158-
156+
System.out.println("Added method " + functionName + " to class " + currentClass.getName());
159157
} else {
160-
// This is a standalone function
161158
model.addOperation(operation);
162-
System.out.println("Added operation to model"); // add for debug
163159
}
164160

165-
// Remove method from scope when done
161+
// Remove method from scope
166162
currentScope.remove(currentScope.size() - 1);
167163
}
168164

169-
170165
@Override
171166
protected void processField(ASTUtil.ASTNode node) {
172-
// Skip if not in a class context
173-
if (currentClass == null) return;
167+
System.out.println("Processing potential field node: " + node.type);
174168

175-
// Handle class attributes (direct assignments in class body)
176-
if (node.type.equals("assignment") && !isInMethod()) {
177-
ASTUtil.ASTNode nameNode = findChildByType(node, "left");
178-
if (nameNode == null || !nameNode.type.equals("identifier")) return;
179-
180-
String fieldName = nameNode.getText(sourceCode);
181-
System.out.println("Processing class attribute: " + fieldName);
182-
183-
LocationInfo locationInfo = new LocationInfo(
184-
filePath,
185-
node.startPoint,
186-
node.endPoint,
187-
CodeElementType.FIELD_DECLARATION
188-
);
189-
190-
UMLAttribute attribute = new UMLAttribute(
191-
fieldName,
192-
new UMLType("object"),
193-
locationInfo
194-
);
195-
196-
// Class attributes are static by default
197-
attribute.setStatic(true);
198-
199-
// Set visibility based on name convention
200-
attribute.setVisibility(determineVisibility(fieldName));
201-
202-
// Get initial value if present
203-
ASTUtil.ASTNode valueNode = findChildByType(node, "right");
204-
if (valueNode != null) {
205-
attribute.setInitialValue(valueNode.getText(sourceCode));
206-
}
207-
208-
currentClass.addAttribute(attribute);
209-
System.out.println("Added class attribute: " + fieldName);
169+
// Skip if not in a class context
170+
if (currentClass == null) {
171+
System.out.println("No current class context");
172+
return;
210173
}
211174

212175
// Handle instance attributes (self.attribute assignments in __init__)
213-
if (node.type.equals("assignment") && isInMethod() && isInInitMethod()) {
214-
ASTUtil.ASTNode leftNode = findChildByType(node, "left");
215-
if (leftNode == null || !leftNode.type.equals("attribute")) return;
216-
217-
// Check if it's a self attribute assignment
218-
ASTUtil.ASTNode objectNode = findChildByType(leftNode, "object");
219-
if (objectNode == null || !objectNode.getText(sourceCode).equals("self")) return;
220-
221-
// Get the attribute name
222-
ASTUtil.ASTNode attributeNode = findChildByType(leftNode, "attribute");
223-
if (attributeNode == null) return;
224-
225-
String fieldName = attributeNode.getText(sourceCode);
226-
System.out.println("Processing instance attribute: " + fieldName);
227-
228-
LocationInfo locationInfo = new LocationInfo(
229-
filePath,
230-
node.startPoint,
231-
node.endPoint,
232-
CodeElementType.FIELD_DECLARATION
233-
);
234-
235-
UMLAttribute attribute = new UMLAttribute(
236-
fieldName,
237-
new UMLType("object"),
238-
locationInfo
239-
);
240-
241-
// Instance attributes are not static
242-
attribute.setStatic(false);
243-
244-
// Set visibility based on name convention
245-
attribute.setVisibility(determineVisibility(fieldName));
176+
if (node.type.equals("assignment")) {
177+
// Find left side of assignment
178+
ASTUtil.ASTNode leftNode = findChildByFieldName(node, "left");
179+
System.out.println("Left node type: " + (leftNode != null ? leftNode.type : "null"));
180+
181+
// For instance attributes, the left node should be an "attribute" type
182+
// that has "self" as its object
183+
if (leftNode != null && leftNode.type.equals("attribute")) {
184+
// Get the object part (should be "self")
185+
ASTUtil.ASTNode objectNode = findChildByFieldName(leftNode, "object");
186+
if (objectNode != null && objectNode.getText(sourceCode).equals("self")) {
187+
// Get the attribute name
188+
ASTUtil.ASTNode attributeNode = findChildByFieldName(leftNode, "attribute");
189+
if (attributeNode != null) {
190+
String fieldName = attributeNode.getText(sourceCode);
191+
System.out.println("Found instance attribute: " + fieldName);
192+
193+
LocationInfo locationInfo = new LocationInfo(
194+
filePath,
195+
node.startPoint,
196+
node.endPoint,
197+
CodeElementType.FIELD_DECLARATION
198+
);
199+
200+
UMLAttribute attribute = new UMLAttribute(
201+
fieldName,
202+
new UMLType("object"),
203+
locationInfo
204+
);
205+
206+
// Instance attributes are not static
207+
attribute.setStatic(false);
208+
209+
// Set visibility based on name convention
210+
attribute.setVisibility(determineVisibility(fieldName));
211+
212+
// Get initial value if present
213+
ASTUtil.ASTNode rightNode = findChildByFieldName(node, "right");
214+
if (rightNode != null) {
215+
attribute.setInitialValue(rightNode.getText(sourceCode));
216+
}
246217

247-
// Get initial value if present
248-
ASTUtil.ASTNode valueNode = findChildByType(node, "right");
249-
if (valueNode != null) {
250-
attribute.setInitialValue(valueNode.getText(sourceCode));
218+
currentClass.addAttribute(attribute);
219+
System.out.println("Added instance attribute: " + fieldName);
220+
}
221+
}
251222
}
252-
253-
currentClass.addAttribute(attribute);
254-
System.out.println("Added instance attribute: " + fieldName);
255223
}
256224
}
257225

0 commit comments

Comments
 (0)