Coverage for phml\nodes\AST.py: 42%

24 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-30 09:38 -0600

1from __future__ import annotations 

2 

3from functools import cached_property 

4from typing import Iterator 

5 

6__all__ = ["AST"] 

7 

8 

9class AST: 

10 """PHML ast. 

11 

12 Contains utility functions that can manipulate the ast. 

13 """ 

14 

15 def __init__(self, tree): 

16 if hasattr(tree, "type") and tree.type in ["root", "element"]: 

17 self.tree = tree 

18 else: 

19 raise TypeError("The given tree/root node for AST must be of type `Root` or `Element`") 

20 

21 def __iter__(self) -> Iterator: 

22 from phml.utils import walk 

23 

24 return walk(self.tree) 

25 

26 def __eq__(self, obj) -> bool: 

27 if isinstance(obj, self.__class__): 

28 if self.tree == obj.tree: 

29 return True 

30 return False 

31 

32 @cached_property 

33 def size(self) -> int: 

34 """Get the number of nodes in the ast tree.""" 

35 from phml.utils import size 

36 

37 return size(self.tree) 

38 

39 @property 

40 def children(self) -> list: 

41 """Get access to the ast roots children. 

42 Is none if there is no root. 

43 """ 

44 return self.tree.children if self.tree is not None else None