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! +

+
+
+ ); +} + +export default App; +""" + +# Parse the TSX code +try: + tsx_tree = tsx_parser.parse(bytes(tsx_code, "utf8")) +except Exception as e: + print(f"Error parsing TSX code: {e}") + +# Print the root nodes of the syntax trees +print("\nTypeScript root node:", typescript_tree.root_node) +print("\nTSX root node:", tsx_tree.root_node) + +# Check if the root nodes represent programs +assert typescript_tree.root_node.type == 'program', "TypeScript root node does not represent a program" +assert tsx_tree.root_node.type == 'program', "TSX root node does not represent a program" + +# Check if the root nodes have children +assert typescript_tree.root_node.child_count > 0, "TypeScript root node has no children" +assert tsx_tree.root_node.child_count > 0, "TSX root node has no children" + +# Function to recursively print the tree structure +def print_tree(node, indent="", last='updown'): + markers = { + 'updown': '├── ', + 'up': '└── ', + 'down': '│ ', + 'empty': ' ' + } + print(f"{indent}{markers[last]}{node.type}") + indent += markers['down'] if last == 'updown' else markers['empty'] + for i, child in enumerate(node.children): + last = 'up' if i == len(node.children) - 1 else 'updown' + print_tree(child, indent, last) + +# Print the tree structure of the TypeScript code +print("\nTypeScript tree structure:") +print_tree(typescript_tree.root_node) + +# Print the tree structure of the TSX code +print("\nTSX tree structure:") +print_tree(tsx_tree.root_node) + +print("\ntree_sitter_typescript package is working!") \ No newline at end of file