MCPcopy Index your code
hub / github.com/FSoft-AI4Code/CodeText-parser / parse_code

Function parse_code

src/codetext/utils/utils.py:71–117  ·  view source on GitHub ↗

Auto parse raw code into `tree_sitter.Tree` Args: raw_code (str): Raw source code need to parse language (str): Language to load parser

(raw_code: str, language: str='Auto', tree_sitter_path: str=None)

Source from the content-addressed store, hash-verified

69
70
71def parse_code(raw_code: str, language: str='Auto', tree_sitter_path: str=None) -> tree_sitter.Tree:
72 """
73 Auto parse raw code into `tree_sitter.Tree`
74
75 Args:
76 raw_code (str): Raw source code need to parse
77 language (str): Language to load parser
78 """
79 # TODO: auto detect language
80 if language == 'Auto':
81 raise NotImplemented("This feature is underdevelopment")
82 language = str(language).lower()
83 if language == 'c#':
84 language = 'c_sharp'
85 elif language == 'c++':
86 language = 'cpp'
87 assert language in SUPPORTED_LANGUAGE, f"Expect {language} in {SUPPORTED_LANGUAGE}"
88
89 if tree_sitter_path:
90 load_path = tree_sitter_path
91 else:
92 calling_script_path = Path(inspect.getframeinfo(sys._getframe(1)).filename)
93 load_path = str(calling_script_path.parent)
94
95 # Get parser from languages
96 parser = Parser()
97 try:
98 from tree_sitter_languages import get_language, get_parser
99 language = get_language(language)
100 except ImportError:
101 # Work-around when pre-built binaries wheels for tree-sitter-languages are not available
102 logger.warning(f"Troubled importing 'tree-sitter-languages', attemp to look for pre-built binaries in the workspace")
103 ts_lang_path = os.path.join(load_path, 'tree-sitter', f'{language}.so')
104 if not os.path.exists(ts_lang_path):
105 logger.warning(f"Not found `{language}.so` in `{load_path}/tree-sitter/`, attemp to build language")
106 build_language(language, load_path)
107 language = Language(load_path + f"/tree-sitter/{language}.so", language)
108 parser.set_language(language)
109
110 if isinstance(raw_code, str):
111 raw_code = bytes(raw_code, 'utf8')
112 elif isinstance(raw_code, bytes):
113 pass
114 else:
115 raise ValueError(f"Expect `str`, got {type(raw_code)}")
116 tree = parser.parse(raw_code)
117 return tree

Callers 15

test_parse_codeMethod · 0.90
setUpMethod · 0.90
test_get_docstringMethod · 0.90
setUpMethod · 0.90
test_get_docstringMethod · 0.90
setUpMethod · 0.90
test_get_docstringMethod · 0.90
setUpMethod · 0.90
test_get_docstringMethod · 0.90
setUpMethod · 0.90

Calls 1

build_languageFunction · 0.85

Tested by 15

test_parse_codeMethod · 0.72
setUpMethod · 0.72
test_get_docstringMethod · 0.72
setUpMethod · 0.72
test_get_docstringMethod · 0.72
setUpMethod · 0.72
test_get_docstringMethod · 0.72
setUpMethod · 0.72
test_get_docstringMethod · 0.72
setUpMethod · 0.72