Skip to content

packages

Define ASTx for more broader scope.

Classes:

  • AliasExpr

    Represents an alias in an import statement.

  • ImportExpr

    Represents an import operation as an expression.

  • ImportFromExpr

    Represents a 'from ... import ...' operation as an expression.

  • ImportFromStmt

    Represents an import-from statement.

  • ImportStmt

    Represents an import statement.

  • Module

    AST main expression class.

  • Package

    AST class for Package.

  • Program

    AST class for Program.

  • Target

    Define the Architecture target for the program.

AliasExpr

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

Bases: Expr

Represents an alias in an import statement.

Methods:

  • get_struct

    Return the AST structure of the alias.

  • 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/packages.py
167
168
169
170
171
172
173
174
175
176
177
def __init__(
    self,
    name: str,
    asname: str = "",
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.name = name
    self.asname = asname
    self.kind = ASTKind.AliasExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the alias.

Source code in src/astx/packages.py
186
187
188
189
190
191
192
193
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the alias."""
    str_asname = f", {self.asname}" if self.asname else ""
    str_name_asname = f"[{self.name}{str_asname}]"
    key = f"Alias {str_name_asname}"
    value = ""

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

ImportExpr

ImportExpr(names: list[AliasExpr], loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

Represents an import operation as an expression.

Methods:

  • get_struct

    Return the AST structure of the import expression.

  • 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/packages.py
281
282
283
284
285
286
287
288
289
def __init__(
    self,
    names: list[AliasExpr],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.names = names
    self.kind = ASTKind.ImportExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the import expression.

Source code in src/astx/packages.py
296
297
298
299
300
301
302
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the import expression."""
    key = "ImportExpr"
    value = cast(
        ReprStruct, [name.get_struct(simplified) for name in self.names]
    )
    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)
    )

ImportFromExpr

ImportFromExpr(names: list[AliasExpr], module: str = '', level: int = 0, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: Expr

Represents a 'from ... import ...' operation as an expression.

Methods:

  • get_struct

    Return the AST structure of the import-from expression.

  • 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/packages.py
314
315
316
317
318
319
320
321
322
323
324
325
326
def __init__(
    self,
    names: list[AliasExpr],
    module: str = "",
    level: int = 0,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.names = names
    self.module = module
    self.level = level
    self.kind = ASTKind.ImportFromExprKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the import-from expression.

Source code in src/astx/packages.py
338
339
340
341
342
343
344
345
346
347
348
349
350
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the import-from expression."""
    level_dots = "." * self.level
    module_str = (
        f"{level_dots}{self.module}" if self.module else level_dots
    )

    key = f"ImportFromExpr [{module_str}]"
    value = cast(
        ReprStruct, [name.get_struct(simplified) for name in self.names]
    )

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

ImportFromStmt

ImportFromStmt(names: list[AliasExpr], module: str = '', level: int = 0, loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

Represents an import-from statement.

Methods:

  • get_struct

    Return the AST structure of the import-from statement.

  • 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/packages.py
236
237
238
239
240
241
242
243
244
245
246
247
248
def __init__(
    self,
    names: list[AliasExpr],
    module: str = "",
    level: int = 0,
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.module = module
    self.names = names
    self.level = level
    self.kind = ASTKind.ImportFromStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the import-from statement.

Source code in src/astx/packages.py
259
260
261
262
263
264
265
266
267
268
269
270
271
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the import-from statement."""
    level_dots = "." * self.level
    module_str = (
        f"{level_dots}{self.module}" if self.module else level_dots
    )

    key = f"ImportFromStmt [{module_str}]"
    value = cast(
        ReprStruct, [name.get_struct(simplified) for name in self.names]
    )

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

ImportStmt

ImportStmt(names: list[AliasExpr], loc: SourceLocation = NO_SOURCE_LOCATION, parent: Optional[ASTNodes] = None)

Bases: StatementType

Represents an import statement.

Methods:

  • get_struct

    Return the AST structure of the import statement.

  • 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/packages.py
203
204
205
206
207
208
209
210
211
def __init__(
    self,
    names: list[AliasExpr],
    loc: SourceLocation = NO_SOURCE_LOCATION,
    parent: Optional[ASTNodes] = None,
) -> None:
    super().__init__(loc=loc, parent=parent)
    self.names = names
    self.kind = ASTKind.ImportStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the import statement.

Source code in src/astx/packages.py
218
219
220
221
222
223
224
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the import statement."""
    key = "ImportStmt"
    value = cast(
        ReprStruct, [name.get_struct(simplified) for name in self.names]
    )
    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)
    )

Module

Module(name: str = 'main', loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: Block

AST main expression class.

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.

Attributes:

Source code in src/astx/packages.py
53
54
55
56
57
58
59
60
def __init__(
    self,
    name: str = "main",
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the AST instance."""
    super().__init__(name=name, loc=loc)
    self.kind = ASTKind.ModuleKind

block property

block: list[AST]

Define an alias for self.nodes.

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/packages.py
71
72
73
74
75
76
77
78
79
80
81
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    block_node = []

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

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

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

Package

Package(name: str = 'main', modules: list[Module] = [], packages: list[Package] = [], loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: ASTNodes

AST class for Package.

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/packages.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def __init__(
    self,
    name: str = "main",
    modules: list[Module] = [],
    packages: list[Package] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the AST instance."""
    super().__init__(loc=loc)
    self.name = name
    self.modules = copy.deepcopy(modules)
    self.packages = copy.deepcopy(packages)

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/packages.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    packages = []
    modules = []

    for package in self.packages:
        packages.append(package.get_struct(simplified))

    for module in self.modules:
        modules.append(module.get_struct(simplified))

    key = str(self)
    value = cast(
        ReprStruct,
        {
            "modules": modules,
            "packages": packages,
        },
    )

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

Program

Program(name: str = 'main', target: Target = Target('', ''), modules: list[Module] = [], packages: list[Package] = [], loc: SourceLocation = NO_SOURCE_LOCATION)

Bases: Package

AST class for Program.

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/packages.py
140
141
142
143
144
145
146
147
148
149
150
151
152
def __init__(
    self,
    name: str = "main",
    target: Target = Target("", ""),
    modules: list[Module] = [],
    packages: list[Package] = [],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """Initialize the AST instance."""
    super().__init__(
        name=name, modules=modules, packages=packages, loc=loc
    )
    self.target = copy.deepcopy(target)

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/packages.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    packages = []
    modules = []

    for package in self.packages:
        packages.append(package.get_struct(simplified))

    for module in self.modules:
        modules.append(module.get_struct(simplified))

    key = str(self)
    value = cast(
        ReprStruct,
        {
            "modules": modules,
            "packages": packages,
        },
    )

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

Target

Target(datalayout: str, triple: str)

Bases: Expr

Define the Architecture target for the program.

Methods:

  • 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/packages.py
33
34
35
36
37
def __init__(self, datalayout: str, triple: str) -> None:
    """Initialize the AST instance."""
    super().__init__()
    self.datalayout = datalayout
    self.triple = triple

get_struct

get_struct(simplified: bool = False) -> ReprStruct

Return the AST structure of the object.

Source code in src/astx/packages.py
39
40
41
42
43
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """Return the AST structure of the object."""
    key = "TARGET"
    value = f"{self.datalayout}, {self.triple}"
    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)
    )