Skip to content

base

AST classes and functions.

Classes:

  • ExprType

    ExprType expression class.

AST

AST(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

AST main expression class.

Methods:

  • get_struct

    Return a structure that represents the node 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
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the AST instance."""
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct

Return a structure that represents the node object.

Source code in src/astx/base.py
255
256
257
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a structure that represents the node object."""

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)
    )

ASTKind

Bases: Enum

The expression kind class used for downcasting.

ASTMeta

Bases: type

ASTNodes

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

Bases: Generic[ASTType], AST

AST with a list of nodes, supporting type-specific elements.

Methods:

  • append

    Append a new node to the stack.

  • get_struct

    Return a string that represents 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 a string that represents the object.

Source code in src/astx/base.py
317
318
319
320
321
322
323
324
325
326
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a string that represents the object."""
    args_nodes = []

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

    key = str(self)
    value = cast(ReprStruct, args_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)
    )

DataType

DataType(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: ExprType

AST main expression class.

Methods:

  • get_struct

    Return a simple structure that represents 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
388
389
390
391
392
393
394
395
396
397
398
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc)
    self.name = f"temp_{DataType._tmp_id}"
    DataType._tmp_id += 1
    # set it as a generic data type
    self.type_: ExprType = ExprType()
    self.parent = parent

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a simple structure that represents the object.

Source code in src/astx/base.py
404
405
406
407
408
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a simple structure that represents the object."""
    key = f"DATA-TYPE[{self.__class__.__name__}]"
    value = self.name
    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)
    )

Expr

Expr(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: AST

AST main expression class.

Methods:

  • get_struct

    Return a structure that represents the node 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
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the AST instance."""
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct

Return a structure that represents the node object.

Source code in src/astx/base.py
255
256
257
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a structure that represents the node object."""

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)
    )

ExprType

ExprType(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

ExprType expression class.

Methods:

  • get_struct

    Return a structure that represents the node 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
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the AST instance."""
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a structure that represents the node object.

Source code in src/astx/base.py
344
345
346
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a structure that represents the node object."""
    return {"Type": self.__class__.__name__}

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)
    )

OperatorType

OperatorType(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: DataType

AST main expression class.

Methods:

  • get_struct

    Return a simple structure that represents 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
388
389
390
391
392
393
394
395
396
397
398
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc)
    self.name = f"temp_{DataType._tmp_id}"
    DataType._tmp_id += 1
    # set it as a generic data type
    self.type_: ExprType = ExprType()
    self.parent = parent

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a simple structure that represents the object.

Source code in src/astx/base.py
404
405
406
407
408
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a simple structure that represents the object."""
    key = f"DATA-TYPE[{self.__class__.__name__}]"
    value = self.name
    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)
    )

StatementType

StatementType(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: AST

AST main expression class.

Methods:

  • get_struct

    Return a structure that represents the node 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
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the AST instance."""
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct abstractmethod

get_struct(simplified: bool = False) -> ReprStruct

Return a structure that represents the node object.

Source code in src/astx/base.py
255
256
257
@abstractmethod
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a structure that represents the node object."""

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)
    )

Undefined

Undefined(loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

Undefined expression class.

Methods:

  • get_struct

    Return a simple structure that represents 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
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    """Initialize the AST instance."""
    self.kind = ASTKind.GenericKind
    self.loc = loc
    self.ref = ""
    self.comment = ""
    self.parent = parent
    self._update_parent()

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return a simple structure that represents the object.

Source code in src/astx/base.py
354
355
356
357
358
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return a simple structure that represents the object."""
    value = "UNDEFINED"
    key = "UNDEFINED"
    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)
    )

is_using_jupyter_notebook

is_using_jupyter_notebook() -> bool

Check if it is executed in a jupyter notebook.

Source code in src/astx/base.py
51
52
53
54
55
56
57
58
59
60
def is_using_jupyter_notebook() -> bool:
    """Check if it is executed in a jupyter notebook."""
    try:
        from IPython import get_ipython  # type: ignore

        if "IPKernelApp" in get_ipython().config:  # type: ignore
            return True
    except Exception:
        pass
    return False