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
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-30 09:38 -0600
1from __future__ import annotations
3from functools import cached_property
4from typing import Iterator
6__all__ = ["AST"]
9class AST:
10 """PHML ast.
12 Contains utility functions that can manipulate the ast.
13 """
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`")
21 def __iter__(self) -> Iterator:
22 from phml.utils import walk
24 return walk(self.tree)
26 def __eq__(self, obj) -> bool:
27 if isinstance(obj, self.__class__):
28 if self.tree == obj.tree:
29 return True
30 return False
32 @cached_property
33 def size(self) -> int:
34 """Get the number of nodes in the ast tree."""
35 from phml.utils import size
37 return size(self.tree)
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