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

§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§

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

Structs§

Traits§

  • A trait alias that Input Iterator must hold.
  • Trait for converting possible types to Parser object.
  • Parser trait. Every parser object must implement this trait.

Functions§

  • This parser will match any character.
  • Check single item with the given closure.
  • 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 pattern of the input with the given parser.
  • Check one character is equal to the given character.
  • Check one character is equal to the given character by equility function.
  • A binary or combinator
  • Parse the input with the given parser.
  • Check one character is in the given range.
  • A binary seq combinator
  • Compare the input starts with the given slice.
  • Compare the input starts with the given slice. With given equality function.
  • Compare the input string starts with the given string.
  • Compare the input string starts with the given string. With given equality function.
  • Compare the input string starts with the given string.
  • Compare the input string starts with the given string. With given equality function.
  • Compare the input starts with the given slice.
  • Compare the input starts with the given slice. With given equality function.