pub struct Parser<O: 'static> {
    pub label: String,
    /* private fields */
}
Expand description

A self-describing parser combinator.

Fields

label: String

The label or description of this parser.

Implementations

Create a new parser from a function and a label.

Sequence this parser with the next one.

use memoir::*;

let p = string("moo").then(symbol('!')).then(symbol('?'));
assert_eq!(p.label, "\"moo\" '!' '?'");

Fail this parser if the predicate fails.

use memoir::*;

let p = string("moo").followed_by(symbol('.'));
assert_eq!(p.parse("moo."), Ok(("moo".to_owned(), ".")));
assert!(p.parse("moo").is_err());

If this parser fails without any consuming input, try another one.

use memoir::*;

let p = symbol('!').or(symbol('?'));

assert_eq!(p.parse("?"), Ok(('?', "")));

Apply this parser, then try to apply the other parser. Only the output from this parser is returned.

use memoir::*;

let p = symbol('X').skip(symbol('Y')).then(symbol('Z'));

assert_eq!(p.parse("XYZ"), Ok((('X', 'Z'), "")));
assert!(p.parse("XZ").is_err());

let p = symbol('X').skip(optional(symbol('Y'))).then(symbol('Z'));

assert_eq!(p.parse("XYZ"), Ok((('X', 'Z'), "")));
assert_eq!(p.parse("XZ"), Ok((('X', 'Z'), "")));

Modify the parser output if it succeeds, with the provided function.

use memoir::*;

let p = symbol('X').map(|out| (out, out));

assert_eq!(p.parse("X"), Ok((('X', 'X'), "")));

Modify the parser output if it succeeds, with the provided function that can fail.

use memoir::*;

let p = symbol('X').try_map::<String, _, _>(|out| Err(format!("failed to parse {}", out)));

assert!(p.parse("X").is_err())

Modify the parser output if it succeeds, with the provided value.

use memoir::*;

let p = symbol('X').value('Y');

assert_eq!(p.parse("X"), Ok(('Y', "")));

Overwrite this parser’s description with the given string. This is useful in particular when using one of the provideed parsers, and the built-in description is not adequate.

Parse an input string and return a result. On success, returns an output, and any leftover input. Otherwise, returns an error.

Expect the end of input.

Try to convert the output of the parser from a string to the specified type.

use memoir::*;

let p = many::<_, String>(digit()).from_str::<u64, _>();

assert_eq!(p.parse("12345"), Ok((12345, "")));
assert!(p.parse("abcde").is_err());

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Converts to this type from the input type.

Converts to this type from the input type.

The resulting type after applying the >> operator.

Performs the >> operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.