Coverage for src/rejuvenation/python_cst_example.py: 80%

41 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"""AI: Example script demonstrating CST-based rewriting of Python source.""" 

8 

9import textwrap 

10 

11from rejuvenation.python_lst_example import python_lst_smoke_test 

12from renaissance.integrations.python.ast.cst_node import PythonCstNode 

13from renaissance.integrations.python.ast.factory import PythonFactory, PythonPatternFactory 

14from renaissance.syntax_tree import ASTRewriter, ASTShower 

15from renaissance.syntax_tree.ast_finder import find_semantic_kind 

16from renaissance.syntax_tree.match_finder import match_pattern 

17from renaissance.syntax_tree.semantic_kind import SemanticKind 

18 

19example_code = """ 

20from module import foo, bar, baz, quux 

21ba(51) 

22na(52) 

23na(53) 

24pa(54) 

25if pa(): 

26 ba() 

27pa(54) 

28""" 

29 

30 

31def python_cst_smoke_test(): 

32 """AI: Demonstrate matching and rewriting `example_code` using the CST integration.""" 

33 # adapter = TreeSitterAdapter(tree_sitter_python) 

34 # tree = adapter.parse_code(code) 

35 # lst = adapter.to_lst(code, tree) 

36 

37 factory = PythonFactory(PythonCstNode) 

38 pattern_factory = PythonPatternFactory(factory) 

39 

40 atu = factory.create_from_text(example_code, "example.py") 

41 

42 pattern1 = pattern_factory.create_statement("if pa(): $$stmts") 

43 pattern2 = pattern_factory.create_expression("na($a)") 

44 

45 print("_______________pattern 1____________________________________") 

46 ASTShower.show_node(pattern1.node, include_properties=True) 

47 print("_______________pattern 1____________________________________") 

48 ASTShower.show_node(pattern2.node, include_properties=False) 

49 print("_______________ast____________________________________") 

50 ASTShower.focus = "ba" 

51 ASTShower.show_node(atu) 

52 

53 print("_______________simple find____________________________________") 

54 nodes = find_semantic_kind(atu, SemanticKind.CALL) 

55 

56 ASTShower.show_node(nodes[0]) 

57 

58 pattern1replacement = textwrap.dedent(""" 

59 # changed if expr to const 

60 isAOne=True 

61 if(isAOne): 

62 $$stmts 

63 """) 

64 pattern2replacement = "# changed function f1 to f2\nf2($a,123456)\n" 

65 

66 rewriter = ASTRewriter(atu) 

67 

68 for match in match_pattern(atu.children, [pattern1]): 

69 refactor(match, pattern1replacement, rewriter) 

70 

71 for match in match_pattern(atu.children, [pattern2]): 

72 refactor(match, pattern2replacement, rewriter) 

73 

74 return rewriter.apply_to_string() 

75 

76 

77def refactor(match, replacement_text, rewriter): 

78 """AI: Expand placeholders in `replacement_text` from `match` and queue the rewrite.""" 

79 for placeholder in match.expansions: 

80 replacement_text = replacement_text.replace(placeholder, match[placeholder]) 

81 return rewriter.replace(replacement_text, match.nodes) 

82 

83 

84if __name__ == "__main__": 

85 result = python_lst_smoke_test() 

86 print("_______________end result_________________________________") 

87 print(result)