[][src]Function nom::bytes::complete::escaped_transform

pub fn escaped_transform<Input, Error, F, G, O1, O2, ExtendItem, Output>(
    normal: F,
    control_char: char,
    transform: G
) -> impl Fn(Input) -> IResult<Input, Output, Error> where
    Input: Clone + Offset + InputLength + InputTake + InputTakeAtPosition + Slice<RangeFrom<usize>> + InputIter,
    Input: ExtendInto<Item = ExtendItem, Extender = Output>,
    O1: ExtendInto<Item = ExtendItem, Extender = Output>,
    O2: ExtendInto<Item = ExtendItem, Extender = Output>,
    Output: Extend<<Input as ExtendInto>::Item>,
    Output: Extend<<O1 as ExtendInto>::Item>,
    Output: Extend<<O2 as ExtendInto>::Item>,
    <Input as InputIter>::Item: AsChar,
    F: Fn(Input) -> IResult<Input, O1, Error>,
    G: Fn(Input) -> IResult<Input, O2, Error>,
    Error: ParseError<Input>, 

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;
use nom::character::complete::alpha1;

fn parser(input: &str) -> IResult<&str, String> {
  escaped_transform(
    alpha1,
    '\\',
    |i:&str| alt!(i,
        tag!("\\")       => { |_| "\\" }
      | tag!("\"")       => { |_| "\"" }
      | tag!("n")        => { |_| "\n" }
    )
  )(input)
}

assert_eq!(parser("ab\\\"cd"), Ok(("", String::from("ab\"cd"))));