Crate fabparse

Source
Expand description

Fabparse. A minimized parser combinator library.

Fabparse is a minimized trait based parser combinator library. Most of the functionality is within the Parser trait. Fabparse implements the parser trait for a wide variety of types. It restricts itself to a few powerful and composable operations.

All of these are parsers.

ParserInputParsingOutputInput after parsing
'a'let mut input = "abc"'a'.fab(&mut input)'a'"bc"
'a'let mut input = "def"'a'.fab(&mut input)FabError(...)"def"
"abc"let mut input = "abcdef""abc".fab(&mut input)"abc""def"
[1, 2]let mut input = "[1, 2, 3].as_slice()"[1, 2].fab(&mut input)[1, 2][3]
('a'..='z')let mut input = "zyx"('a'..='z').fab(&mut input)z"yx"
take(2)let mut input = "abc"take(2).fab(&mut input)"ab""c"
❘c❘ c=='m'let mut input = "moo"(❘c❘ c=='m').fab(&mut input)'m'"oo"
char::is_ascii_digitlet mut input = "123"char::is_ascii_digit.fab(&mut input)'1'"23"
let parser = ❘c: char❘ if c=='m' {Some(5)} else {None}let mut input = "moo"parser.fab(&mut input)5"oo"
let parser = ❘c: char❘ if c=='m' {Ok(5)} else {Err(ErrType)}let mut input = "moo"parser.fab(&mut input)5"oo"

Custom functions can also be parsers.

fn is_it_a(input: &mut &str) -> Result<char, FabError> { 'a'.fab(input) }

ParserInputParsingOutputInput after parsing
is_it_alet mut input = "abc"is_it_a.fab(&mut input)'a'"bc"

These parsers can be modified through the methods available in the Parser trait.

ParserInputParsingOutputInput after parsing
let parser = 'a'.fab_value(5)let mut input = "abc"parser.fab(&mut input)5"bc"
let parser = 'a'.fab_map(char::to_ascii_uppercase)let mut input = "abc"parser.fab(&mut input)A"bc"
let parser = '1'.fab_try_map(❘c❘ c.to_digit(10))let mut input = "123"parser.fab(&mut input)1"23"
let parser = 'a'.fab_try_map(❘c❘ c.to_digit(10))let mut input = "abc"parser.fab(&mut input)FabError(...)"abc"
let parser = 'a'.fab_repeat()let mut input = "aabb"parser.fab(&mut input)vec['a','a']"bb"
let parser = 'a'.fab_repeat()let mut input = "bbbb"parser.fab(&mut input)vec[]"bbbb"
let parser = 'a'.fab_repeat().as_input_slice()let mut input = "aabb"parser.fab(&mut input)"aa""bb"
let parser = 'a'.fab_repeat().min(1)let mut input = "bbbb"parser.fab(&mut input)FabError(...)"bbbb"

fab_try_map works both with functions that return Results and ones that return Options.

The Repeat struct has additional method for customization trait. These include setting a maximum number of items to parse, or outputting a custom data structure.

These parsers can be combined with these methods.

ParserInputParsingOutputInput after parsing
alt('a','b')let mut input = "abc"alt('a','b').fab(&mut input)a"bc"
alt('a','b')let mut input = "bca"alt('a','b').fab(&mut input)b"ca"
alt('a','b')let mut input = "cab"alt('a','b').fab(&mut input)FabError(...)"cab"
opt('a')let mut input = "abc"opt('a').fab(&mut input)Some('a')"bc"
opt('a')let mut input = "cab"opt('a').fab(&mut input)None"cab"
take_not('a')let mut input = "cab"take_not('a').fab(&mut input)'c'"ab"
take_not('a')let mut input = "abc"take_not('a').fab(&mut input)FabError(...)"abc"

Some code is inspired by Winnow by Elliot Page + other contributors.

Re-exports§

pub use error::FabError;
pub use error::ParserError;
pub use error::NoContextFabError;
pub use repeat::TryReducer;
pub use repeat::TryReducerError;
pub use repeat::Repeat;

Modules§

branch
combinator
error
repeat
sequence
tag
util

Enums§

ParserType
This enum represents the kinds of parsers in Fabparse. This is used in errors to identify the parser that failed.

Traits§

Parser

Functions§

alt
This function takes in a tuple of 1 to 11 parsers, all with the same output type. It returns a parser that succeeds when any of its input parsers succeed, with the output of the parser that succeeded.
opt
This function makes the underlying parser optional. If the underlying parser succeeds with Ok(out), this parser returns Some(out). Otherwise, this parser succeeds with None and consumes no input.
permutation
This function takes in a tuple of 1 to 11 parsers. It returns a parser that succeeds when all of the input parsers have succeeded in any order. permutation((parser_1,parser_2)) will return (output_parser_1, output_parser_2), regardless of the order they succeed in.
take
take(x: usize) Constructs a parser that takes x items. For strings, this will be characters and for arrays it will be elements. This parser outputs a &str for an input of &str and a &[T] for an input of &[T]
take_not
Creates a parser that takes a single item if the underlying parser fails. If the underlying parser succeeds, this parser fails. For strings, on success this will take a char and for arrays it will take a single item. The result will be the char/item.