| 1 | # Parsing Expression Grammars in Rust |
| 2 | |
| 3 | [Documentation](https://docs.rs/peg) | [Release Notes](https://github.com/kevinmehall/rust-peg/releases) |
| 4 | |
| 5 | `rust-peg` is a simple yet flexible parser generator that makes it easy to write robust parsers. Based on the [Parsing Expression Grammar](https://en.wikipedia.org/wiki/Parsing_expression_grammar) formalism, it provides a Rust macro that builds a recursive descent parser from a concise definition of the grammar. |
| 6 | |
| 7 | ## Features |
| 8 | |
| 9 | * Parse input from `&str`, `&[u8]`, `&[T]` or custom types implementing traits |
| 10 | * Customizable reporting of parse errors |
| 11 | * Rules can accept arguments to create reusable rule templates |
| 12 | * Precedence climbing for prefix/postfix/infix expressions |
| 13 | * Helpful `rustc` error messages for errors in the grammar definition or the Rust |
| 14 | code embedded within it |
| 15 | * Rule-level tracing to debug grammars |
| 16 | |
| 17 | ## Example |
| 18 | |
| 19 | Parse a comma-separated list of numbers surrounded by brackets into a `Vec<u32>`: |
| 20 | |
| 21 | ```rust |
| 22 | peg::parser!{ |
| 23 | grammar list_parser() for str { |
| 24 | rule number() -> u32 |
| 25 | = n:$(['0'..='9']+) {? n.parse().or(Err("u32")) } |
| 26 | |
| 27 | pub rule list() -> Vec<u32> |
| 28 | = "[" l:(number() ** ",") "]" { l } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn main() { |
| 33 | assert_eq!(list_parser::list("[1,1,2,3,5,8]"), Ok(vec![1, 1, 2, 3, 5, 8])); |
| 34 | } |
| 35 | ``` |
| 36 | |
| 37 | [See the tests for more examples](./tests/run-pass/) |
| 38 | [Grammar rule syntax reference in rustdoc](https://docs.rs/peg) |
| 39 | |
| 40 | ## Comparison with similar parser generators |
| 41 | |
| 42 | | crate | parser type | action code | integration | input type | precedence climbing | parameterized rules | streaming input | |
| 43 | |----------- |------------- |------------- |-------------------- |------------------------ |--------------------- |-------------------- |----------------- | |
| 44 | | peg | PEG | in grammar | proc macro (block) | `&str`, `&[T]`, custom | Yes | Yes | No | |
| 45 | | [pest] | PEG | external | proc macro (file) | `&str` | Yes | No | No | |
| 46 | | [nom] | combinators | in source | library | `&[u8]`, custom | No | Yes | Yes | |
| 47 | | [lalrpop] | LR(1) | in grammar | build script | `&str` | No | Yes | No | |
| 48 | |
| 49 | [pest]: https://github.com/pest-parser/pest |
| 50 | [nom]: https://github.com/geal/nom |
| 51 | [lalrpop]: https://github.com/lalrpop/lalrpop |
| 52 | |
| 53 | ## See also |
| 54 | |
| 55 | * [pegviz] is a UI for visualizing rust-peg's trace output to debug parsers. |
| 56 | * There exist several crates to format diagnostic messages on source code snippets in the terminal, including [chic], [annotate-snippets], [codespan-reporting], and [codemap-diagnostic]. |
| 57 | |
| 58 | [pegviz]: https://github.com/fasterthanlime/pegviz |
| 59 | [chic]: https://crates.io/crates/chic |
| 60 | [annotate-snippets]: https://crates.io/crates/annotate-snippets |
| 61 | [codespan-reporting]: https://crates.io/crates/codespan-reporting |
| 62 | [codemap-diagnostic]: https://crates.io/crates/codemap-diagnostic |
| 63 | |
| 64 | ## Development |
| 65 | |
| 66 | The `rust-peg` grammar is written in `rust-peg`: `peg-macros/grammar.rustpeg`. To avoid the circular dependency, a precompiled grammar is checked in as `peg-macros/grammar.rs`. To regenerate this, run the `./bootstrap.sh` script. |
| 67 | |
| 68 | There is a large test suite which uses [`trybuild`](https://crates.io/crates/trybuild) to test both functionality (`tests/run-pass`) and error messages for incorrect grammars (`tests/compile-fail`). Because `rustc` error messages change, the `compile-fail` tests are only run on the minimum supported Rust version to avoid spurious failures. |
| 69 | |
| 70 | Use `cargo test` to run the entire suite, |
| 71 | or `cargo test -- trybuild trybuild=lifetimes.rs` to test just the indicated file. |
| 72 | Add `--features trace` to trace these tests. |