3 releases
0.1.2 | Nov 9, 2023 |
---|---|
0.1.1 | Oct 24, 2023 |
0.1.0 | Oct 24, 2023 |
#46 in #peg
Used in pegy
32KB
696 lines
Pegy - derive based Parser in Rust
Features
- derive based parser generation
- async api for parsing
- parse input from str, async readers or custom input types.
- AST generation on the run
- meaningful error messages for errors in grammar definition
- supports no_std
MSRV
The current MSRV supported is 1.75
Example
use pegy::Parse;
#[derive(Debug, Default, Parse)]
#[grammar($item0:['a'-'z''A'-'Z''0'-'9'])]
pub struct AlphaDigit(char);
#[derive(Debug, Default, Parse)]
#[grammar($item0:['0'-'9'])]
pub struct Digit(char);
#[derive(Debug, Default, Parse)]
#[grammar(!Digit $item0:AlphaDigit+)]
pub struct Ident(Vec<AlphaDigit>);
pub fn main(){
let re: pegy::Result<Ident> = pegy::parse_blocking::<Ident, _>("myIdent");
assert!(re.is_ok());
}
Comparison with similar traits
crate | action code | integration | input type | streaming input |
---|---|---|---|---|
pegy | in grammar | proc macro(derive) | &str , AsyncRead , custom |
Yes |
peg | in grammar | proc macro(block) | &str , &[T] , custom |
No |
pest | external | proc macro(file) | &str |
No |
Expression Reference
Term
"some string"
- string literal: matches a str slice. returns&'static str
.'c'
- character literal: matches a character. returnschar
.Ident
- rule: matches a Parse rule. It must be a valid type and imlplementspegy::Parse
. returnsIdent
type.['a'-'z''A'-'Z''$']
- character class: matches a range of characters. returnschar
.
Quantifier
?
- optional: matches zero or one term. returnsOption<T>
*
- repeat: matches zero or more terms. returnsVec<T>
+
- repeat atleast: matches one or more terms. returnsVec<T>
.{min, max}
- repeat range: matches at leastmin
and at mostmax
number of terms. returnsVec<T>
Special
$ident:term
- field binding: bind the result of the term to the fieldident
of result. returns()
.( alternatives )
- group: matches the terms and returns aSpan
.terms | terms | terms
- alternatives: trys to match the first terms, if failed, matches the second one and so on until a match is found. returns aSpan
.!term
- negative lookahead: matches the term without consuming any characters._ term
- quiet: matches the term and returns()
.
Dependencies
~235–680KB
~16K SLoC