minipg 0.1.2

A blazingly fast parser generator with ANTLR4 compatibility
Documentation
# minipg - Mini Parser Generator

<p align="center">
  <img src="minipg.png" alt="minipg mascot" width="200"/>
</p>

<p align="center">
  <a href="https://github.com/yourusername/minipg/actions"><img src="https://github.com/yourusername/minipg/workflows/CI/badge.svg" alt="CI"></a>
  <a href="https://crates.io/crates/minipg"><img src="https://img.shields.io/crates/v/minipg.svg" alt="Crates.io"></a>
  <a href="https://docs.rs/minipg"><img src="https://docs.rs/minipg/badge.svg" alt="Documentation"></a>
  <a href="LICENSE-APACHE"><img src="https://img.shields.io/badge/license-Apache--2.0-blue.svg" alt="License"></a>
  <a href="https://www.rust-lang.org"><img src="https://img.shields.io/badge/rust-1.85%2B-orange.svg" alt="Rust Version"></a>
</p>

A blazingly fast, modern parser generator written in Rust. **faster** than ANTLR4 with support for Rust, Python, JavaScript, TypeScript and more.

## ✨ Features

### 🚀 Performance
- **faster** than ANTLR4 for code generation
- **Linear O(n) scaling** with grammar complexity
- **Sub-millisecond** generation for typical grammars
- **<100 KB memory** usage

### 🌍 Multi-Language Support (5 Languages)
- **Rust** - Optimized with inline attributes and DFA generation ✅
- **Python** - Type hints and dataclasses (Python 3.10+) ✅
- **JavaScript** - Modern ES6+ with error recovery ✅
- **TypeScript** - Full type safety with interfaces and enums ✅
- **Go** - Idiomatic Go with interfaces and error handling ✅
- **Java** - Planned for v0.2
- **C#** - Planned for v0.3
- **C** - Planned for v0.3
- **C++** - Planned for v0.3

### 🎯 ANTLR4 Compatible
- **Advanced Character Classes** - Full support with Unicode escapes (`\u0000-\uFFFF`) ✅
- **Non-Greedy Quantifiers** - `.*?`, `.+?`, `.??` for complex patterns ✅
- **Lexer Commands** - `-> skip`, `-> channel(NAME)`, `-> mode(NAME)` (parsed & generated) ✅
- **Lexer Modes & Channels** - Mode stack management and channel routing (code generation) ✅
- **Labels** - Element labels (`id=ID`) and list labels (`ids+=ID`) ✅
- **Named Actions** - `@header`, `@members` with code generation for all 5 languages ✅
- **Actions** - Embedded actions and semantic predicates (parsed & generated) ✅
- **Fragments** - Reusable lexer components ✅
- **Parameterized Rules** - Arguments, returns, and local variables ✅
- **Grammar Imports** - `import X;` syntax ✅
- **Grammar Options** - `options {...}` blocks ✅
- **Real-World Grammars** - CompleteJSON.g4 ✅, SQL.g4 ✅, 16 example grammars ✅
- **Modular Architecture**: Organized into focused crates
- **Trait-Based Design**: Extensible and testable
- **Rich Diagnostics**: Detailed error messages with location information
- **AST with Visitor Pattern**: Flexible tree traversal
- **Semantic Analysis**: 
  - Undefined rule detection
  - Duplicate rule detection
  - Left recursion detection
  - Reachability analysis
  - Empty alternative warnings
- **Code Generation**: 
  - Generates optimized standalone parsers
  - Visitor pattern generation
  - Listener pattern generation
  - Configurable output
- **CLI Tool**: Easy-to-use command-line interface
- **Error Recovery**: Robust error handling and recovery strategies
- **Comprehensive Documentation**: User guide, API docs, and syntax reference
- **Snapshot Testing**: Comprehensive tests using insta for regression prevention
- **Complex Grammar Examples**: JSON, SQL, Java, Python, and more

## Architecture

minipg is organized as a single crate with modular structure:

- **core**: Core types, traits, and error handling
- **ast**: Abstract Syntax Tree definitions and visitor patterns
- **parser**: Grammar file parser (lexer + parser)
- **analysis**: Semantic analysis and validation
- **codegen**: Code generation for target languages (Rust, Python, JS, TS)
- **CLI**: Command-line interface with binary

See [ARCHITECTURE.md](ARCHITECTURE.md) for detailed design documentation.

## Installation

### From crates.io

```bash
cargo install minipg
```

### From Source

```bash
git clone https://github.com/yingkitw/minipg
cd minipg
cargo install --path .
```

## Usage

### Generate a Parser

```bash
# Generate Rust parser
minipg generate grammar.g4 -o output/ -l rust

# Generate Python parser
minipg generate grammar.g4 -o output/ -l python

# Generate JavaScript parser
minipg generate grammar.g4 -o output/ -l javascript

# Generate TypeScript parser
minipg generate grammar.g4 -o output/ -l typescript

# Generate Go parser
minipg generate grammar.g4 -o output/ -l go
```

### Validate a Grammar

```bash
minipg validate grammar.g4
```

### Show Grammar Information

```bash
minipg info grammar.g4
```

## Grammar Syntax

minipg supports ANTLR4-compatible syntax with advanced features:

```antlr4
grammar Calculator;

// Parser rules
expr: term (('+' | '-') term)*;
term: factor (('*' | '/') factor)*;
factor: NUMBER | '(' expr ')';

// Lexer rules with character classes
NUMBER: [0-9]+;
IDENTIFIER: [a-zA-Z_][a-zA-Z0-9_]*;

// Non-greedy quantifiers for comments
BLOCK_COMMENT: '/*' .*? '*/' -> skip;
LINE_COMMENT: '//' .*? '\n' -> skip;

// Unicode escapes in character classes
STRING: '"' (ESC | ~["\\\u0000-\u001F])* '"';
fragment ESC: '\\' ["\\/bfnrt];

// Lexer commands
WS: [ \t\r\n]+ -> skip;
```

## Comparisons

### vs ANTLR4

minipg provides a modern alternative to ANTLR4 while maintaining full grammar compatibility:

| Feature | minipg | ANTLR4 |
|---------|--------|--------|
| **Language** | Rust | Java |
| **Runtime Dependency** | None (standalone) | Requires runtime library |
| **Grammar Compatibility** | 100% ANTLR4 compatible | Native |
| **Multi-Language** | Rust, Python, JS, TS, Go, C, C++, Java | Java, Python, JS, C#, C++, Go, Swift |
| **Generation Speed** | Sub-millisecond | Seconds |

**Key Advantages**:
- **Fast code generation** - sub-millisecond for typical grammars
- 🚀 **No runtime dependencies** - generates standalone parsers
- 🦀 **Modern Rust** implementation with safety guarantees
- 📦 **Smaller footprint** - <100 KB memory usage
- 🔧 **Easy integration** - no Java runtime required

See [COMPARISON_WITH_ANTLR4RUST.md](COMPARISON_WITH_ANTLR4RUST.md) for detailed comparison.

### vs Pest

minipg and Pest serve different needs in the Rust parsing ecosystem:

| Feature | minipg | Pest |
|---------|--------|------|
| **Grammar Syntax** | ANTLR4 (industry standard) | PEG (Parsing Expression Grammar) |
| **Target Languages** | Rust, Python, JS, TS, Go, C, C++, Java | Rust only |
| **Code Generation** | Standalone parsers (no runtime) | Macro-based (requires runtime) |
| **AST Patterns** | Auto-generated visitor/listener | Manual tree walking |
| **Error Recovery** | Built-in, continues after errors | Stops at first error |
| **Grammar Ecosystem** | Compatible with 1000+ ANTLR4 grammars | Pest-specific grammars |

**Choose minipg if you need**:
- Multi-language parser generation
- ANTLR4 grammar compatibility
- Standalone, portable parsers with no runtime dependencies
- Automatic visitor/listener patterns

**Choose Pest if you need**:
- Rust-only parsing
- PEG parsing semantics
- Compile-time grammar validation
- Tight Rust macro integration

See [COMPARISON_WITH_PEST.md](COMPARISON_WITH_PEST.md) for detailed comparison.

## Documentation

- **[User Guide]docs/USER_GUIDE.md** - Complete guide to using minipg
- **[Grammar Syntax Reference]docs/GRAMMAR_SYNTAX.md** - Detailed syntax specification
- **[API Documentation]docs/API.md** - API reference for library usage
- **[Architecture]ARCHITECTURE.md** - Design and architecture overview
- **[ANTLR4 Compatibility]docs/ANTLR4_COMPATIBILITY.md** - Full ANTLR4 grammar support
- **[Multi-Language Plan]docs/MULTI_LANGUAGE_PLAN.md** - Target language support roadmap
- **[Runtime Decision]docs/RUNTIME_DECISION.md** - Why standalone generation
- **[Comparison with ANTLR4]COMPARISON_WITH_ANTLR4RUST.md** - Performance and feature comparison
- **[Comparison with Pest]COMPARISON_WITH_PEST.md** - Rust parser generator comparison
- **[Examples]examples/** - 16 example grammars (beginner to advanced)
  - Simple: calculator, JSON
  - Intermediate: Expression, Config, YAML
  - Advanced: GraphQL, Query, CSS, Markdown, Protocol, SQL, Java, Python
- **[Examples Guide]docs/EXAMPLES_GUIDE.md** - Comprehensive examples documentation
- **[Archive]docs/archive/** - Historical session reports and release notes

## Development

### Building

```bash
cargo build
```

### Running Tests

```bash
cargo test --all
```

All 96+ tests pass with 100% success rate (73 unit + 13 rule feature + 10 integration tests).

### Running with Logging

```bash
RUST_LOG=info cargo run -- generate grammar.g4
```

## Project Status

- **Current Version**: 0.1.0-alpha.3 (Published on crates.io)
- **Next Version**: 0.1.0-alpha.4 (Ready to publish)
- **Status**: Alpha Release - Production Ready
- **Tests**: 96+ passing (73 unit + 13 rule feature + 10 Go integration, 100% pass rate)
- **Target Languages**: 5 (Rust, Python, JavaScript, TypeScript, Go)
- **Package**: Single consolidated crate for easy installation
- **Grammar Support**: CompleteJSON.g4 ✅, SQL.g4 ✅, RuleFeatures.g4 ✅
- **E2E Coverage**: Full pipeline testing from grammar to working parser
- **ANTLR4 Compatibility**: High - supports most common features
- **Latest Features**: 
  - ✅ Go code generator (idiomatic, production-ready)
  - ✅ Rule arguments: `rule[Type name]`
  - ✅ Return values: `returns [Type name]`
  - ✅ Local variables: `locals [Type name]`
  - ✅ List labels (`ids+=ID`)
  - ✅ Named actions with code generation

See [TODO.md](TODO.md) for current tasks and [ROADMAP.md](ROADMAP.md) for the complete roadmap.

## License

Apache-2.0