Crate rusty_parser
source ·Expand description
§Rusty Parser
A Generic compile-time Parser generator and Pattern Matching Library written in Rust
Define pattern, combine them, and parse the input.
RustyParser provides a set of basic parsers, combinators, and parser-generating functions.
§Example
use rusty_parser as rp;
use rp::IntoParser;
// define pattern
// digit: [0-9]
// this will match one digit, and returns (char,), the character it parsed
let digit_parser = rp::range('0'..='9');
// define pattern
// num: digit+
// this will match one or more digits, and returns (Vec<char>,), the character it parsed
let num_parser = digit_parser.repeat(0..);
// map the output
// ( Vec<char>, ) --> (i32, )
let num_parser = num_parser.map(|(digits,): (Vec<char>,)| -> (i32,) {
let mut num = 0;
for ch in digits {
num = num * 10 + (ch as i32 - '0' as i32);
}
(num,)
});
// parse input iterator with given pattern, and return the result
let res = rp::parse(&num_parser, "123456hello_world".chars());
// res contains the result of parsing
assert_eq!(res.output.unwrap(), (123456,));
// res.it: iterator after parsing
// here, '123456' is parsed, so the rest is "hello_world"
assert_eq!(res.it.collect::<String>(), "hello_world");Those generated parsers are used to parse the input string, and return the extracted data.
parse(...) takes a Pattern Object and iterator of input string, then returns ParseResult<Self::Output, It>.
match_pattern(...) is used
when you only want to check if the pattern is matched or not, without extracting data.
For some parsers, like repeat, it is expensive to call parse(...) to get the output since it invokes Vec::push inside.
§Note
- Since the
parse(...)internally clones the iterator, the iterator must be cheaply clonable. Outputmust beTuple, including(). If you want to return a single value, use(Value,).
Macros§
- A macro for creating or combination of parsers.
- A macro for creating a sequence of parsers.
Structs§
- Dictionary using trie, implementation uses BTreeMap; O(log(N)) search.
- Dictionary using trie, implementation uses HashMap; O(1) search.
- A Box<dyn Parser> wrapper for iterators of
std::str::Chars. - A Box<dyn Parser> wrapper for iterators of
std::iter::Copied<std::slice::Iter>. - struct that holds the result of parsing.
- Rc<Parser> wrapper.
- RefCell<Parser> wrapper.
Traits§
- A trait alias that Input Iterator must hold.
- Trait for converting possible types to Parser.
- Parser trait.
Functions§
- This Parser will compare the input string starts with the given string.
- This Parser will always success and return the clone of given output.
- Parser that success if reached end of input
- This Parser will always fail.
- convert the given type to Parser ( if it impl IntoParser )
- Match the input with the given parser.
- Check one character is equal to the given character.
- A binary or combinator
- Parse the input with the given parser.
- Check one character is in the given range.
- A binary seq combinator
- This Parser will compare the input string starts with the given slice