Expand description
§Rusty Parser
A Generic compile-time Parser generator and Pattern Matching Library written in Rust
RustyParser provides a set of basic parsers, combinators, and parser-generating functions.
This library is designed to work with general iterators, but some functionalities are limited to specific iterators.
§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(1..);
// 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.
crate::parse()
takes a Pattern Object and iterator of input string, then returns crate::ParseResult
.
crate::match_pattern()
can be used
when you only want to check if the pattern is matched or not, without extracting data.
For some parsers, like IntoParser::repeat
, it is expensive to call crate::parse()
to get the output since it invokes Vec::push
inside.
§Note
- Since the
crate::parse()
internally clones the iterator, the iterator must be cheaply clonable. Output
must beTuple
, including()
. If you want to return a single value, use(Value,)
.
§Parsers Overview
§Basic(Leaf) Parsers
Parser | Description | Output |
---|---|---|
crate::one , crate::one_by | Match one charactor | (Iterator::Item,) |
crate::range | Match one charactor in the range | (Iterator::Item,) |
crate::str , crate::str_by , crate::slice , crate::slice_by | Match multiple charactors | () |
crate::string , crate::string_by , crate::vec , crate::vec_by | Match multiple charactors | () |
crate::check | Check one charactor with closure | (T,) |
crate::any | Match any charactor | (Iterator::Item,) |
crate::DictBTree , crate::DictHashMap | Trie Dictionary | T |
crate::DynBoxChars , crate::DynBoxSlice , crate::DynBoxSliceCopied | Dynamic Parser that can take any parser with same Output | T |
§Combinators
Combinator | Description | Output |
---|---|---|
seq! | Sequence of parsers | ( *<Output of A>, *<Output of B> ... ) (Tuple Concatenated ) |
or! | Or combinator | Output of the all parsers |
IntoParser::map | Map the output of the parser | (T,) |
IntoParser::repeat | Repeat the parser multiple times | (Vec<Output of Self>,) |
IntoParser::optional | Success whether the pattern is matched or not | ( Option<Output of Self>, ) |
IntoParser::optional_or | Success whether the pattern is matched or not | Output of Self |
IntoParser::not | Match for Pattern1 to success and Pattern2 to fail | Output of Self |
IntoParser::reduce_left , IntoParser::reduce_right | Reduce the output of the parser | Output of Self |
IntoParser::reduce_with , IntoParser::reduce_right_with | Reduce the output of the parser with initial value | Init |
§Others
Parser | Description | Output |
---|---|---|
crate::constant | Always succeed, and return the constant value | () |
crate::end | Success if it reached to the end of input | () |
crate::fail | Always fail | () |
IntoParser::void | Ignore the output of the parser | () |
IntoParser::output | Change Parser’s Output to (output,) | (T,) |
IntoParser::string , IntoParser::vec | Captures the matched range into String or Vec<T> | (String,) or (Vec<Iterator::Item>,) |
IntoParser::not_consume | Check if the pattern is matched or not, without consuming the input | Output of Self |
Macros§
Structs§
- DictB
Tree - Dictionary using trie, implementation uses
std::collections::BTreeMap
; O(log(N)) search. - Dict
Hash Map - Dictionary using trie, implementation uses
std::collections::HashMap
; O(1) search. - DynBox
Chars - A
Box<dyn Parser>
wrapper for iterators ofstd::str::Chars
. - DynBox
Slice - A
Box<dyn Parser>
wrapper for iterators ofstd::iter::Cloned<std::slice::Iter>
. - DynBox
Slice Copied - A
Box<dyn Parser>
wrapper for iterators ofstd::iter::Copied<std::slice::Iter>
. - Parse
Result - struct that holds the result of parsing.
Traits§
- Input
Iterator Trait - A trait alias that Input Iterator must hold.
- Into
Parser - Trait for converting possible types to Parser object.
- Parser
- Parser trait. Every parser object must implement this trait.
Functions§
- any
- This parser will match any character.
- check
- Check single item with the given closure.
- constant
- This Parser will always success and return the clone of given output.
- end
- Parser that success if reached end of input
- fail
- This Parser will always fail.
- into_
parser - Convert the given type to Parser ( if it impl IntoParser )
- match_
pattern - Match pattern of the input with the given parser.
- one
- Check one character is equal to the given character.
- one_by
- Check one character is equal to the given character by equility function.
- or
- A binary or combinator
- parse
- Parse the input with the given parser.
- range
- Check one character is in the given range.
- seq
- A binary seq combinator
- slice
- Compare the input starts with the given slice.
- slice_
by - Compare the input starts with the given slice. With given equality function.
- str
- Compare the input string starts with the given string.
- str_by
- Compare the input string starts with the given string. With given equality function.
- string
- Compare the input string starts with the given string.
- string_
by - Compare the input string starts with the given string. With given equality function.
- vec
- Compare the input starts with the given slice.
- vec_by
- Compare the input starts with the given slice. With given equality function.