[][src]Macro nom::escaped_transform

macro_rules! escaped_transform {
    ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => { ... };
    ($i:expr, $normal:expr, $control_char: expr, $submac2:ident!( $($args2:tt)*) ) => { ... };
    ($i:expr, $submac1:ident!( $($args:tt)* ), $control_char: expr, $transform:expr ) => { ... };
    ($i:expr, $normal:expr, $control_char: expr, $transform:expr) => { ... };
}

escaped_transform!(&[T] -> IResult<&[T], &[T]>, T, &[T] -> IResult<&[T], &[T]>) => &[T] -> IResult<&[T], Vec<T>> 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)

Example

fn to_s(i:Vec<u8>) -> String {
  String::from_utf8_lossy(&i).into_owned()
}

 named!(transform < String >,
   map!(
     escaped_transform!(call!(alpha1), '\\',
       alt!(
           tag!("\\")       => { |_| &b"\\"[..] }
         | tag!("\"")       => { |_| &b"\""[..] }
         | tag!("n")        => { |_| &b"\n"[..] }
       )
     ), to_s
   )
 );
 assert_eq!(transform(&b"ab\\\"cd"[..]), Ok((&b""[..], String::from("ab\"cd"))));