28 releases (7 breaking)
0.8.5 | Sep 18, 2024 |
---|---|
0.7.2 | Sep 13, 2024 |
0.6.1 | Jun 6, 2024 |
0.2.5 | Mar 26, 2024 |
#1321 in Rust patterns
1,496 downloads per month
Used in roped
27KB
705 lines
Parsr
A simple libary for creating new parsing systems as well as a few simple parsers for common use cases with support for custom contexts (such as trimming by arbitrary characters).
Usage
Trimming:
use parsr::prelude::*;
use trim_parser::TrimParser;
use white_space::WhiteSpace;
let input = " hello";
let trimmed = input
.parse_to_with::<TrimParser>(&WhiteSpace)
.map(|t| t.input()); // the 'into_output()' is the unit struct '()'
assert_eq!(trimmed, Ok(Some("hello")));
Segmenting:
use parsr::*;
use segment_parser::SegmentParser;
use white_space::WhiteSpace;
let input = " \n hello world \t !!! \r \n";
let vec = input
.parse_iter_with::<SegmentParser>(&WhiteSpace)
.map(|t| t.get())
.collect::<Vec<_>>();
assert_eq!(vec, vec!["hello", "world", "!!!"]);
Functionality
Core traits:
ParseFrom
: parsing an object from an inputParseFromWith
: parsing an object from an input and a reference to contextParseFromWithMut
: parsing an object from an input and a mutable reference to context
Iterators:
ParseIter
: iterator that calls ParseFrom until there is an error or the input is emptyParseIterWith
: iterator that calls ParseFromWith until there is an error or the input is emptyParseIterWithMut
: iterator that calls ParseFromWithMut until there is an error or the input is empty
Parsers:
TrimParser
: trims an inputUntilParser
: reads to the output until a match is found (trim but it saves the trimmed part as an output)SegmentParser
: an application ofTrimParser
followed byUntilParser
ExpectParser
: like trim except with more versatile auto derives and containers
Matchers:
MatchOver
: a trait that allows you to match over an input for 'TrimParser'FindMatch
: a trait that allows you to find a match in an input for 'UntilParser'- Combined they allow a matcher to be used in a 'SegmentParser'
Dependencies
~7KB