-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.py
More file actions
145 lines (125 loc) · 4.88 KB
/
Copy pathTreeNode.py
File metadata and controls
145 lines (125 loc) · 4.88 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
#Jonathan Ke
#11/19/2019
#Output parse node and tree structures, translating helper structures
#java parse tree object class
class TreeNode:
def __init__(self, name):
self.name = name
self.children = []
def addChild(self, child):
self.children.append(child)
def giveSelfToChildren(self):
for child in self.children:
child.parent = self
#returns pathway of first appearance of token or None
#the last value of pathway is the token
def findToken(self, token, pathway):
for i in range(len(self.children)):
child = self.children[i]
returned = self.checkEqualTokens(child, token, pathway)
if returned != None:
return returned
return None #token not found, return to parent
def checkEqualTokens(self, child, token, pathway):
if (isinstance(token, NodeCapturer)
and isinstance(child,TreeNode)
and token.name == child.name):
token.capture(child)
pathway.append(self)
pathway.append(child)
return pathway
elif isinstance(child,TreeNode): #nonterminal path
path = child.findToken(token, pathway+[self])
if path != None:
return path
elif isinstance(token,Capturer) and isinstance(child,token.tokenType):
token.capture(child.string)
pathway.append(self)
pathway.append(child)
return pathway
elif isinstance(child, Token) and child.string == token:
#check if the child's value exactly matches token input
pathway.append(self)
pathway.append(child)
return pathway
return None
def compareToStatement(self, tokenList):
for token in tokenList:
if self.findToken(token, []) == None:
return False
return True
def getStatementPaths(self, tokenList):
firstPath = self.findToken(tokenList[0],[])
if firstPath == None: #statement not found
return None
pathways = [firstPath]
for token in tokenList[1:]:
print('token =',token)
potentialPath = self.findNearestTokenPath(token,pathways[-1])
if potentialPath == None:
return None
pathways.append(potentialPath)
return pathways
#checks if the subsequent token to the current token path
#is parameter token
def findNextToken(self, token, currentPath):
child = currentPath[-1]
parent = currentPath[-2]
if len(parent.children.index(child)) == 1:
pass
indexOfChild = parent.children.index(child)
def __repr__(self):
return self.name
#special object used to extract certain token types from tree
class Capturer(object):
def __init__(self, tokenType):
self.tokenType = tokenType
def capture(self, value):
print(value)
if value == 'true':
value = 'True'
elif value == 'false':
value = 'False'
elif value == 'null':
value = 'None'
elif value == '&&':
value = 'and'
elif value == '||':
value = 'or'
self.value = value
#used to extract entire subtrees from current tree
class NodeCapturer(object):
def __init__(self,name, changeName = None):
self.name = name
self.tokenType = TreeNode
self.changeName = changeName
def capture(self, node):
self.node = node
if self.changeName != None:
self.node.name = self.changeName
def __repr__(self):
return f'Node Capturer<{self.name}>'
#used to extract a subtree and specify where it will move up the current tree
class NodeMover(NodeCapturer):
def __init__(self, name, desiredParent, childAhead = None,
childBehind = None, changeName = None):
super().__init__(name, changeName = changeName)
self.parentDestination = desiredParent
self.childAhead = childAhead
self.childBehind = childBehind
def moveToNewParent(self):
parent = self.node.parent
while parent.name != self.parentDestination:
parent = parent.parent
children = parent.children
if self.childAhead == None and self.childBehind == None:
children.append(self.node)
else:
for i in range(len(children)):
if not isinstance(children[i],str):
if children[i].name == self.childAhead:
children.insert(i+1,self.node)
return
elif children[i].name == self.childBehind:
children.insert(i-1,self.node)
return