vals.push(3);
{statement}
pest is a general purpose parser written in Rust with a focus on accessibility, correctness, and performance. It uses parsing expression grammars (or PEG) as input, which are similar in spirit to regular expressions, but which offer the enhanced expressivity needed to parse complex languages.
Grammar-generated parsers are both easier to use and maintain than their hand-written counterparts.
Grammars offer better correctness guarantees, and issues can be solved declaratively in the grammar itself. Rust's memory safety further limits the amount of damage bugs can do.
High-level static analysis and careful low-level implementation build a solid foundation on which serious performance tuning is possible.
The following is an example of a grammar for a list of alpha-numeric identifiers where the first identifier does not start with a digit:
alpha = { 'a'..'z' | 'A'..'Z' }
digit = { '0'..'9' }
ident = { (alpha | digit)+ }
ident_list = _{ !digit ~ ident ~ (" " ~ ident)+ }
// ^
// ident_list rule is silent (produces no tokens or error reports)
Grammars are saved in separate .pest
files which are
never mixed with procedural code. This results in an always up-to-date
formalization of a language that is easy to read and maintain.
Based on the grammar definition, the parser also includes automatic
error reporting. For the example above, the input "123"
will result in:
thread 'main' panicked at ' --> 1:1
|
1 | 123
| ^---
|
= unexpected digit', src/main.rs:12
while "ab *"
will result in:
thread 'main' panicked at ' --> 1:1
|
1 | ab *
| ^---
|
= expected ident', src/main.rs:12