diff --git a/README.md b/README.md index 4dcf2fe0..c00da240 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bindings/python/tree_sitter_typescript/test.py b/bindings/python/tree_sitter_typescript/test.py new file mode 100644 index 00000000..98cf268b --- /dev/null +++ b/bindings/python/tree_sitter_typescript/test.py @@ -0,0 +1,88 @@ +import tree_sitter_typescript as tstypescript +from tree_sitter import Language, Parser + +# Load TypeScript and TSX grammars +TYPESCRIPT_LANGUAGE = Language(tstypescript.language_typescript()) +TSX_LANGUAGE = Language(tstypescript.language_tsx()) + +# Create a parser for TypeScript +typescript_parser = Parser() +typescript_parser.language = TYPESCRIPT_LANGUAGE + +# Create a parser for TSX +tsx_parser = Parser() +tsx_parser.language = TSX_LANGUAGE + +# Create a simple TypeScript code snippet +typescript_code = """ +function add(a: number, b: number): number { + return a + b; +} +""" + +# Parse the TypeScript code +try: + typescript_tree = typescript_parser.parse(bytes(typescript_code, "utf8")) +except Exception as e: + print(f"Error parsing TypeScript code: {e}") + +# Create a simple TSX code snippet +tsx_code = """ +import React from 'react'; + +function App() { + return ( +
+ Hello, world! +
+