Coverage for src/rejuvenation/python_lst_example.py: 92%
40 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 tree-sitter LST-based rewriting of Python source."""
9import textwrap
11from renaissance.integrations.python.ast.factory import PythonFactory, PythonPatternFactory
12from renaissance.integrations.tree_sitter.lst import LSTNode
13from renaissance.syntax_tree import ASTRewriter, ASTShower
14from renaissance.syntax_tree.ast_finder import find_semantic_kind
15from renaissance.syntax_tree.match_finder import match_pattern
16from renaissance.syntax_tree.semantic_kind import SemanticKind
18example_code = """
19from module import foo, bar, baz, quux
20ba(51)
21na(52)
22na(53)
23pa(54)
24if pa():
25 ba()
26pa(54)
27"""
30def python_lst_smoke_test():
31 """AI: Demonstrate matching and rewriting `example_code` using the tree-sitter LST integration."""
32 # adapter = TreeSitterAdapter(tree_sitter_python)
33 # tree = adapter.parse_code(code)
34 # lst = adapter.to_lst(code, tree)
36 factory = PythonFactory(LSTNode)
37 pattern_factory = PythonPatternFactory(factory)
39 atu = factory.create_from_text(example_code, "example.py")
41 pattern1 = pattern_factory.create_statement("if pa(): $$stmts")
42 pattern2 = pattern_factory.create_expression("na($a)")
44 print("_______________pattern 1____________________________________")
45 ASTShower.show_node(pattern1.node, include_properties=True)
46 print("_______________pattern 1____________________________________")
47 ASTShower.show_node(pattern2.node, include_properties=False)
48 print("_______________ast____________________________________")
49 ASTShower.focus = "ba"
50 ASTShower.show_node(atu)
52 print("_______________simple find____________________________________")
53 nodes = find_semantic_kind(atu, SemanticKind.CALL)
55 ASTShower.show_node(nodes[0])
57 pattern1replacement = textwrap.dedent("""
58 # changed if expr to const
59 isAOne=True
60 if(isAOne):
61 $$stmts
62 """)
63 pattern2replacement = "# changed function f1 to f2\nf2($a,123456)\n"
65 rewriter = ASTRewriter(atu)
67 for match in match_pattern(atu.children, [pattern1]):
68 refactor(match, pattern1replacement, rewriter)
70 for match in match_pattern(atu.children, [pattern2]):
71 refactor(match, pattern2replacement, rewriter)
73 return rewriter.apply_to_string()
76def refactor(match, replacement_text, rewriter):
77 """AI: Expand placeholders in `replacement_text` from `match` and queue the rewrite."""
78 for placeholder in match.expansions:
79 replacement_text = replacement_text.replace(placeholder, match[placeholder])
80 return rewriter.replace(replacement_text, match.nodes)
83if __name__ == "__main__":
84 result = python_lst_smoke_test()
85 print("_______________end result_________________________________")
86 print(result)