/// Helper trait for the [alt()] combinator. /// /// This trait is implemented for tuples of up to 21 elements pubtrait Alt<I, O, E> { /// Tests each parser in the tuple and returns the result of the first one that succeeds fn choice(&mutself, input: I) -> IResult<I, O, E>;
}
/// Tests a list of parsers one by one until one succeeds. /// /// It takes as argument a tuple of parsers. There is a maximum of 21 /// parsers. If you need more, it is possible to nest them in other `alt` calls, /// like this: `alt(parser_a, alt(parser_b, parser_c))` /// /// ```rust /// # use nom::error_position; /// # use nom::{Err,error::ErrorKind, Needed, IResult}; /// use nom::character::complete::{alpha1, digit1}; /// use nom::branch::alt; /// # fn main() { /// fn parser(input: &str) -> IResult<&str, &str> { /// alt((alpha1, digit1))(input) /// }; /// /// // the first parser, alpha1, recognizes the input /// assert_eq!(parser("abc"), Ok(("", "abc"))); /// /// // the first parser returns an error, so alt tries the second one /// assert_eq!(parser("123456"), Ok(("", "123456"))); /// /// // both parsers failed, and with the default error type, alt will return the last error /// assert_eq!(parser(" "), Err(Err::Error(error_position!(" ", ErrorKind::Digit)))); /// # } /// ``` /// /// With a custom error type, it is possible to have alt return the error of the parser /// that went the farthest in the input data pubfn alt<I: Clone, O, E: ParseError<I>, List: Alt<I, O, E>>( mut l: List,
) -> impl FnMut(I) -> IResult<I, O, E> { move |i: I| l.choice(i)
}
/// Helper trait for the [permutation()] combinator. /// /// This trait is implemented for tuples of up to 21 elements pubtrait Permutation<I, O, E> { /// Tries to apply all parsers in the tuple in various orders until all of them succeed fn permutation(&mutself, input: I) -> IResult<I, O, E>;
}
/// Applies a list of parsers in any order. /// /// Permutation will succeed if all of the child parsers succeeded. /// It takes as argument a tuple of parsers, and returns a /// tuple of the parser results. /// /// ```rust /// # use nom::{Err,error::{Error, ErrorKind}, Needed, IResult}; /// use nom::character::complete::{alpha1, digit1}; /// use nom::branch::permutation; /// # fn main() { /// fn parser(input: &str) -> IResult<&str, (&str, &str)> { /// permutation((alpha1, digit1))(input) /// } /// /// // permutation recognizes alphabetic characters then digit /// assert_eq!(parser("abc123"), Ok(("", ("abc", "123")))); /// /// // but also in inverse order /// assert_eq!(parser("123abc"), Ok(("", ("abc", "123")))); /// /// // it will fail if one of the parsers failed /// assert_eq!(parser("abc;"), Err(Err::Error(Error::new(";", ErrorKind::Digit)))); /// # } /// ``` /// /// The parsers are applied greedily: if there are multiple unapplied parsers /// that could parse the next slice of input, the first one is used. /// ```rust /// # use nom::{Err, error::{Error, ErrorKind}, IResult}; /// use nom::branch::permutation; /// use nom::character::complete::{anychar, char}; /// /// fn parser(input: &str) -> IResult<&str, (char, char)> { /// permutation((anychar, char('a')))(input) /// } /// /// // anychar parses 'b', then char('a') parses 'a' /// assert_eq!(parser("ba"), Ok(("", ('b', 'a')))); /// /// // anychar parses 'a', then char('a') fails on 'b', /// // even though char('a') followed by anychar would succeed /// assert_eq!(parser("ab"), Err(Err::Error(Error::new("b", ErrorKind::Char)))); /// ``` /// pubfn permutation<I: Clone, O, E: ParseError<I>, List: Permutation<I, O, E>>( mut l: List,
) -> impl FnMut(I) -> IResult<I, O, E> { move |i: I| l.permutation(i)
}
// If we reach here, every iterator has either been applied before, // or errored on the remaining input iflet Some(err) = err { // There are remaining parsers, and all errored on the remaining input return Err(Err::Error(Error::append(input, ErrorKind::Permutation, err)));
}
// All parsers were applied match res {
($(Some($item)),+) => return Ok((input, ($($item),+))),
_ => unreachable!(),
}
}
}
}
);
);
permutation_trait!(
FnA A a
FnB B b
FnC C c
FnD D d
FnE E e
FnF F f
FnG G g
FnH H h
FnI I i
FnJ J j
FnK K k
FnL L l
FnM M m
FnN N n
FnO O o
FnP P p
FnQ Q q
FnR R r
FnS S s
FnT T t
FnU U u
);
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.