[][src]Crate glue

Glue is a parser combinator framework that is designed for parsing text based formats, it is easy to use and relatively efficient.

Usage

use glue::prelude::*;

match take(1.., is(alphabetic)).parse((), "foobar") {
    Ok((_, result, _)) => {
        println!("Found: {}", result);
    },
    Err(_) => {
        println!("Nothing found!");
    }
}

A closer examination of the Ok result reveals:

assert_eq!(take(1.., is(alphabetic)).parse((), "foobar"), Ok((
    (), "foobar", ""
)))

Writing your own parser functions

#[derive(Debug, PartialEq)]
enum Token {
    Identifier(String),
}

fn identifier<S, U: Parsable>() -> impl Parser<S, U, Token> {
    move |state: S, unparsed: U| {
        take(1.., is(alphabetic)).parse(state, unparsed)
            .map_result(|token| Token::Identifier(token.into()))
    }
}

assert_eq!(identifier().parse((), "foobar"), Ok((
    (),
    Token::Identifier("foobar".into()),
    ""
)));

Pretty human readable error messages

Glue does the hard work of implementing error messages for you, have a look at this example which created the following message:

   1 │ foo xxx
   · │       ┃
   · ┢━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
   · ┃ Unexpected 'xxx'                                                       ┃
   · ┃ Expected bar                                                           ┃
   · ┃ At 1:7 of path/to/file                                                 ┃
     ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

For more information, read the tutorial, check the cheat sheet, look in the examples directory in the git repository.

Modules

branches

Combinators that parse alternatives.

matches

Parsers that match input.

parsers

Parser implementations and traits.

repeaters

Combinators that repeat multiple times.

sequences

Combinators that parse in sequence.

states

Parser success and failure states.

structures

Combinators for parsing common structures.

testers

Tester implementations for the is and isnt parsers.