#parser #grammar #peg #parser-generator

camxes-rs

A Parsing Expression Grammar (PEG) parser generator with zero-copy parsing and rich debugging capabilities

2 releases

new 0.1.1 Feb 8, 2025
0.1.0 Feb 2, 2025

#48 in Parser tooling

Download history 72/week @ 2025-01-27 128/week @ 2025-02-03

200 downloads per month

MIT license

47KB
1K SLoC

🚀 Rust PEG Parser Generator

Crates.io Documentation License

A lightning-fast Parsing Expression Grammar (PEG) parser generator implemented in Rust. This tool helps you create parsers from grammar definitions with minimal hassle.

📚 Documentation

Full API documentation is available on docs.rs

✨ Features

  • Zero-Copy Parsing: Efficient parsing without unnecessary string allocations
  • Rich Debugging: Detailed logging for grammar validation and parsing process
  • Flexible Grammar Syntax: Supports all standard PEG operators and extensions
  • Error Recovery: Robust error handling with detailed diagnostic messages
  • Thread-Safe: Designed for concurrent use with thread-safe grammar structures

📦 Installation

Add this to your Cargo.toml:

[dependencies]
camxes-rs = "0.1.1"

🚦 Quick Start

Local testing

You must have latest Rust installed.

Go to the project directory and run cargo run --example cmaxes-test. It will run the PEG grammar specified in src/examples/cmaxes-test.rs file against a sample input.

🔧 Grammar Syntax

The grammar supports these PEG operators:

Operator Description Example
<- Definition rule <- expression
/ Ordered choice a / b
* Zero or more [0-9]*
+ One or more [a-z]+
? Optional [A-Z]?
& And-predicate &[a-z]
! Not-predicate ![0-9]
() Grouping (a / b)
[] Character range / class [abd] and [a-zA-Z]
. Any character .

🔍 Debugging

Enable debug logging to see detailed parsing information:

// Initialize logging (using env_logger)
env_logger::builder()
    .filter_level(log::LevelFilter::Debug)
    .init();

Or set the environment variable:

RUST_LOG=debug cargo run

📚 Examples

Simple Calculator Grammar

let calc_grammar = (
    "calc",
    r#"
    calc <- additive
    additive <- multiplicative (('+' / '-') multiplicative)*
    multiplicative <- primary (('*' / '/') primary)*
    primary <- number / '(' additive ')'
    number <- [0-9]+ 
    "#
);

JSON Parser Grammar

let json_grammar = (
    "json",
    r#"
    json <- spacing value spacing
    value <- object / array / string / number / true / false / null
    object <- '{' spacing (pair (',' pair)*)? '}' 
    pair <- string spacing ':' spacing value
    array <- '[' spacing (value (',' value)*)? ']'
    string <- '"' (!'"' .)* '"'
    number <- '-'? ([0-9] / [1-9][0-9]*) ('.' [0-9]+)? ([eE][-+]?[0-9]+)?
    true <- 'true'
    false <- 'false'
    null <- 'null'
    spacing <- [ \t\n\r]*
    "#
);

🤝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments


Built with ❤️ for the Rust community

Dependencies

~86KB