Function nom8::bytes::escaped_transform
source · pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output, const STREAMING: bool>(
normal: F,
control_char: char,
transform: G
) -> impl FnMut(Input) -> IResult<Input, Output, Error>where
Input: Clone + Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter + InputIsStreaming<STREAMING> + IntoOutput + ExtendInto<Item = ExtendItem, Extender = Output>,
O1: ExtendInto<Item = ExtendItem, Extender = Output>,
O2: ExtendInto<Item = ExtendItem, Extender = Output>,
<Input as InputIter>::Item: AsChar,
F: Parser<Input, O1, Error>,
G: Parser<Input, O2, Error>,
Error: ParseError<Input>,
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 nom8::bytes::{escaped_transform, tag};
use nom8::character::alpha1;
use nom8::branch::alt;
use nom8::combinator::value;
fn parser(input: &str) -> IResult<&str, String> {
escaped_transform(
alpha1,
'\\',
alt((
tag("\\").value("\\"),
tag("\"").value("\""),
tag("n").value("\n"),
))
)(input)
}
assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd"))));
assert_eq!(parser("ab\\ncd"), Ok(("", String::from("ab\ncd"))));
use nom8::bytes::{escaped_transform, tag};
use nom8::character::alpha1;
use nom8::branch::alt;
use nom8::combinator::value;
fn parser(input: Streaming<&str>) -> IResult<Streaming<&str>, String> {
escaped_transform(
alpha1,
'\\',
alt((
tag("\\").value("\\"),
tag("\"").value("\""),
tag("n").value("\n"),
))
)(input)
}
assert_eq!(parser(Streaming("ab\\\"cd\"")), Ok((Streaming("\""), String::from("ab\"cd"))));