Created
July 22, 2022 23:23
-
-
Save colinclement/3f8c257668440eb1864de455b9f397a3 to your computer and use it in GitHub Desktop.
Get all Python function calls with Tree Sitter queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from athenacommon.tree_sitter import get_language | |
| from tree_sitter import Parser | |
| parser = Parser() | |
| lang = get_language('python') # get lang whatever way if you lack athenacommon | |
| parser.set_language(lang) | |
| code = bytes("""def foo(qux): | |
| h = boo() | |
| for j in range(10): | |
| qoo(h) | |
| return h | |
| """, "utf-8") | |
| print(code.decode()) | |
| tree = parser.parse(code) | |
| query = lang.query("""((call function: (identifier) arguments: (argument_list)) @function.call)""") | |
| print("function calls:") | |
| function_nodes = query.captures(tree.root_node) | |
| for node, label in function_nodes: | |
| print(code[node.start_byte:node.end_byte].decode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment