Function winnow::ascii::escaped_transform
source · pub fn escaped_transform<Input, Error, Normal, Escape, Output>(
normal: Normal,
control_char: char,
escape: Escape
) -> impl Parser<Input, Output, Error>where
Input: StreamIsPartial + Stream + Compare<char>,
Output: Accumulate<<Input as Stream>::Slice>,
Normal: Parser<Input, <Input as Stream>::Slice, Error>,
Escape: Parser<Input, <Input as Stream>::Slice, Error>,
Error: ParserError<Input>,
Expand description
Parse escaped characters, unescaping them
As an example, the chain abc\tdef
could be abc def
(it also consumes the control character)
Example
use winnow::token::literal;
use winnow::ascii::escaped_transform;
use winnow::ascii::alpha1;
use winnow::combinator::alt;
fn parser<'s>(input: &mut &'s str) -> PResult<String, InputError<&'s str>> {
escaped_transform(
alpha1,
'\\',
alt((
"\\".value("\\"),
"\"".value("\""),
"n".value("\n"),
))
).parse_next(input)
}
assert_eq!(parser.parse_peek("ab\\\"cd"), Ok(("", String::from("ab\"cd"))));
assert_eq!(parser.parse_peek("ab\\ncd"), Ok(("", String::from("ab\ncd"))));
use winnow::token::literal;
use winnow::ascii::escaped_transform;
use winnow::ascii::alpha1;
use winnow::combinator::alt;
fn parser<'s>(input: &mut Partial<&'s str>) -> PResult<String, InputError<Partial<&'s str>>> {
escaped_transform(
alpha1,
'\\',
alt((
"\\".value("\\"),
"\"".value("\""),
"n".value("\n"),
))
).parse_next(input)
}
assert_eq!(parser.parse_peek(Partial::new("ab\\\"cd\"")), Ok((Partial::new("\""), String::from("ab\"cd"))));