David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 1 | use nom::IResult; |
| 2 | use unicode_xid::UnicodeXID; |
| 3 | |
| 4 | pub fn whitespace(input: &str) -> IResult<&str, ()> { |
| 5 | if input.is_empty() { |
| 6 | return IResult::Error; |
| 7 | } |
| 8 | |
| 9 | let mut start = 0; |
| 10 | let mut chars = input.char_indices(); |
| 11 | while let Some((i, ch)) = chars.next() { |
| 12 | let s = &input[start + i..]; |
| 13 | if ch == '/' { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 14 | if s.starts_with("//") && (!s.starts_with("///") || s.starts_with("////")) && |
| 15 | !s.starts_with("//!") { |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 16 | if let Some(len) = s.find('\n') { |
| 17 | start += i + len + 1; |
| 18 | chars = input[start..].char_indices(); |
| 19 | continue; |
| 20 | } |
| 21 | break; |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 22 | } else if s.starts_with("/*") && !s.starts_with("/**") && !s.starts_with("/*!") { |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 23 | match block_comment(s) { |
| 24 | IResult::Done(_, com) => { |
| 25 | start += i + com.len(); |
| 26 | chars = input[start..].char_indices(); |
| 27 | continue; |
| 28 | } |
| 29 | IResult::Error => { |
| 30 | return IResult::Error; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | if !ch.is_whitespace() { |
| 36 | return if start + i > 0 { |
| 37 | IResult::Done(s, ()) |
| 38 | } else { |
| 39 | IResult::Error |
| 40 | }; |
| 41 | } |
| 42 | } |
| 43 | IResult::Done("", ()) |
| 44 | } |
| 45 | |
| 46 | pub fn block_comment(input: &str) -> IResult<&str, &str> { |
| 47 | if !input.starts_with("/*") { |
| 48 | return IResult::Error; |
| 49 | } |
| 50 | |
| 51 | let mut depth = 0; |
| 52 | let mut chars = input.char_indices(); |
| 53 | while let Some((i, _)) = chars.next() { |
| 54 | let s = &input[i..]; |
| 55 | if s.starts_with("/*") { |
| 56 | depth += 1; |
| 57 | chars.next(); // eat '*' |
| 58 | } else if s.starts_with("*/") { |
| 59 | depth -= 1; |
| 60 | if depth == 0 { |
| 61 | return IResult::Done(&input[i + 2..], &input[..i + 2]); |
| 62 | } |
| 63 | chars.next(); // eat '/' |
| 64 | } |
| 65 | } |
| 66 | IResult::Error |
| 67 | } |
| 68 | |
| 69 | pub fn word_break(input: &str) -> IResult<&str, ()> { |
| 70 | match input.chars().next() { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 71 | Some(ch) if UnicodeXID::is_xid_continue(ch) => IResult::Error, |
| 72 | Some(_) | None => IResult::Done(input, ()), |
David Tolnay | 14cbdeb | 2016-10-01 12:13:59 -0700 | [diff] [blame] | 73 | } |
| 74 | } |