Skip to content

Parser combinators in Rust. Build complicated parsers by composing small parsers.

Notifications You must be signed in to change notification settings

aiifabbf/parsec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

parsec

Build complicated parsers by composing small parsers

Quick start

Build a parser that extracts as many digits as possible

let digit = satisfy(|c| c.is_digit(10));
let parser = digit.many();
assert_eq!(dbg!(parser.parse("1234abc")), Some(("1234".to_owned(), "abc")));

Build a parser that matches valid parentheses like (()())((()))

fn s(input: &str) -> Option<((), &str)> {
    eof
        .choice(
            string("()")
                .map(|_| ())
                .choice(s.between(char('('), char(')'))) // Yes, recursive grammar is possible.
                .many()
                .map(|_| ()),
        )
        .parse(input)
}

let parser = s; // fn(&str) -> Option<(T, &str)> implements Parser<T>
assert_eq!(
    dbg!(parser.parse("((()))(()(()))".repeat(100000).as_str())),
    Some(((), ""))
); // Don't worry, performance is good.

About

Parser combinators in Rust. Build complicated parsers by composing small parsers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages