Skip to content

blocks

Classes:

Block

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

Bases: ASTNodes[ASTType]

Methods:

Source code in packages/astx/src/astx/base.py
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
def __init__(
    self,
    name: str = "entry",
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """
    title: Initialize the AST instance.
    parameters:
      name:
        type: str
      loc:
        type: SourceLocation
      parent:
        type: Optional[ASTNodes]
    """
    super().__init__(loc=loc, parent=parent)
    self.name = name
    self.nodes: list[ASTType] = []
    self.position: int = 0

append

append(value: ASTType) -> None
Source code in packages/astx/src/astx/base.py
484
485
486
487
488
489
490
491
def append(self, value: ASTType) -> None:
    """
    title: Append a new node to the stack.
    parameters:
      value:
        type: ASTType
    """
    self.nodes.append(value)

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/blocks.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    block_nodes = []

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

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

    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )