Skip to main content

Binary Python wheels for all tree sitter languages.

Project description

Fork of grantjenks/py-tree-sitter-languages.

Binary Python wheels for all tree sitter languages.

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 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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-musllinux_1_1_x86_64.whl (5.9 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-musllinux_1_1_i686.whl (6.1 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (5.4 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-macosx_11_0_arm64.whl (6.1 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-macosx_10_9_x86_64.whl (5.6 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-musllinux_1_1_x86_64.whl (5.9 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-musllinux_1_1_i686.whl (6.1 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (5.4 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-macosx_11_0_arm64.whl (6.1 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-macosx_10_9_x86_64.whl (5.6 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 a7acdd9f5b63202db18e9fbf813a64344c7c30db9065666cea6cdec5faa975d3
MD5 bf84d738b0374126fb8cd63ab39549bd
BLAKE2b-256 656634ea4a7726660a455de6aca904e7d92b705b4101960df90870945bf587eb

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 476ff89c536f028a2537a7947250aab1a9924f9a8a4ea9f00f185da2ad38b980
MD5 0db407bf0dd3a8ce8070c79f07c19e62
BLAKE2b-256 c5441a7f996c5e286884220612159f388ede291d087145ce22ff71e8d54d1241

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 33f7fb86cfb4f9d0007309bcc6d296c78462a144fb3c59034b7bdb49d45c4ccd
MD5 67026613b6156732551c77d96c8a9bbf
BLAKE2b-256 86dce0c534d69e8e34a0480ec2d075bf235a4ad96d4c1f1a6b1da28133838cd8

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 bd622108a4cad6549d8a8bd6138b0fca5d4302713795bbcb8f08b790fbe2bf8b
MD5 18d4da4267c8ffb87d8ac342dd593dab
BLAKE2b-256 ecf86a6290a9b8408e3c26258b74d76412e97ee4ec90f7e69dfbcae58b17c6f5

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bbd8a49718434a77bd3149727ffdaae6ec23493572597df9b9cb59df8e91bfce
MD5 f4e8fdb11c6809cc389daeaa23dd920d
BLAKE2b-256 9b2307755eea2cff3f02dc3303974f85e068c0557558fb6eb021a18be2c5fd6d

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 910608142d8c5d0385eb23ba7a80c1b4410512a102266344ff42d3b7dd381795
MD5 2d0b55c26f062bc11bd125222b1d21dd
BLAKE2b-256 fa8fa4ff851b97d05a723cf8caf7386dfac1efd4bdb27525e372869572268bd0

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6fae43b37a930dd468affe19cda1cba03c4fa50d7623854b8f28a2bb2ea2ef83
MD5 e889b0d299920423b7faeaeebba5f05b
BLAKE2b-256 794c996cdc4ad1d92fc27a295d3e23f3fb4e9747aa69b60d0c4fb393211db044

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 128ddaaa8af5ed772bf76c5431a37107b8a421d2a4e19790d36ea3530c7cedf7
MD5 5182d10927e67f2f6ab7370bb30fdcbb
BLAKE2b-256 fab8e082e058ffd8b2aeca976424fb946d194b3b066f2647dee106b048716f3d

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d091d156b93bb9225f4e3b3eccf1f6b1dc0ce62cbe234a0abe4c5a4f4ffe2e28
MD5 b49b5a328b2e2ebb15b6e253b590d88d
BLAKE2b-256 6c240fcbde07b120942784340c3108a014b6e348e148afd72fc45521a33d5d9b

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 8d45a9dc2fe36fb16cd31929b684346ac9810218d8c547640236d04df09acc61
MD5 8a942d4ae46e9d784426919608cc2417
BLAKE2b-256 c2bf52b4e6b0103a041261497e5ac1695ebdd3b94c62c98bd5c2be5403eecb90

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9bcdaccdafe523a92df673114a5713e49152379206d34c909ec083ec1ff1f12a
MD5 77361cb5bf69c6ee720a1172ee8bac15
BLAKE2b-256 8196f299f2a9a413015bde6ae9c42baf7cab0ad78a3a289b8a1bcab0314b6b85

See more details on using hashes here.

File details

Details for the file pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pantoufle_tree_sitter_languages-1.6.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 606686f527be49c10bc457137426df9e0e3010ba95ba833f7d3116fc2fb957fa
MD5 824b231b67b24398240e2d910516134d
BLAKE2b-256 6cc51bea5840356ab1898f608bdc51cb25ba8a4c40e122e5898f6fa8cd7c5440

See more details on using hashes here.

Supported by

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