Binary Python wheels for all tree sitter languages
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Binary Python wheels for all tree sitter languages, eliminating the need to download and compile support for individual languages. This package provides a comprehensive collection of tree-sitter language parsers that can be easily installed via pip, offering a simple API to access any of the included language parsers without the complexity of individual language setup.
pip install tree_sitter_languagesfrom tree_sitter_languages import get_language, get_parserfrom tree_sitter_languages import get_language, get_parser
# Get a language object for Python
language = get_language('python')
# Get a pre-configured parser for Python
parser = get_parser('python')
# Parse some Python code
code = b"""
def hello():
print("Hello, world!")
"""
tree = parser.parse(code)
root_node = tree.root_node
# Query for function definitions
query = language.query('(function_definition name: (identifier) @func)')
captures = query.captures(root_node)
# Print function names
for node, capture_name in captures:
if capture_name == "func":
print(f"Found function: {node.text.decode()}")Creates a tree-sitter Language object for the specified language, loading the appropriate binary parser from the bundled language binaries.
def get_language(language: str) -> LanguageParameters:
language (str): Language name identifier (one of the 48 supported languages)Returns:
Language: A tree-sitter Language object configured for the specified languageRaises:
Creates a pre-configured tree-sitter Parser object for the specified language, combining language loading and parser setup in one step.
def get_parser(language: str) -> ParserParameters:
language (str): Language name identifier (one of the 48 supported languages)Returns:
Parser: A tree-sitter Parser object pre-configured with the specified languageRaises:
The package includes binary parsers for the following 48 programming languages:
bash - Bash shell scriptsc - C programming languagec_sharp - C# programming language (use 'c_sharp', not 'c-sharp')commonlisp - Common Lispcpp - C++ programming languagecss - Cascading Style Sheetsdockerfile - Docker container filesdot - Graphviz DOT languageelisp - Emacs Lispelixir - Elixir programming languageelm - Elm programming languageembedded_template - Embedded template languages (use 'embedded_template', not 'embedded-template')erlang - Erlang programming languagefixed_form_fortran - Fixed-form Fortranfortran - Modern Fortrango - Go programming languagegomod - Go module files (use 'gomod', not 'go-mod')hack - Hack programming languagehaskell - Haskell programming languagehcl - HashiCorp Configuration Languagehtml - HyperText Markup Languagejava - Java programming languagejavascript - JavaScript programming languagejsdoc - JSDoc documentation commentsjson - JavaScript Object Notationjulia - Julia programming languagekotlin - Kotlin programming languagelua - Lua programming languagemake - Makefile syntaxmarkdown - Markdown markup languageobjc - Objective-C programming languageocaml - OCaml programming languageperl - Perl programming languagephp - PHP programming languagepython - Python programming languageql - CodeQL query languager - R programming languageregex - Regular expressionsrst - reStructuredText markupruby - Ruby programming languagerust - Rust programming languagescala - Scala programming languagesql - SQL database languagesqlite - SQLite-specific SQLtoml - TOML configuration formattsq - Tree-sitter query languagetypescript - TypeScript programming languageyaml - YAML configuration format__version__: str = '1.10.2'
__title__: str = 'tree_sitter_languages'
__author__: str = 'Grant Jenks'
__license__: str = 'Apache 2.0'
__copyright__: str = '2022-2023, Grant Jenks'The functions return standard tree-sitter objects:
# From tree_sitter package (dependency)
class Language:
"""Tree-sitter language parser object"""
def query(self, source: str) -> Query: ...
class Parser:
"""Tree-sitter parser object"""
def parse(self, source: bytes) -> Tree: ...
def set_language(self, language: Language) -> None: ...
class Tree:
"""Parse tree result"""
@property
def root_node(self) -> Node: ...
class Node:
"""Tree node"""
@property
def text(self) -> bytes: ...
@property
def type(self) -> str: ...
class Query:
"""Tree-sitter query object"""
def captures(self, node: Node) -> List[Tuple[Node, str]]: ...from tree_sitter_languages import get_parser
# Parse different file types in a project
parsers = {
'python': get_parser('python'),
'javascript': get_parser('javascript'),
'css': get_parser('css'),
'html': get_parser('html')
}
def analyze_file(file_path, content):
if file_path.endswith('.py'):
tree = parsers['python'].parse(content.encode())
elif file_path.endswith('.js'):
tree = parsers['javascript'].parse(content.encode())
elif file_path.endswith('.css'):
tree = parsers['css'].parse(content.encode())
elif file_path.endswith('.html'):
tree = parsers['html'].parse(content.encode())
else:
return None
return tree.root_nodefrom tree_sitter_languages import get_language, get_parser
# Set up Python parser and language
language = get_language('python')
parser = get_parser('python')
# Parse Python code
python_code = b'''
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
def standalone_function():
calc = Calculator()
return calc.add(1, 2)
'''
tree = parser.parse(python_code)
# Find all method definitions in classes
method_query = language.query('''
(class_definition
body: (block
(function_definition
name: (identifier) @method_name)))
''')
methods = method_query.captures(tree.root_node)
for node, capture_name in methods:
print(f"Method: {node.text.decode()}")
# Find all function calls
call_query = language.query('(call function: (identifier) @func_name)')
calls = call_query.captures(tree.root_node)
for node, capture_name in calls:
print(f"Function call: {node.text.decode()}")Invalid language names will raise exceptions from the underlying tree-sitter library:
from tree_sitter_languages import get_language
try:
# This will raise an exception
language = get_language('invalid_language')
except Exception as e:
print(f"Error: {e}")
# Handle the error appropriatelyThe package handles platform-specific binary loading automatically (.so files on Unix/Linux, .dll files on Windows), so no platform-specific code is needed in your application.