Skip to content

blocks

Module for different kind of ASTx blocks.

Block

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

Bases: ASTNodes

The AST tree.

Source code in src/astx/base.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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
    # note: maybe it would be nice to add options for rules, so
    #       it could have specific rules for the type of AST
    #       accepted
    self.nodes: list[AST] = []
    self.position: int = 0

append

append(value: AST) -> None

Append a new node to the stack.

Source code in src/astx/base.py
259
260
261
def append(self, value: AST) -> 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
19
20
21
22
23
24
25
26
27
28
29
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 = "BLOCK"
    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
217
218
219
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
211
212
213
214
215
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)
    )