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 | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame^] | 7 | $crate::helper::punct($i, $punct) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | }; |
| 9 | } |
| 10 | |
David Tolnay | 13e5da4 | 2016-09-04 16:18:34 -0700 | [diff] [blame^] | 11 | pub fn punct<'a>(input: &'a str, token: &'static str) -> IResult<&'a str, &'a str> { |
| 12 | let mut chars = input.char_indices(); |
| 13 | while let Some((i, ch)) = chars.next() { |
| 14 | if !ch.is_whitespace() { |
| 15 | return if input[i..].starts_with(token) { |
| 16 | let end = i + token.len(); |
| 17 | IResult::Done(&input[end..], &input[i..end]) |
| 18 | } else { |
| 19 | IResult::Error(nom::Err::Position(nom::ErrorKind::TagStr, input)) |
| 20 | }; |
| 21 | } |
| 22 | } |
| 23 | IResult::Error(nom::Err::Position(nom::ErrorKind::TagStr, input)) |
| 24 | } |
| 25 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 26 | macro_rules! option ( |
| 27 | ($i:expr, $submac:ident!( $($args:tt)* )) => ({ |
| 28 | match $submac!($i, $($args)*) { |
| 29 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, Some(o)), |
| 30 | ::nom::IResult::Error(_) => ::nom::IResult::Done($i, None), |
| 31 | ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, None), |
| 32 | } |
| 33 | }); |
| 34 | ($i:expr, $f:expr) => ( |
| 35 | option!($i, call!($f)); |
| 36 | ); |
| 37 | ); |
| 38 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 39 | macro_rules! opt_vec ( |
| 40 | ($i:expr, $submac:ident!( $($args:tt)* )) => ({ |
| 41 | match $submac!($i, $($args)*) { |
| 42 | ::nom::IResult::Done(i, o) => ::nom::IResult::Done(i, o), |
| 43 | ::nom::IResult::Error(_) => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 44 | ::nom::IResult::Incomplete(_) => ::nom::IResult::Done($i, Vec::new()), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 45 | } |
| 46 | }); |
| 47 | ); |
| 48 | |
| 49 | macro_rules! epsilon { |
| 50 | ($i:expr,) => { |
| 51 | call!($i, { |
| 52 | fn epsilon<T>(input: T) -> ::nom::IResult<T, ()> { |
| 53 | ::nom::IResult::Done(input, ()) |
| 54 | } |
| 55 | epsilon |
| 56 | }) |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | pub fn escaped_string(input: &str) -> IResult<&str, String> { |
| 61 | let mut s = String::new(); |
| 62 | let mut chars = input.char_indices().peekable(); |
| 63 | while let Some((byte_offset, ch)) = chars.next() { |
| 64 | match ch { |
| 65 | '"' => { |
| 66 | return IResult::Done(&input[byte_offset..], s); |
| 67 | } |
| 68 | '\\' => { |
| 69 | match chars.next() { |
| 70 | Some((_, 'x')) => unimplemented!(), |
| 71 | Some((_, 'u')) => unimplemented!(), |
| 72 | Some((_, 'n')) => s.push('\n'), |
| 73 | Some((_, 'r')) => s.push('\r'), |
| 74 | Some((_, 't')) => s.push('\t'), |
| 75 | Some((_, '0')) => s.push('\0'), |
| 76 | Some((_, '\\')) => s.push('\\'), |
| 77 | Some((_, '\n')) => { |
| 78 | while let Some(&(_, ch)) = chars.peek() { |
| 79 | if ch.is_whitespace() { |
| 80 | chars.next(); |
| 81 | } else { |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | _ => break, |
| 87 | } |
| 88 | } |
| 89 | ch => { |
| 90 | s.push(ch); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | IResult::Error(nom::Err::Position(nom::ErrorKind::Escaped, input)) |
| 95 | } |