pylasu.model package

Submodules

pylasu.model.errors module

class pylasu.model.errors.ErrorNode(message: str = None, position: Position | None = None)[source]

Bases: object

An AST node that marks the presence of an error, for example a syntactic or semantic error in the original tree.

message: str = None
position: Position | None = None
class pylasu.model.errors.GenericErrorNode(message: str = None, position: pylasu.model.position.Position | None = None, error: Exception | None = None)[source]

Bases: Node, ErrorNode

error: Exception | None = None

pylasu.model.model module

class pylasu.model.model.CompositeDestination(elements: List[pylasu.model.model.Destination] = <factory>)[source]

Bases: Destination

elements: List[Destination]
class pylasu.model.model.CompositeOrigin(elements: List[pylasu.model.model.Origin] = <factory>, position: Optional[pylasu.model.position.Position] = None, source_text: Optional[str] = None)[source]

Bases: Origin

elements: List[Origin]
position: Position | None = None
source_text: str | None = None
class pylasu.model.model.Concept(name, bases, namespace, /, **kwargs)[source]

Bases: ABCMeta

is_node_property(name)[source]
property node_properties
class pylasu.model.model.Destination[source]

Bases: ABC

class pylasu.model.model.InternalField(default, default_factory, init, repr, hash, compare, metadata, kw_only)[source]

Bases: Field

compare
default
default_factory
hash
init
kw_only
metadata
name
repr
type
class pylasu.model.model.Node(origin: Origin | None = None, parent: Node | None = None, position_override: Position | None = None)[source]

Bases: Origin, Destination

assign_parents()

Sets or corrects the parent of all AST nodes.

Pylasu does not see set/add/delete operations on the AST nodes, so this function should be called manually after modifying the AST, unless you’ve taken care of assigning the parents yourself.

Parameters:

self – the root of the AST subtree to start from.

property children
destination: Destination | None = None
find_ancestor_of_type(target: type)

Returns the nearest ancestor of this node that is an instance of the target type.

Note that type is not strictly forced to be a subtype of Node. This is intended to support trait types like Statement or Expression. However, the returned value is guaranteed to be a Node, as only Node instances can be part of the hierarchy.

Parameters:
  • self – the node from which to start the search.

  • target – the target type.

property node_type[source]
origin: Origin | None = None
parent: Node | None = None
property position: Position | None[source]
position_override: Position | None = None
property properties[source]
replace_with(other: Node)

Replace this node with another (by modifying the children of the parent node). For this to work, this node must have a parent assigned.

Parameters:
  • self – the node to replace.

  • other – the replacement node.

search_by_type(target_type, walker=<function walk>)
property source: Source | None[source]
property source_text: str | None[source]
transform_children(operation: Callable[[Node], Node])
walk()

Walks the whole AST starting from this node, depth-first.

walk_ancestors()

Iterator over the sequence of nodes from this node’s parent all the way up to the root node.

walk_descendants(walker=<function walk>, restrict_to=<class 'pylasu.model.model.Node'>)

Walks the whole AST starting from the child nodes of this node.

Parameters:
  • self – the node from which to start the walk, which is NOT included in the walk.

  • walker – a function that generates a sequence of nodes. By default this is the depth-first “walk” method.

For post-order traversal, use “walk_leaves_first”. :param restrict_to: optional type filter. By default, all nodes (i.e., subclasses of Node) are included, but you can limit the walk to only a subtype of Node.

walk_leaves_first()

Performs a post-order (or leaves-first) node traversal starting with a given node.

walk_within(position: Position)

Walks the AST within the given [position] starting from this node, depth-first. :param self: the node from which to start the walk. :param position: the position within which the walk should remain.

with_origin(origin: Origin | None)[source]
with_parent(parent: Node | None)[source]
with_position(position: Position | None)[source]
class pylasu.model.model.Origin[source]

Bases: ABC

abstract property position: Position | None[source]
property source: Source | None[source]
property source_text: str | None[source]
class pylasu.model.model.TextFileDestination(position: pylasu.model.position.Position | None = None)[source]

Bases: Destination

position: Position | None = None
pylasu.model.model.concept_of(node)[source]
pylasu.model.model.internal_field(*, default=<dataclasses._MISSING_TYPE object>, default_factory=<dataclasses._MISSING_TYPE object>, init=True, repr=True, hash=None, compare=True, metadata=None, kw_only=False)[source]

Return an object to identify internal dataclass fields. The arguments are the same as dataclasses.field.

pylasu.model.model.internal_properties(*props: str)[source]
class pylasu.model.model.internal_property(fget=None, fset=None, fdel=None, doc=None)[source]

Bases: property

pylasu.model.model.is_internal_property_or_method(value)[source]
pylasu.model.model.provides_nodes(decl_type)[source]

pylasu.model.naming module

class pylasu.model.naming.Named(name: str)[source]

Bases: PossiblyNamed

name: str
class pylasu.model.naming.PossiblyNamed(name: str = None)[source]

Bases: object

name: str
class pylasu.model.naming.ReferenceByName(name: str, referred: T | None = None)[source]

Bases: Generic[T]

name: str
referred: T | None = None
resolve(scope: Scope) bool[source]
resolved()[source]
try_to_resolve(candidates: List[T], case_insensitive: bool = False) bool[source]

Try to resolve the reference by finding a named element with a matching name. The name match is performed in a case sensitive or insensitive way depending on the value of caseInsensitive.

class pylasu.model.naming.Scope(symbols: Dict[str, List[pylasu.model.naming.Symbol]] = <factory>, parent: Optional[ForwardRef('Scope')] = None)[source]

Bases: object

add(symbol: Symbol)[source]
lookup(symbol_name: str, symbol_type: type = <class 'pylasu.model.naming.Symbol'>) Symbol | None[source]
parent: Scope | None = None
symbols: Dict[str, List[Symbol]]
class pylasu.model.naming.Symbol(name: str)[source]

Bases: PossiblyNamed

pylasu.model.position module

class pylasu.model.position.FileSource(file: pathlib.Path)[source]

Bases: Source

file: Path
class pylasu.model.position.Point(line: int, column: int)[source]

Bases: object

column: int
is_before(other: Point)[source]
line: int
class pylasu.model.position.Position(start: Point, end: Point, source: Source = None)[source]

Bases: object

An area in a source file, from start to end. The start point is the point right before the starting character. The end point is the point right after the last character. An empty position will have coinciding points.

Consider a file with one line, containing the text “HELLO”. The Position of such text will be Position(Point(1, 0), Point(1, 5)).

end: Point
source: Source = None
start: Point
class pylasu.model.position.Source[source]

Bases: object

class pylasu.model.position.SourceSet(name: str, root: pathlib.Path)[source]

Bases: object

name: str
root: Path
class pylasu.model.position.SourceSetElement(sourceSet: pylasu.model.position.SourceSet, relativePath: pathlib.Path)[source]

Bases: Source

relativePath: Path
sourceSet: SourceSet
class pylasu.model.position.StringSource(code: str = None)[source]

Bases: Source

code: str = None
class pylasu.model.position.URLSource(url: str = None)[source]

Bases: Source

url: str = None
pylasu.model.position.pos(start_line: int, start_col: int, end_line: int, end_col: int, source: Source = None)[source]

Utility function to create a Position

pylasu.model.processing module

pylasu.model.processing.assign_parents(self: Node)[source]

Sets or corrects the parent of all AST nodes.

Pylasu does not see set/add/delete operations on the AST nodes, so this function should be called manually after modifying the AST, unless you’ve taken care of assigning the parents yourself.

Parameters:

self – the root of the AST subtree to start from.

pylasu.model.processing.children(self: Node)[source]
pylasu.model.processing.nodes_in(iterable)[source]
pylasu.model.processing.replace_with(self: Node, other: Node)[source]

Replace this node with another (by modifying the children of the parent node). For this to work, this node must have a parent assigned.

Parameters:
  • self – the node to replace.

  • other – the replacement node.

pylasu.model.processing.search_by_type(self: ~pylasu.model.model.Node, target_type, walker=<function walk>)[source]
pylasu.model.processing.transform_children(self: Node, operation: Callable[[Node], Node])[source]

pylasu.model.traversing module

pylasu.model.traversing.find_ancestor_of_type(self: Node, target: type)[source]

Returns the nearest ancestor of this node that is an instance of the target type.

Note that type is not strictly forced to be a subtype of Node. This is intended to support trait types like Statement or Expression. However, the returned value is guaranteed to be a Node, as only Node instances can be part of the hierarchy.

Parameters:
  • self – the node from which to start the search.

  • target – the target type.

pylasu.model.traversing.walk(self: Node)[source]

Walks the whole AST starting from this node, depth-first.

pylasu.model.traversing.walk_ancestors(self: Node)[source]

Iterator over the sequence of nodes from this node’s parent all the way up to the root node.

pylasu.model.traversing.walk_descendants(self: ~pylasu.model.model.Node, walker=<function walk>, restrict_to=<class 'pylasu.model.model.Node'>)[source]

Walks the whole AST starting from the child nodes of this node.

Parameters:
  • self – the node from which to start the walk, which is NOT included in the walk.

  • walker – a function that generates a sequence of nodes. By default this is the depth-first “walk” method.

For post-order traversal, use “walk_leaves_first”. :param restrict_to: optional type filter. By default, all nodes (i.e., subclasses of Node) are included, but you can limit the walk to only a subtype of Node.

pylasu.model.traversing.walk_leaves_first(self: Node)[source]

Performs a post-order (or leaves-first) node traversal starting with a given node.

pylasu.model.traversing.walk_within(self: Node, position: Position)[source]

Walks the AST within the given [position] starting from this node, depth-first. :param self: the node from which to start the walk. :param position: the position within which the walk should remain.

Module contents