David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 1 | #![cfg(feature = "parsing")] |
| 2 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | use nom::{self, IResult}; |
| 4 | |
| 5 | macro_rules! punct { |
| 6 | ($i:expr, $punct:expr) => { |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 7 | complete!($i, preceded!(opt!(call!(::nom::multispace)), tag_s!($punct))) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | }; |
| 9 | } |
| 10 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 11 | macro_rules! option ( |
| 12 | ($i:expr, $submac:ident!( $($args:tt)* )) => ({ |
| 13 | match $submac!($i, $($args)*) { |
| 14 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)), |
| 15 | ::nom::IResult::Error(_) => ::nom::IResult::Done($i, None), |
| 16 | ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, None), |
| 17 | } |
| 18 | }); |
| 19 | ($i:expr, $f:expr) => ( |
| 20 | option!($i, call!($f)); |
| 21 | ); |
| 22 | ); |
| 23 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 24 | macro_rules! opt_vec ( |
| 25 | ($i:expr, $submac:ident!( $($args:tt)* )) => ({ |
| 26 | match $submac!($i, $($args)*) { |
| 27 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o), |
| 28 | ::nom::IResult::Error(_) => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame^] | 29 | ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 30 | } |
| 31 | }); |
| 32 | ); |
| 33 | |
| 34 | macro_rules! epsilon { |
| 35 | ($i:expr,) => { |
| 36 | call!($i, { |
| 37 | fn epsilon<T>(input: T) -> ::nom::IResult<T, ()> { |
| 38 | ::nom::IResult::Done(input, ()) |
| 39 | } |
| 40 | epsilon |
| 41 | }) |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | pub fn escaped_string(input: &str) -> IResult<&str, String> { |
| 46 | let mut s = String::new(); |
| 47 | let mut chars = input.char_indices().peekable(); |
| 48 | while let Some((byte_offset, ch)) = chars.next() { |
| 49 | match ch { |
| 50 | '"' => { |
| 51 | return IResult::Done(&input[byte_offset..], s); |
| 52 | } |
| 53 | '\\' => { |
| 54 | match chars.next() { |
| 55 | Some((_, 'x')) => unimplemented!(), |
| 56 | Some((_, 'u')) => unimplemented!(), |
| 57 | Some((_, 'n')) => s.push('\n'), |
| 58 | Some((_, 'r')) => s.push('\r'), |
| 59 | Some((_, 't')) => s.push('\t'), |
| 60 | Some((_, '0')) => s.push('\0'), |
| 61 | Some((_, '\\')) => s.push('\\'), |
| 62 | Some((_, '\n')) => { |
| 63 | while let Some(&(_, ch)) = chars.peek() { |
| 64 | if ch.is_whitespace() { |
| 65 | chars.next(); |
| 66 | } else { |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | _ => break, |
| 72 | } |
| 73 | } |
| 74 | ch => { |
| 75 | s.push(ch); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | IResult::Error(nom::Err::Position(nom::ErrorKind::Escaped, input)) |
| 80 | } |