Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python usage instructions to README and create test.py for installation verification #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,46 @@ For Javascript files with [flow] type annotations you can use the `tsx` parser.
[tree-sitter]: https://github.com/tree-sitter/tree-sitter
[flow]: https://flow.org/en/

References
## Python usage

The `tree-sitter` library is required for this package. Ensure you have `pip` installed.

```console
pip install tree-sitter
```

First, install the package using pip:
```console
pip install .
```

This package allows you to load both TypeScript and TSX grammars as a Language object in Python. Here's how you can do it:

```python
import tree_sitter_typescript as tstypescript
from tree_sitter import Language, Parser

# Load TypeScript grammar
TYPESCRIPT_LANGUAGE = Language(tstypescript.language_typescript())

# Load TSX grammar
TSX_LANGUAGE = Language(tstypescript.language_tsx())
```

### Practical Example
For a practical example of how to use these grammars with `tree-sitter` python library, please refer to the test file located at [`bindings/python/tree_sitter_typescript/test.py`](bindings/python/tree_sitter_typescript/test.py).

You can test a successful installation by running the following command:

```console
python bindings/python/tree_sitter_typescript/test.py
```
This will execute the test script, which demonstrates how to parse TypeScript and TSX code using the tree-sitter library.

## References

- [TypeScript Language Spec](https://github.com/microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md)
- [tree-sitter Documentation](https://tree-sitter.github.io/tree-sitter/)

[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/tree-sitter-typescript/ci.yml?logo=github&label=CI
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
Expand Down
88 changes: 88 additions & 0 deletions bindings/python/tree_sitter_typescript/test.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.