-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.py
More file actions
114 lines (99 loc) · 3.69 KB
/
AST.py
File metadata and controls
114 lines (99 loc) · 3.69 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
import json
from Model import ASTNode
from diffOperate import DiffOperate
class AST(object):
def constructTreeByJSON(self, subASTTree):
'''
通过JSON构建树型结构的AST
递归
'''
children = []
if subASTTree["children"] != []:
for i in subASTTree["children"]:
children.append(self.constructTreeByJSON(i))
subASTTree["id"] = self.id
self.id = self.id + 1
# print(subASTTree)
ASTNodeObj = ASTNode(subASTTree, children)
self.ASTNodeList.append(ASTNodeObj)
return ASTNodeObj
def addNodeParent(self,subTree,parent):
'''
递归的给所有节点添加父节点的引用
'''
subTree.parent = parent
for i in subTree.children:
self.addNodeParent(i, subTree)
def __init__(self,ASTJSON):
# with open(code, 'r') as f:
# self.CODE = f.read()
self.ASTNodeList = []
AST = json.loads(ASTJSON)
self.id = 0
# print(AST["root"])
self.constructTreeByJSON(AST["root"])
self.addNodeParent(self.ASTNodeList[-1], None)
# print(self.ASTNodeList[9].id)
def astToJson(self, i=-1):
'''
将AST转换回json格式
'''
def astToDict(node):
tempDict = {"type":str(node.type), "typeLabel":node.typeLabel, "pos":str(node.pos), "length": str(node.length)}
if node.label != None:
tempDict["label"]=node.label
childrenList = []
# print(node.children)
for j in node.children:
# print("fuck")
childrenList.append(astToDict(j))
tempDict["children"] = childrenList
return tempDict
return json.dumps(astToDict(self.ASTNodeList[i]))
def getNodeByID(self, nodeID) -> ASTNode:
return self.ASTNodeList[nodeID]
def getHeadNode(self):
return self.ASTNodeList[-1]
def insertNode(self, parentID, index, node):
parentNode = self.getNodeByID(parentID)
parentNode.children.insert(index, node)
node.parent = parentNode
if __name__ == "__main__":
astBefore = AST("ASTbefore.json")
astAfter = AST("ASTafter.json")
diff = DiffOperate("diffscript.txt")
changedNodeID = set() #子节点变化的Node的ID,before树中
changedNodeID = changedNodeID | set(diff.getDeleteBeforeIDs())
tempSet = set()
for i in diff.getInsertedIDs():
if diff.getMatchedBeforeID(i) != -1:
tempSet.add(diff.getMatchedBeforeID(i))
changedNodeID = changedNodeID | tempSet
changedNodeID = changedNodeID | {astBefore.ASTNodeList[i].parent.id for i in diff.getMovedBeforeIDs()}
tempSet = set()
for i in diff.getMovedAfterIDs():
if diff.getMatchedBeforeID(i) != -1:
tempSet.add(diff.getMatchedBeforeID(i))
changedNodeID = changedNodeID | tempSet
changedNodeID = list(changedNodeID)
def getCommonParent(changedNodeID):
while (len(changedNodeID) > 0):
a = astBefore.ASTNodeList[changedNodeID.pop()]
b = astBefore.ASTNodeList[changedNodeID.pop()]
c = a
print(changedNodeID)
while(b != None):
a = c
while(a != None):
print(a.id, b.id)
if a is b:
changedNodeID.append(a.id)
return getCommonParent(changedNodeID)
b = b.parent
a = a.parent
print(a)
else:
return changedNodeID
commonParentID = getCommonParent(changedNodeID)
print(commonParentID)
print(astBefore.astToJson(commonParentID))