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

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 

6 

7"""Protocol defining syntax nodes such as AST, CST, and parse tree nodes.""" 

8 

9from collections.abc import Mapping, Sequence 

10from typing import Any, Protocol, Self, runtime_checkable 

11 

12from renaissance.syntax_tree.text_segment import TextSegment 

13 

14 

15@runtime_checkable 

16class SyntaxNode[NodeType](TextSegment, Protocol): 

17 """Protocol for anything that represents syntax nodes. 

18 

19 Syntax nodes include AST nodes, CST nodes, and parse tree nodes. 

20 A syntax node is a text segment. 

21 

22 Read-only access is enforced "as much as possible" by 

23 exposing only @property getters in the protocol 

24 """ 

25 

26 @property 

27 def kind(self) -> str: 

28 """The textual representation of the kind of this node. 

29 

30 The property 'kind' is used to compare syntax nodes. 

31 """ 

32 ... 

33 

34 @property 

35 def children(self) -> Sequence[Self]: 

36 """The children of this node. 

37 

38 The read-only Sequence keeps the protocol covariant, so implementations may return a list. 

39 

40 The property 'children' is used to compare syntax nodes. 

41 """ 

42 ... 

43 

44 @property 

45 def syntax_attributes(self) -> Mapping[str, Any]: 

46 """The syntax attributes of this node. 

47 

48 The read-only Mapping keeps the protocol covariant, so implementations may return a dict. 

49 

50 The property 'syntax_attributes' is used to compare syntax nodes. 

51 """ 

52 ... 

53 

54 @property 

55 def parent(self) -> Self | None: 

56 """The parent of this node. 

57 

58 The parent should only be None when the node represents the top of a tree, 

59 such as a compilation unit. 

60 

61 The property 'parent' is not used to compare syntax nodes. 

62 """ 

63 ... 

64 

65 @property 

66 def original_node(self) -> NodeType: 

67 """The original node as produced by the parser. 

68 

69 The property 'original_node' is not used to compare syntax nodes. 

70 """ 

71 ...