# Glue <img src="brand/glue-512.png" align="right" width="128" height="128" />
Glue is a parser combinator framework for parsing text based formats, it is easy to use and relatively fast too.
## Usage
```rust
use glue::prelude::*;
match merge_1(is(alphabetic)).parse("foobar") {
Ok((result, _)) => {
println!("Found: {}", result);
},
Err(_) => {
println!("Nothing found!");
}
}
```
### Writing your own parser functions
```rust
use glue::prelude::*;
#[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].
[git repository]: https://gitlab.com/glue-for-rust/glue-rs
[examples directory]: https://gitlab.com/glue-for-rust/glue-rs/tree/release/0.5.x/examples
[this example]: https://gitlab.com/glue-for-rust/glue-rs/blob/release/0.5.x/examples/printable_error.rs
### List of parsers and combinators
#### Branches
<dl>
<dt><code>all((Parser, ...))</code></dt>
<dd>Run each of the provided parsers and return the first that is successful.</dd>
<dt><code>either(Parser, Parser)</code></dt>
<dd>Run either parser returning whichever succeeds.</dd>
<dt><code>optional()</code></dt>
<dd>Run a parser and return its result on success or an empty result on failure.</dd>
<dt><code>peek(peek: Parser, Parser)</code></dt>
<dd>Run a parser, and if it is successful, run another parser and return its results.</dd>
</dl>
#### Matches
<dl>
<dt><code>empty()</code></dt>
<dd>Matches nothing and always succeeds, useful as a placeholder in tuples</dd>
<dt><code>is(Testable)</code></dt>
<dd>Match a character using a callback, string or anything that implements `Testable`.</dd>
<dt><code>isnt(Testable)</code></dt>
<dd>Match negatively, a character using a callback, string or anything that implements `Testable`.</dd>
<dt><code>literal(match: &str)</code></dt>
<dd>Match a literal string.</dd>
<dt><code>take(number: usize)</code></dt>
<dd>Match a specific number of characters.</dd>
</dl>
#### Repeaters
<dl>
<dt><code>capture_n(minimum: usize, Parser)</code></dt>
<dd>Run a parser a minimum number or more times and capture its results.</dd>
<dt><code>capture_nm(minimum: usize, maximum: usize, Parser)</code></dt>
<dd>Run a parser a minimum number of times and up to a maximum and capture its results.</dd>
<dt><code>capture_0(Parser)</code></dt>
<dd>Run a parser zero or more times until it has matched everything it can and capture its results.</dd>
<dt><code>capture_0m(maximum: usize, Parser)</code></dt>
<dd>Run a parser zero to a maximum number of times and capture its results.</dd>
<dt><code>capture_1(Parser)</code></dt>
<dd>Run a parser one or more times until it has matched everything it can and capture its results.</dd>
<dt><code>capture_1m(maximum: usize, Parser)</code></dt>
<dd>Run a parser one to a maximum number of times and capture its results.</dd>
<dt><code>merge(Parser)</code></dt>
<dd>Run a parsers that return multiple results and turns them into a single result.</dd>
<dt><code>merge_n(minimum: usize, Parser)</code></dt>
<dd>Run a parser a minimum number or more times and capture the input it parses.</dd>
<dt><code>merge_nm(minimum: usize, maximum: usize, Parser)</code></dt>
<dd>Run a parser a minimum number of times and up to a maximum and capture the input it parses.</dd>
<dt><code>merge_0(Parser)</code></dt>
<dd>Run a parser zero or more times until it has matched everything it can and capture the input it parses.</dd>
<dt><code>merge_0m(maximum: usize, Parser)</code></dt>
<dd>Run a parser zero to a maximum number of times and capture the input it parses.</dd>
<dt><code>merge_1(Parser)</code></dt>
<dd>Run a parser one or more times until it has matched everything it can and capture the input it parses.</dd>
<dt><code>merge_1m(maximum: usize, Parser)</code></dt>
<dd>Run a parser one to a maximum number of times and capture the input it parses.</dd>
</dl>
#### Sequences
<dl>
<dt><code>all((Parser, ...))</code></dt>
<dd>Run each of the provided parsers and in the specified order and return all of the results.</dd>
<dt><code>both(Parser, Parser)</code></dt>
<dd>Run both parsers where one must follow the other.</dd>
</dl>
#### Structures
<dl>
<dt><code>separated_list(separator: Parser, Parser)</code></dt>
<dd>Run a parser many times, separated by another parser.</dd>
</dl>