Skip to content

blocks

Module for different kind of ASTx blocks.

Classes:

  • Block

    The AST tree.

Block

Block(name: str = 'entry', loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: ASTNodes[ASTType]

The AST tree.

Methods:

  • append

    Append a new node to the stack.

  • get_struct

    Return the AST structure of the object.

  • to_json

    Return an json string that represents the object.

  • to_yaml

    Return an yaml string that represents the object.

Source code in src/astx/base.py
278
279
280
281
282
283
284
285
286
287
288
def __init__(
    self,
    name: str = "entry",
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the AST instance."""
    super().__init__(loc=loc, parent=parent)
    self.name = name
    self.nodes: list[ASTType] = []
    self.position: int = 0

append

append(value: ASTType) -> None

Append a new node to the stack.

Source code in src/astx/base.py
305
306
307
def append(self, value: ASTType) -> None:
    """Append a new node to the stack."""
    self.nodes.append(value)

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/blocks.py
22
23
24
25
26
27
28
29
30
31
32
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    block_nodes = []

    for node in self.nodes:
        block_nodes.append(node.get_struct(simplified))

    key = f"BLOCK[{self.name}]"
    value = cast(ReprStruct, block_nodes)

    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str

Return an json string that represents the object.

Source code in src/astx/base.py
265
266
267
def to_json(self, simplified: bool = False) -> str:
    """Return an json string that represents the object."""
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str

Return an yaml string that represents the object.

Source code in src/astx/base.py
259
260
261
262
263
def to_yaml(self, simplified: bool = False) -> str:
    """Return an yaml string that represents the object."""
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )