[][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 merge_1(is(alphabetic)).parse("foobar") {
    Ok((result, _)) => {
        println!("Found: {}", result);
    },
    Err(_) => {
        println!("Nothing found!");
    }
}

A closer examination of the Ok result reveals:

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

Writing your own parser functions

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

fn identifier<I: Parsable>() -> impl Parser<I, Token> {
    move |input: I| {
        let (token, input) = merge_1(is(alphabetic)).parse(input)?;

        Ok((Token::Identifier(token.to_string()), input))
    }
}

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 detailed examples, look in the examples directory in the git repository.

List of parsers and combinators

Branches

all((Parser, ...))
Run each of the provided parsers and return the first that is successful.
either(Parser, Parser)
Run either parser returning whichever succeeds.
optional()
Run a parser and return its result on success or an empty result on failure.
peek(peek: Parser, Parser)
Run a parser, and if it is successful, run another parser and return its results.

Matches

empty()
Matches nothing and always succeeds, useful as a placeholder in tuples
is(Testable)
Match a character using a callback, string or anything that implements Testable.
isnt(Testable)
Match negatively, a character using a callback, string or anything that implements Testable.
literal(match: &str)
Match a literal string.
take(number: usize)
Match a specific number of characters.

Repeaters

capture_n(minimum: usize, Parser)
Run a parser a minimum number or more times and capture its results.
capture_nm(minimum: usize, maximum: usize, Parser)
Run a parser a minimum number of times and up to a maximum and capture its results.
capture_0(Parser)
Run a parser zero or more times until it has matched everything it can and capture its results.
capture_0m(maximum: usize, Parser)
Run a parser zero to a maximum number of times and capture its results.
capture_1(Parser)
Run a parser one or more times until it has matched everything it can and capture its results.
capture_1m(maximum: usize, Parser)
Run a parser one to a maximum number of times and capture its results.
merge(Parser)
Run a parsers that return multiple results and turns them into a single result.
merge_n(minimum: usize, Parser)
Run a parser a minimum number or more times and capture the input it parses.
merge_nm(minimum: usize, maximum: usize, Parser)
Run a parser a minimum number of times and up to a maximum and capture the input it parses.
merge_0(Parser)
Run a parser zero or more times until it has matched everything it can and capture the input it parses.
merge_0m(maximum: usize, Parser)
Run a parser zero to a maximum number of times and capture the input it parses.
merge_1(Parser)
Run a parser one or more times until it has matched everything it can and capture the input it parses.
merge_1m(maximum: usize, Parser)
Run a parser one to a maximum number of times and capture the input it parses.

Sequences

all((Parser, ...))
Run each of the provided parsers and in the specified order and return all of the results.
both(Parser, Parser)
Run both parsers where one must follow the other.

Structures

separated_list(separator: Parser, Parser)
Run a parser many times, separated by another parser.

Modules

branches

Combinators that parse alternatives.

errors

Error types and error handling.

matches

Parsers that match input.

parsers

Parser implementations and traits.

repeaters

Combinators that repeat multiple times.

sequences

Combinators that parse in sequence.

structures

Combinators for parsing common structures.

utilities

Utilities for working with parsers and combinators.