phml.core.nodes.AST

Basic node that holds a root node and has basic utilties.

You can check the size of the tree, iterate over the tree, and directly access the children of the root node.

 1# pylint: disable=invalid-name
 2"""Basic node that holds a root node and has basic utilties.
 3
 4You can check the size of the tree, iterate over the tree, and directly access
 5the children of the root node.
 6"""
 7
 8from __future__ import annotations
 9
10from functools import cached_property
11from typing import Iterator
12
13__all__ = ["AST"]
14
15class AST:
16    """PHML ast.
17
18    Contains utility functions that can manipulate the ast.
19    """
20
21    def __init__(self, tree):
22        if tree is not None and hasattr(tree, "type") and tree.type in ["root", "element"]:
23            self.tree = tree
24        else:
25            raise TypeError("The given tree/root node for AST must be of type `Root` or `Element`")
26
27    def __iter__(self) -> Iterator:
28        from phml import walk  # pylint: disable=import-outside-toplevel
29
30        return walk(self.tree)
31
32    def __eq__(self, obj) -> bool:
33        if isinstance(obj, self.__class__):
34            if self.tree == obj.tree:
35                return True
36        return False
37
38    @cached_property
39    def size(self) -> int:
40        """Get the number of nodes in the ast tree."""
41        from phml import size  # pylint: disable=import-outside-toplevel
42
43        return size(self.tree)
44
45    @property
46    def children(self) -> list:
47        """Get access to the ast roots children.
48        Is none if there is no root.
49        """
50        return self.tree.children if self.tree is not None else None
class AST:
16class AST:
17    """PHML ast.
18
19    Contains utility functions that can manipulate the ast.
20    """
21
22    def __init__(self, tree):
23        if tree is not None and hasattr(tree, "type") and tree.type in ["root", "element"]:
24            self.tree = tree
25        else:
26            raise TypeError("The given tree/root node for AST must be of type `Root` or `Element`")
27
28    def __iter__(self) -> Iterator:
29        from phml import walk  # pylint: disable=import-outside-toplevel
30
31        return walk(self.tree)
32
33    def __eq__(self, obj) -> bool:
34        if isinstance(obj, self.__class__):
35            if self.tree == obj.tree:
36                return True
37        return False
38
39    @cached_property
40    def size(self) -> int:
41        """Get the number of nodes in the ast tree."""
42        from phml import size  # pylint: disable=import-outside-toplevel
43
44        return size(self.tree)
45
46    @property
47    def children(self) -> list:
48        """Get access to the ast roots children.
49        Is none if there is no root.
50        """
51        return self.tree.children if self.tree is not None else None

PHML ast.

Contains utility functions that can manipulate the ast.

AST(tree)
22    def __init__(self, tree):
23        if tree is not None and hasattr(tree, "type") and tree.type in ["root", "element"]:
24            self.tree = tree
25        else:
26            raise TypeError("The given tree/root node for AST must be of type `Root` or `Element`")
size: int

Get the number of nodes in the ast tree.

children: list

Get access to the ast roots children. Is none if there is no root.