Skip to main content

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

  1. Use virtual environment to isolate dependencies
  2. Perfect for Move code syntax analysis, highlighting, refactoring tool development
  3. Suitable for integration into IDE plugins or code analysis tools
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree_sitter_move-0.0.1.tar.gz (352.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tree_sitter_move-0.0.1-cp38-abi3-macosx_10_13_universal2.whl (157.8 kB view details)

Uploaded CPython 3.8+macOS 10.13+ universal2 (ARM64, x86-64)

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

Hashes for tree_sitter_move-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8ac4a962d537f40aebcfaa6d03f0f1c35c760e49d992fa286f0a4a373bf76ef3
MD5 8eb1cf059b896e9d2240787a31b1d696
BLAKE2b-256 eca2794da8a01d4ee0945864d6146311d71fad31592c322d87baee4e4d2f3efa

See more details on using hashes here.

File details

Details for the file tree_sitter_move-0.0.1-cp38-abi3-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for tree_sitter_move-0.0.1-cp38-abi3-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 36fc620baaca299c65ca3fc9140f59cd12fe50a52d24054062c2547aa8258a3c
MD5 c33255790f9a4f63fa08db18de579bbc
BLAKE2b-256 07dcc5a62ac23ec7b302a8e3b5ad77e83f0a74db7c29aa93d16960b64c5d6a35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page