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
sourceimpl<O> Parser<O>
impl<O> Parser<O>
sourcepub fn new<F, S>(f: F, label: S) -> Self where
F: 'static + Fn(&str) -> Result<'_, O>,
S: Into<String>,
pub fn new<F, S>(f: F, label: S) -> Self where
F: 'static + Fn(&str) -> Result<'_, O>,
S: Into<String>,
Create a new parser from a function and a label.
sourcepub fn then<U: 'static>(self, next: Parser<U>) -> Parser<(O, U)>
pub fn then<U: 'static>(self, next: Parser<U>) -> Parser<(O, U)>
Sequence this parser with the next one.
use memoir::*;
let p = string("moo").then(symbol('!')).then(symbol('?'));
assert_eq!(p.label, "\"moo\" '!' '?'");
sourcepub fn followed_by<U>(self, p: Parser<U>) -> Parser<O>
pub fn followed_by<U>(self, p: Parser<U>) -> Parser<O>
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());
sourcepub fn or(self, other: Parser<O>) -> Parser<O>
pub fn or(self, other: Parser<O>) -> Parser<O>
If this parser fails without any consuming input, try another one.
use memoir::*;
let p = symbol('!').or(symbol('?'));
assert_eq!(p.parse("?"), Ok(('?', "")));
sourcepub fn skip<U>(self, skip: Parser<U>) -> Parser<O> where
Self: Sized,
pub fn skip<U>(self, skip: Parser<U>) -> Parser<O> where
Self: Sized,
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'), "")));
sourcepub fn map<U: 'static, F>(self, f: F) -> Parser<U> where
F: 'static + Fn(O) -> U,
pub fn map<U: 'static, F>(self, f: F) -> Parser<U> where
F: 'static + Fn(O) -> U,
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'), "")));
sourcepub fn try_map<U: 'static, S, F>(self, f: F) -> Parser<U> where
F: 'static + Fn(O) -> Result<U, S>,
S: Into<String>,
pub fn try_map<U: 'static, S, F>(self, f: F) -> Parser<U> where
F: 'static + Fn(O) -> Result<U, S>,
S: Into<String>,
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())
sourcepub fn value<U: Clone + 'static>(self, val: U) -> Parser<U>
pub fn value<U: Clone + 'static>(self, val: U) -> Parser<U>
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', "")));
sourcepub fn label(self, l: impl Into<String>) -> Parser<O>
pub fn label(self, l: impl Into<String>) -> Parser<O>
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.
sourcepub fn parse<'a>(&self, input: &'a str) -> Result<'a, O>
pub fn parse<'a>(&self, input: &'a str) -> Result<'a, O>
Parse an input string and return a result. On success, returns an output, and any leftover input. Otherwise, returns an error.
sourcepub fn from_str<U, E>(self) -> Parser<U> where
O: AsRef<str>,
U: FromStr<Err = E>,
E: Display,
pub fn from_str<U, E>(self) -> Parser<U> where
O: AsRef<str>,
U: FromStr<Err = E>,
E: Display,
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
Auto Trait Implementations
impl<O> !RefUnwindSafe for Parser<O>
impl<O> !Send for Parser<O>
impl<O> !Sync for Parser<O>
impl<O> Unpin for Parser<O>
impl<O> !UnwindSafe for Parser<O>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more