Coverage for src/renaissance/integrations/tree_sitter/adapter.py: 100%

24 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: Adapter that parses source code with tree-sitter into the internal LST representation.""" 

8 

9from tree_sitter import Language, Parser 

10 

11from renaissance.integrations.tree_sitter.lst import LST, LSTNode 

12from renaissance.utils.ast_utils import detect_placeholder, replace_dollar 

13 

14 

15class TreeSitterAdapter: 

16 """AI: Adapter that parses source code with tree-sitter into the internal LST representation.""" 

17 

18 def __init__(self, grammar_module) -> None: 

19 """AI: Configure a tree-sitter parser for the given language grammar.""" 

20 language = Language(grammar_module.language()) 

21 self.language = language 

22 self.parser = Parser(language) 

23 

24 def parse_code(self, source_code: str): 

25 """AI: Parse source_code with tree-sitter and return the resulting parse tree.""" 

26 return self.parser.parse(bytes(source_code, "utf8")) 

27 

28 def to_lst(self, source_code: str, tree) -> LST: 

29 """AI: Convert a tree-sitter parse tree of source_code into an LST.""" 

30 root_node = tree.root_node 

31 source_code = replace_dollar(source_code) 

32 return LST(self._convert_node(root_node, source_code, None)) 

33 

34 def _convert_node(self, node, source_code: str, parent, root=None) -> LSTNode: 

35 signature = source_code[node.start_byte : node.end_byte] 

36 is_ph, coerced_type, ph_name = detect_placeholder(signature, node.type) 

37 

38 lst_node = LSTNode( 

39 node_type=coerced_type if is_ph else node.type, 

40 properties={ 

41 "start_point": node.start_point, 

42 "end_point": node.end_point, 

43 "source_code": source_code, 

44 "name": ph_name, 

45 "is_named": node.is_named, 

46 **( 

47 { 

48 "placeholder": True, 

49 "placeholder_name": ph_name, 

50 "original_node_type": node.type, 

51 } 

52 if is_ph 

53 else {} 

54 ), 

55 }, 

56 signature=signature, 

57 offset=node.start_byte, 

58 children=[], 

59 parent=parent, 

60 root=root, 

61 ) 

62 if not root: 

63 root = lst_node 

64 

65 for child in node.children: 

66 lst_child = self._convert_node(child, source_code, lst_node, root) 

67 lst_node.add_child(lst_child) 

68 return lst_node