pub fn escaped_transform<I, Error, F, G, O1, O2, ExtendItem, Output>(
normal: F,
control_char: char,
transform: G,
) -> impl FnMut(I) -> IResult<I, Output, Error>where
I: Clone + Offset + Input + ExtendInto<Item = ExtendItem, Extender = Output>,
O1: ExtendInto<Item = ExtendItem, Extender = Output>,
O2: ExtendInto<Item = ExtendItem, Extender = Output>,
<I as Input>::Item: AsChar,
F: Parser<I, Output = O1, Error = Error>,
G: Parser<I, Output = O2, Error = Error>,
Error: ParseError<I>,
Expand description
Matches a byte string with escaped characters.
- The first argument matches the normal characters (it must not match the control character)
- The second argument is the control character (like
\
in most languages) - The third argument matches the escaped characters and transforms them
As an example, the chain abc\tdef
could be abc def
(it also consumes the control character)
use nom::bytes::complete::{escaped_transform, tag};
use nom::character::complete::alpha1;
use nom::branch::alt;
use nom::combinator::value;
fn parser(input: &str) -> IResult<&str, String> {
escaped_transform(
alpha1,
'\\',
alt((
value("\\", tag("\\")),
value("\"", tag("\"")),
value("\n", tag("n")),
))
)(input)
}
assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd"))));
assert_eq!(parser("ab\\ncd"), Ok(("", String::from("ab\ncd"))));