Coverage for src/renaissance/syntax_tree/ast_processor.py: 98%

63 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-16 16:04 +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"""AI: Processor that coordinates parsing, pattern matching, and rewriting of a single AST.""" 

8 

9from collections.abc import Callable, Iterator, Mapping, Sequence 

10from pathlib import Path 

11from typing import Any 

12 

13import renaissance.syntax_tree.match_finder 

14from renaissance.syntax_tree import ASTNode 

15from renaissance.syntax_tree.ast_factory import ASTFactory 

16from renaissance.syntax_tree.ast_finder import ASTFinder, find_semantic_kind 

17from renaissance.syntax_tree.ast_rewriter import ASTRewriter, Rewritable 

18from renaissance.syntax_tree.match_finder import PatternMatch 

19from renaissance.syntax_tree.node_protocol import NodeProtocol 

20from renaissance.syntax_tree.semantic_kind import SemanticKind 

21 

22 

23class ASTProcessor: 

24 """AI: Processor that coordinates parsing, pattern matching, and rewriting of a single AST.""" 

25 

26 def __init__( 

27 self, 

28 root: ASTNode, 

29 ast_factory: ASTFactory, 

30 in_memory: bool = False, 

31 ) -> None: 

32 """AI: Prepare a processor that runs refactoring/analysis steps over the given AST.""" 

33 self.__root_node = root 

34 self.__rewriter = ASTRewriter(root) 

35 self.__ast_factory = ast_factory 

36 self.in_memory = in_memory 

37 self.repeat_step = 0 

38 

39 @property 

40 def factory(self) -> ASTFactory: 

41 """AI: Return the AST factory used to create nodes for this processor.""" 

42 return self.__ast_factory 

43 

44 @property 

45 def node(self) -> ASTNode: 

46 """AI: Return the root AST node this processor operates on.""" 

47 return self.__root_node 

48 

49 @property 

50 def filename(self) -> str: 

51 """AI: Return the filename of the AST this processor operates on.""" 

52 return self.__rewriter.get_filename() 

53 

54 @property 

55 def root(self) -> ASTNode: 

56 """AI: Return the root AST node this processor operates on.""" 

57 return self.__root_node 

58 

59 def replace( 

60 self, 

61 new_content: str, 

62 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

63 include_whitespace: bool = True, 

64 include_comments: bool = True, 

65 transforms: Mapping[str, Callable[[str], str]] | None = None, 

66 ) -> None: 

67 """Queue a rewrite replacing target's source text with new_content. 

68 

69 Args: 

70 new_content: The replacement text; for a `PatternMatch` target, its placeholders are expanded. 

71 target: The nodes or match to replace. 

72 include_whitespace: Whether the whitespace around target is replaced as well. 

73 include_comments: Whether the comments around target are replaced as well. 

74 transforms: Per placeholder, a function applied to the text of each node bound to it before expansion. 

75 

76 """ 

77 self.__rewriter.replace(new_content, target, include_whitespace, include_comments, transforms) 

78 

79 def remove( 

80 self, 

81 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

82 include_whitespace: bool = True, 

83 include_comments: bool = True, 

84 ) -> None: 

85 """AI: Queue a rewrite removing target's source text.""" 

86 self.__rewriter.remove(target, include_whitespace, include_comments) 

87 

88 def insert_before( 

89 self, 

90 new_content: str, 

91 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

92 include_whitespace: bool = True, 

93 include_comments: bool = True, 

94 ) -> None: 

95 """AI: Queue a rewrite inserting new_content immediately before target's source text.""" 

96 self.__rewriter.insert_before(new_content, target, include_whitespace, include_comments) 

97 

98 def insert_after( 

99 self, 

100 new_content: str, 

101 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

102 include_whitespace: bool = True, 

103 include_comments: bool = True, 

104 ) -> None: 

105 """AI: Queue a rewrite inserting new_content immediately after target's source text.""" 

106 self.__rewriter.insert_after(new_content, target, include_whitespace, include_comments) 

107 

108 def find_all(self, function: Callable[[ASTNode], Iterator[ASTNode] | bool]) -> Sequence[ASTNode]: 

109 """AI: Return all descendant nodes of the root for which function returns a truthy value or child iterator.""" 

110 return ASTFinder.find_all(self.__root_node, function) 

111 

112 def find_semantic_kind(self, kind: SemanticKind) -> Sequence[NodeProtocol]: 

113 """AI: Return all descendant nodes of the root matching the given semantic kind.""" 

114 return find_semantic_kind(self.__root_node, kind) 

115 

116 def find_match(self, *patterns_list: Any, recursive: bool = True) -> Sequence[PatternMatch]: 

117 """AI: Return matches of patterns_list against the root's children.""" 

118 return renaissance.syntax_tree.match_finder.find_all( 

119 self.__root_node.children, 

120 *patterns_list, 

121 recursive=recursive, 

122 ) 

123 

124 def has_changed(self) -> bool: 

125 """AI: Return whether any rewrites have been queued.""" 

126 return self.__rewriter.has_changed() 

127 

128 def apply_to_string(self) -> str: 

129 """AI: Return the rewritten source as a string, applying all queued rewrites.""" 

130 return self.__rewriter.apply_to_string() 

131 

132 def commit(self) -> ASTProcessor: 

133 """Commit the current changes to the AST (Abstract Syntax Tree) and return a new ASTProcessor instance. 

134 

135 This method applies the current changes to the source code and creates a new ASTProcessor instance 

136 with the updated AST. If the changes are in-memory, it directly creates the new AST from the updated 

137 code string. Otherwise, it writes the changes to the file, reloads the file, and then creates the new AST. 

138 

139 Returns: 

140 ASTProcessor: A new instance of ASTProcessor with the updated AST. 

141 

142 Raises: 

143 IOError: If there is an error writing to the file. 

144 

145 """ 

146 if not self.__rewriter.has_changed(): 

147 return self 

148 self.__root_node, self.__rewriter = self._commit(self.__rewriter, self.__ast_factory, self.in_memory) 

149 return ASTProcessor(self.__root_node, self.__ast_factory, self.in_memory) 

150 

151 @staticmethod 

152 def _commit(rewriter: ASTRewriter, factory: ASTFactory, in_memory: bool = False) -> tuple[ASTNode[Any, Any], ASTRewriter]: 

153 rewriter.apply_to_string() 

154 if in_memory: 

155 atu = factory.create_from_text(rewriter.apply_to_string(), rewriter.get_filename()) 

156 return atu, ASTRewriter(atu) 

157 # save file first then reload it 

158 with Path(rewriter.get_filename()).open("wb") as f: 

159 f.write(rewriter.apply()) 

160 atu = factory.create(Path(rewriter.get_filename())) 

161 return atu, ASTRewriter(atu)