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.
source: https://github.com/BradMoonUESTC/tree-sitter-move
✨ 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!
🎉 打包完成!您的tree-sitter-move包已经成功构建
✅ 完成的工作
-
包配置完善
- 更新了
pyproject.toml配置文件 - 添加了作者信息、依赖关系、项目URL
- 创建了
MANIFEST.in文件管理包文件
- 更新了
-
成功构建了两种分发包
- Wheel包 (154KB):
tree_sitter_move-0.0.1-cp38-abi3-macosx_10_13_universal2.whl - 源码包 (344KB):
tree_sitter_move-0.0.1.tar.gz
- Wheel包 (154KB):
-
质量验证通过
- ✅
twine check验证通过 - ✅ 独立环境安装测试通过
- ✅ 功能测试通过(可以正常解析Move代码)
- ✅
🚀 如何使用您的包
方法1: 本地安装(推荐)
<code_block_to_apply_from>
方法2: 发布到PyPI
# 上传到Test PyPI(测试用)
twine upload --repository testpypi dist/*
# 上传到PyPI(正式发布)
twine upload dist/*
方法3: 从源码安装
# 开发模式安装
pip install -e .
📋 包信息
- 包名:
tree-sitter-move - 版本:
0.0.1 - 支持Python: 3.8+
- 依赖:
tree-sitter>=0.21.0 - 许可证: Apache License 2.0
🧪 使用示例
import tree_sitter_move as ts_move
from tree_sitter import Language, Parser
# 创建解析器
move_language = Language(ts_move.language())
parser = Parser(move_language)
# 解析Move代码
code = "module 0x1::hello { public fun world() {} }"
tree = parser.parse(bytes(code, "utf8"))
print("✅ 解析成功!" if not tree.root_node.has_error else "❌ 解析失败")
📖 完整文档
详细的安装和使用指南已保存在 PACKAGING_GUIDE.md 文件中。
现在您的tree-sitter-move包已经可以:
- ✅ 本地安装使用
- ✅ 分发给其他用户
- ✅ 上传到PyPI供全球用户使用
🎊 恭喜!您已经成功创建了一个可分发的Python包!
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.2.tar.gz.
File metadata
- Download URL: tree_sitter_move-0.0.2.tar.gz
- Upload date:
- Size: 354.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62d7a979b1f464bbbf6c3bbd39e01bfdfe51c6ba3c889fd4824cc505b4ed628a
|
|
| MD5 |
baa0a7ce1f92c31a0f4554f836161e50
|
|
| BLAKE2b-256 |
88e478fff98742190d2d3640308f79bd3f27f35b1914d0c4f8d7835758d98d60
|
File details
Details for the file tree_sitter_move-0.0.2-cp38-abi3-macosx_10_13_universal2.whl.
File metadata
- Download URL: tree_sitter_move-0.0.2-cp38-abi3-macosx_10_13_universal2.whl
- Upload date:
- Size: 158.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 |
2149de4dea9f168f5dbb8c714928d3a5fad4215bfd172f9fe0e3c455307ff47c
|
|
| MD5 |
821485e658d9410fc8ddd85a1b2d6dfb
|
|
| BLAKE2b-256 |
155ce472b8a8023f00e7c6596a6b6c210d6e7622ba14c967185cba4a5260e7e2
|