Skip to main content

Binary Python wheels for all tree sitter languages.

Project description

Binary Python wheels for all tree sitter languages.

This is a fork of py-tree-sitter-languages which adds support for Python 3.13.

Note that it does not add support for the newer tree-sitter versions - this is primarily intended to ensure Textual applications can use syntax highlighting on modern Python versions.

py-tree-sitter is a fantastic library that provides Python bindings for the even more fantastic tree-sitter parsing library.

py-tree-sitter-languages provides binary Python wheels for all tree sitter languages. The binary wheels remove the need to download and compile support for individual languages.

Install

pip install tree_sitter_languages

Source installs are not supported. To see how the binary wheels are built, look at:

  1. setup.py — Python package setup.

  2. repos.txt — Text file that contains a list of included language repositories and their commit hashes.

  3. build.py — Python script to download and build the language repositories.

  4. .github/workflows/release.yml — GitHub action to invoke cibuildwheel and release to PyPI.

Usage

from tree_sitter_languages import get_language, get_parser

language = get_language('python')
parser = get_parser('python')

That’s the whole API!

Refer to py-tree-sitter for the language and parser API. Notice the Language.build_library(...) step can be skipped! The binary wheel includes the language binary.

Demo

Want to know something crazy? Python lacks multi-line comments. Whhaaa!?!

It’s really not such a big deal. Instead of writing

"""
My awesome
multi-line
comment.
"""

Simply write

# My awesome
# multi-line
# comment.

So multi-line comments are made by putting multiple single-line comments in sequence. Amazing!

Now, how to find all the strings being used as comments?

Start with some example Python code

example = """
#!shebang
# License blah blah (Apache 2.0)
"This is a module docstring."

a = 1

'''This
is
not
a
multiline
comment.'''

b = 2

class Test:
    "This is a class docstring."

    'This is bogus.'

    def test(self):
        "This is a function docstring."

        "Please, no."

        return 1

c = 3
"""

Notice a couple things:

  1. Python has module, class, and function docstrings that bare a striking resemblance to the phony string comments.

  2. Python supports single-quoted, double-quoted, triple-single-quoted, and triple-double-quoted strings (not to mention prefixes for raw strings, unicode strings, and more).

Creating a regular expression to capture the phony string comments would be exceedingly difficult!

Enter tree-sitter

from tree_sitter_languages import get_language, get_parser

language = get_language('python')
parser = get_parser('python')

Tree-sitter creates an abstract syntax tree (actually, a concrete syntax tree) and supports queries

tree = parser.parse(example.encode())
node = tree.root_node
print(node.sexp())

Look for statements that are a single string expression

stmt_str_pattern = '(expression_statement (string)) @stmt_str'
stmt_str_query = language.query(stmt_str_pattern)
stmt_strs = stmt_str_query.captures(node)
stmt_str_points = set(
    (node.start_point, node.end_point) for node, _ in stmt_strs
)
print(stmt_str_points)

Now, find those statement string expressions that are actually module, class, or function docstrings

doc_str_pattern = """
    (module . (comment)* . (expression_statement (string)) @module_doc_str)

    (class_definition
        body: (block . (expression_statement (string)) @class_doc_str))

    (function_definition
        body: (block . (expression_statement (string)) @function_doc_str))
"""
doc_str_query = language.query(doc_str_pattern)
doc_strs = doc_str_query.captures(node)
doc_str_points = set(
    (node.start_point, node.end_point) for node, _ in doc_strs
)

With the set of string expression statements and the set of docstring statements, the locations of all phony string comments is

comment_strs = stmt_str_points - doc_str_points
print(sorted(comment_strs))

License

Copyright 2022-2023 Grant Jenks

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

The project also includes the following other projects distributed in binary form:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textual_tree_sitter_languages-0.0.0a0.tar.gz (51.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textual_tree_sitter_languages-0.0.0a0-cp311-cp311-macosx_13_0_arm64.whl (23.4 kB view details)

Uploaded CPython 3.11macOS 13.0+ ARM64

File details

Details for the file textual_tree_sitter_languages-0.0.0a0.tar.gz.

File metadata

File hashes

Hashes for textual_tree_sitter_languages-0.0.0a0.tar.gz
Algorithm Hash digest
SHA256 c56d04f899fe4da77901f403ef123915512abfbf632ff4488bde831562f9cee6
MD5 b5c49e441445fc86e04d6211c4f44212
BLAKE2b-256 8a631cf9f4f77e09bb1c28aef51760a5e5b4459184fd84a1f4afb6b16f22e3c5

See more details on using hashes here.

File details

Details for the file textual_tree_sitter_languages-0.0.0a0-cp311-cp311-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for textual_tree_sitter_languages-0.0.0a0-cp311-cp311-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 b02e526bfcb1e4ff887eed8c9f6dafb44836175a1f154b20f5d376f2fd3e87b5
MD5 24198ac2f85d3ff7cf4781a4352598a5
BLAKE2b-256 9ed8e3ace526666272c4e8cabcd86d7e0919726b57da2439bc4d5f70c9503d43

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page