Skip to content

Instantly share code, notes, and snippets.

@colinclement
Created July 22, 2022 23:23
Show Gist options
  • Select an option

  • Save colinclement/3f8c257668440eb1864de455b9f397a3 to your computer and use it in GitHub Desktop.

Select an option

Save colinclement/3f8c257668440eb1864de455b9f397a3 to your computer and use it in GitHub Desktop.
Get all Python function calls with Tree Sitter queries
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