Coverage for src/renaissance/syntax_tree/syntax_node.py: 100%
15 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""Protocol defining syntax nodes such as AST, CST, and parse tree nodes."""
9from collections.abc import Mapping, Sequence
10from typing import Any, Protocol, Self, runtime_checkable
12from renaissance.syntax_tree.text_segment import TextSegment
15@runtime_checkable
16class SyntaxNode[NodeType](TextSegment, Protocol):
17 """Protocol for anything that represents syntax nodes.
19 Syntax nodes include AST nodes, CST nodes, and parse tree nodes.
20 A syntax node is a text segment.
22 Read-only access is enforced "as much as possible" by
23 exposing only @property getters in the protocol
24 """
26 @property
27 def kind(self) -> str:
28 """The textual representation of the kind of this node.
30 The property 'kind' is used to compare syntax nodes.
31 """
32 ...
34 @property
35 def children(self) -> Sequence[Self]:
36 """The children of this node.
38 The read-only Sequence keeps the protocol covariant, so implementations may return a list.
40 The property 'children' is used to compare syntax nodes.
41 """
42 ...
44 @property
45 def syntax_attributes(self) -> Mapping[str, Any]:
46 """The syntax attributes of this node.
48 The read-only Mapping keeps the protocol covariant, so implementations may return a dict.
50 The property 'syntax_attributes' is used to compare syntax nodes.
51 """
52 ...
54 @property
55 def parent(self) -> Self | None:
56 """The parent of this node.
58 The parent should only be None when the node represents the top of a tree,
59 such as a compilation unit.
61 The property 'parent' is not used to compare syntax nodes.
62 """
63 ...
65 @property
66 def original_node(self) -> NodeType:
67 """The original node as produced by the parser.
69 The property 'original_node' is not used to compare syntax nodes.
70 """
71 ...