Expand description
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(one_or_more(is(alphabetic))).parse("foobar") {
Ok((result, _)) => {
println!("Found: {}", result);
},
Err(_) => {
println!("Nothing found!");
}
}A closer examination of the Ok result reveals:
assert_eq!(merge(one_or_more(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(one_or_more(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.
§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
min_to_max(minimum: usize, maximum: usize, Parser)- Run a parser a minimum number of times and up to a maximum.
min_or_more(minimum: usize, Parser)- Run a parser a minimum number or more times.
zero_to_max(maximum: usize, Parser)- Run a parser zero to a maximum number of times.
zero_or_more(Parser)- Run a parser zero or more times until it has matched everything it can.
one_to_max(maximum: usize, Parser)- Run a parser one to a maximum number of times.
one_or_more(Parser)- Run a parser one or more times until it has matched everything it can.
merge(Parser)- Run a parsers that return multiple results and turns them into a single result.
§Sequences
both(Parser, Parser)- Run both parsers where one must follow the other.
all((Parser, ...))- Run each of the provided parsers and in the specified order and return all of the results.
§Structures
separated_list(separator: Parser, Parser)- Run a parser many times, separated by another parser.
Modules§
- Combinators that parse alternatives.
- Error types and error handling.
- Parsers that match input.
- Parser implementations and traits.
- Combinators that repeat multiple times.
- Combinators that parse in sequence.
- Combinators for parsing common structures.
- Utilities for working with parsers and combinators.