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
« 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"""AI: Example script demonstrating CST-based rewriting of Python source."""
9import textwrap
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
19example_code = """
20from module import foo, bar, baz, quux
21ba(51)
22na(52)
23na(53)
24pa(54)
25if pa():
26 ba()
27pa(54)
28"""
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)
37 factory = PythonFactory(PythonCstNode)
38 pattern_factory = PythonPatternFactory(factory)
40 atu = factory.create_from_text(example_code, "example.py")
42 pattern1 = pattern_factory.create_statement("if pa(): $$stmts")
43 pattern2 = pattern_factory.create_expression("na($a)")
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)
53 print("_______________simple find____________________________________")
54 nodes = find_semantic_kind(atu, SemanticKind.CALL)
56 ASTShower.show_node(nodes[0])
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"
66 rewriter = ASTRewriter(atu)
68 for match in match_pattern(atu.children, [pattern1]):
69 refactor(match, pattern1replacement, rewriter)
71 for match in match_pattern(atu.children, [pattern2]):
72 refactor(match, pattern2replacement, rewriter)
74 return rewriter.apply_to_string()
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)
84if __name__ == "__main__":
85 result = python_lst_smoke_test()
86 print("_______________end result_________________________________")
87 print(result)