2 releases
new 0.1.1 | Feb 8, 2025 |
---|---|
0.1.0 | Feb 2, 2025 |
#48 in Parser tooling
200 downloads per month
47KB
1K
SLoC
🚀 Rust PEG Parser Generator
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:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Based on Bryan Ford's Parsing Expression Grammars paper
- Inspired by various PEG implementations in the Rust ecosystem
Built with ❤️ for the Rust community
Dependencies
~86KB