Move grammar for tree-sitter
Project description
Tree-sitter Move Parser
A Python library for parsing Aptos Move smart contract language, built on tree-sitter.
โจ Features
- Pure Python Environment: No need to install npm, cargo or other toolchains
- Plug and Play: Includes pre-compiled grammar files, ready to use out of the box
- Complete Functionality: Supports full Move language syntax parsing and analysis
- Easy Integration: Provides clean Python API interface
- Comprehensive Testing: Successfully parses all valid Move code samples
๐ Quick Start
Requirements
- Python 3.8+
- pip
Installation
Option 1: One-click Installation (Recommended)
./setup.sh
Option 2: Manual Installation
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install tree-sitter --trusted-host pypi.org --trusted-host files.pythonhosted.org
# Install this package
pip install -e .
# Run demo
python move_parser_demo.py
Quick Test
# Run basic demo
python move_parser_demo.py
# Run comprehensive tests
python comprehensive_move_test.py
๐ป Usage Example
Basic Usage
import tree_sitter_move as ts_move
from tree_sitter import Language, Parser
# Create parser
move_language = Language(ts_move.language())
parser = Parser()
parser.language = move_language
# Parse Move code
move_code = """
module 0x1::coin {
struct Coin has key, store {
value: u64,
}
public fun mint(value: u64): Coin {
Coin { value }
}
public fun get_value(coin: &Coin): u64 {
coin.value
}
}
"""
tree = parser.parse(bytes(move_code, "utf8"))
# Check parsing result
if tree.root_node.has_error:
print("โ Syntax error")
else:
print("โ
Parsing successful")
print(f"Root node type: {tree.root_node.type}")
print(f"Number of child nodes: {tree.root_node.child_count}")
Advanced AST Analysis
def analyze_ast(node, depth=0):
indent = " " * depth
print(f"{indent}โโ {node.type}")
for child in node.children:
if depth < 3: # Limit depth
analyze_ast(child, depth + 1)
# Analyze the AST structure
analyze_ast(tree.root_node)
๐ Test Results
The parser has been thoroughly tested with various Move code samples:
| File | Size | Lines | Description | Result |
|---|---|---|---|---|
basic_coin.move |
1.9KB | 58 | Basic coin module with mint/transfer functions | โ Pass |
nft_collection.move |
4.0KB | 150 | NFT collection with complex events and vectors | โ Pass |
defi_swap.move |
6.9KB | 203 | DeFi swap module with generics and algorithms | โ Pass |
script_example.move |
770B | 23 | Move script example | โ Pass |
syntax_error.move |
579B | 29 | File with intentional syntax errors | โ Fail (Expected) |
Overall Success Rate: 100% (4/4 valid files, 1 syntax error file correctly detected)
๐ง Supported Move Syntax
โ Successfully Parsed Features:
- Module definitions (
module 0x1::name) - Struct definitions with abilities (
has key, store) - Function definitions with visibility modifiers (
public fun) - Generic type parameters (
<T>,phantom) - References and borrowing (
&,&mut) - Complex expressions and control flow
- Script definitions (
script { }) - Comments and documentation
- Import statements (
use) - Error handling (
assert!,abort)
โ Advanced Features:
- Resource management (
move_to,borrow_global) - Event emission (
event::emit) - Vector operations
- String handling
- Conditional expressions and loops
โ Error Detection:
- Accurately identifies syntax error locations
- Provides detailed error information
- Distinguishes between legal and illegal syntax structures
๐ Project Structure
tree-sitter-move/
โโโ src/ # Pre-compiled grammar files
โ โโโ grammar.json # Grammar rules
โ โโโ parser.c # Parser C code
โ โโโ scanner.c # External scanner
โโโ bindings/python/ # Python bindings
โโโ move_test_files/ # Test Move files
โ โโโ basic_coin.move # Basic coin module
โ โโโ nft_collection.move # NFT collection
โ โโโ defi_swap.move # DeFi swap module
โ โโโ script_example.move # Script example
โโโ move_parser_demo.py # Basic demo script
โโโ comprehensive_move_test.py # Comprehensive test script
โโโ setup.py # Python package configuration
โโโ setup.sh # Installation script
โโโ README.md # This documentation
๐ฒ AST Analysis
The parser generates complete Abstract Syntax Trees (AST) including:
- Root node type identification
- Complete node hierarchy structure
- Detailed node type statistics
- Accurate position information
๐ฏ Use Cases
- Move code syntax analysis
- Smart contract code validation
- IDE syntax highlighting support
- Code formatting tools
- Documentation generation
- Code refactoring tools
- Static analysis tools
- Educational and learning tools
๐ Troubleshooting
SSL Certificate Error
pip install tree-sitter --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org
Import Error: No module named 'tree_sitter_move'
pip install -e .
Virtual Environment Not Activated
source venv/bin/activate
๐ Getting Started Tips
- Use virtual environment to isolate dependencies
- Perfect for Move code syntax analysis, highlighting, refactoring tool development
- Suitable for integration into IDE plugins or code analysis tools
- Tested with Python 3.13 on macOS, compatible with Python 3.8+
๐ License
Apache License 2.0 - See LICENSE file for details
๐ Enjoy using Tree-sitter Move Parser!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tree_sitter_move-0.0.1.tar.gz.
File metadata
- Download URL: tree_sitter_move-0.0.1.tar.gz
- Upload date:
- Size: 352.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ac4a962d537f40aebcfaa6d03f0f1c35c760e49d992fa286f0a4a373bf76ef3
|
|
| MD5 |
8eb1cf059b896e9d2240787a31b1d696
|
|
| BLAKE2b-256 |
eca2794da8a01d4ee0945864d6146311d71fad31592c322d87baee4e4d2f3efa
|
File details
Details for the file tree_sitter_move-0.0.1-cp38-abi3-macosx_10_13_universal2.whl.
File metadata
- Download URL: tree_sitter_move-0.0.1-cp38-abi3-macosx_10_13_universal2.whl
- Upload date:
- Size: 157.8 kB
- Tags: CPython 3.8+, macOS 10.13+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36fc620baaca299c65ca3fc9140f59cd12fe50a52d24054062c2547aa8258a3c
|
|
| MD5 |
c33255790f9a4f63fa08db18de579bbc
|
|
| BLAKE2b-256 |
07dcc5a62ac23ec7b302a8e3b5ad77e83f0a74db7c29aa93d16960b64c5d6a35
|