p-arse 0.0.1 • the inelegant parser
p-arse is a PEG parser library focused on readability and type safety • it follows the syntax from the original paper as closely as possible • the parsers are point-free (they're (mostly) variables, not functions), as opposed to nom's parsers which are functions or compositions of functions • this encourages the user to bind and name many intermediate parsers • it is similar to pest in this regard
examples
hex color
let parse_hex_dd = |s: &str| {
u8::from_str_radix(s, 16).unwrap()
};
let construct_color = |(r, g, b)| Color { r, g, b };
let hex_d = ('0'.to('9')).or('a'.to('f'));
let hex_dd = (hex_d, hex_d).maps(parse_hex_dd);
let color = ("#", hex_dd, hex_dd, hex_dd).r0().map(construct_color);
let (color, _tail) = color.p_arse("#defec8").unwrap();
FASTA (only recognition)
let nl = '\
';
let header_content = (nl.not_ahead(), any()).more();
let header_tag = ">";
let header = (header_tag, header_content, nl);
let sequence_char = ('A'.to('Z')).or('*').or('-');
let subsequence = sequence_char.more();
let sequence =
(subsequence, (nl, subsequence).zore(), nl.opt());
let entry = (header, sequence);
let file = (entry.zore(), eoi());
i've also replicated some examples from the other parser libaries, i.e. nom's hex color (mine), pest's ident list (mine) and pom's json (mine)
supported parsers
- string and char literals, char ranges
- sequences (tuple)
- ordered choice (alternative)
- repetitions
- optionals
- positive and negative look-aheads
- recursive non-terminals
- end of input and any