Crate rusty_parser

Source
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 be Tuple, including (). If you want to return a single value, use (Value,).

§Parsers Overview

§Basic(Leaf) Parsers

ParserDescriptionOutput
crate::one, crate::one_byMatch one charactor(Iterator::Item,)
crate::rangeMatch one charactor in the range(Iterator::Item,)
crate::str, crate::str_by, crate::slice, crate::slice_byMatch multiple charactors()
crate::string, crate::string_by, crate::vec, crate::vec_byMatch multiple charactors()
crate::checkCheck one charactor with closure(T,)
crate::anyMatch any charactor(Iterator::Item,)
crate::DictBTree, crate::DictHashMapTrie DictionaryT
crate::DynBoxChars, crate::DynBoxSlice, crate::DynBoxSliceCopiedDynamic Parser that can take any parser with same OutputT

§Combinators

CombinatorDescriptionOutput
seq!Sequence of parsers( *<Output of A>, *<Output of B> ... )(Tuple Concatenated )
or!Or combinatorOutput of the all parsers
IntoParser::mapMap the output of the parser(T,)
IntoParser::repeatRepeat the parser multiple times(Vec<Output of Self>,)
IntoParser::optionalSuccess whether the pattern is matched or not( Option<Output of Self>, )
IntoParser::optional_orSuccess whether the pattern is matched or notOutput of Self
IntoParser::notMatch for Pattern1 to success and Pattern2 to failOutput of Self
IntoParser::reduce_left, IntoParser::reduce_rightReduce the output of the parserOutput of Self
IntoParser::reduce_with, IntoParser::reduce_right_withReduce the output of the parser with initial valueInit

§Others

ParserDescriptionOutput
crate::constantAlways succeed, and return the constant value()
crate::endSuccess if it reached to the end of input()
crate::failAlways fail()
IntoParser::voidIgnore the output of the parser()
IntoParser::outputChange Parser’s Output to (output,)(T,)
IntoParser::string, IntoParser::vecCaptures the matched range into String or Vec<T>(String,) or (Vec<Iterator::Item>,)
IntoParser::not_consumeCheck if the pattern is matched or not, without consuming the inputOutput of Self

Macros§

or
A macro for creating or combination of parsers.
seq
A macro for creating a sequence of parsers.

Structs§

DictBTree
Dictionary using trie, implementation uses std::collections::BTreeMap; O(log(N)) search.
DictHashMap
Dictionary using trie, implementation uses std::collections::HashMap; O(1) search.
DynBoxChars
A Box<dyn Parser> wrapper for iterators of std::str::Chars.
DynBoxSlice
A Box<dyn Parser> wrapper for iterators of std::iter::Cloned<std::slice::Iter>.
DynBoxSliceCopied
A Box<dyn Parser> wrapper for iterators of std::iter::Copied<std::slice::Iter>.
ParseResult
struct that holds the result of parsing.

Traits§

InputIteratorTrait
A trait alias that Input Iterator must hold.
IntoParser
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.