11#!/usr/bin/python
22# -*- coding: utf-8 -*-
33
4+ from typing import Optional
45
5- import copy
66import collections
77from api .errors import Error
88
@@ -21,11 +21,13 @@ def __str__(self):
2121 return self .msg
2222
2323
24- class Tree ( object ) :
24+ class Tree :
2525 """ Simple tree implementation
2626 """
27- class childrenList (object ):
28- def __init__ (self , node ):
27+ parent : Optional ['Tree' ] = None
28+
29+ class ChildrenList :
30+ def __init__ (self , node : 'Tree' ):
2931 assert isinstance (node , Tree )
3032 self .node = node # Node having this children
3133 self ._children = []
@@ -34,7 +36,7 @@ def __getitem__(self, key):
3436 if isinstance (key , int ):
3537 return self ._children [key ]
3638
37- result = Tree .childrenList (self .node )
39+ result = Tree .ChildrenList (self .node )
3840 for x in self ._children [key ]:
3941 result .append (x )
4042 return result
@@ -68,10 +70,10 @@ def __len__(self):
6870 return len (self ._children )
6971
7072 def __add__ (self , other ):
71- if not isinstance (other , Tree .childrenList ):
73+ if not isinstance (other , Tree .ChildrenList ):
7274 assert isinstance (other , collections .Container )
7375
74- result = Tree .childrenList (self .node )
76+ result = Tree .ChildrenList (self .node )
7577 for x in self :
7678 result .append (x )
7779 for x in other :
@@ -82,19 +84,19 @@ def __repr__(self):
8284 return "%s:%s" % (self .node .__repr__ (), str ([x .__repr__ () for x in self ._children ]))
8385
8486 def __init__ (self ):
85- self ._children = Tree .childrenList (self )
87+ self ._children = Tree .ChildrenList (self )
8688
8789 @property
8890 def children (self ):
8991 return self ._children
9092
9193 @children .setter
9294 def children (self , value ):
93- assert isinstance (value , collections .Container )
95+ assert isinstance (value , collections .Iterable )
9496 while len (self .children ):
9597 self .children .pop ()
9698
97- self ._children = Tree .childrenList (self )
99+ self ._children = Tree .ChildrenList (self )
98100 for x in value :
99101 self .children .append (x )
100102
@@ -131,24 +133,3 @@ def prependChild(self, node):
131133 """ Inserts the given node at the beginning of the children list
132134 """
133135 self .children .insert (0 , node )
134-
135- @classmethod
136- def makenode (cls , symbol , * nexts ):
137- """ Stores the symbol in an AST instance,
138- and left and right to the given ones
139- """
140- result = cls (symbol )
141- for i in nexts :
142- if i is None :
143- continue
144- if not isinstance (i , cls ):
145- raise NotAnAstError (i )
146- result .appendChild (i )
147-
148- return result
149-
150- def __deepcopy (self , memo ):
151- result = Tree (self .symbol ) # No need to duplicate the symbol memory
152- result .next = copy .deepcopy (self .children )
153-
154- return result
0 commit comments