David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 1 | use std::fmt::{self, Display}; |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 2 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 3 | #[derive(Debug, Clone, Eq, Hash)] |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 4 | pub struct Ident(String); |
| 5 | |
| 6 | impl Ident { |
| 7 | pub fn new<T: Into<Ident>>(t: T) -> Self { |
| 8 | t.into() |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | impl<'a> From<&'a str> for Ident { |
| 13 | fn from(s: &str) -> Self { |
| 14 | Ident(s.to_owned()) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | impl From<String> for Ident { |
| 19 | fn from(s: String) -> Self { |
| 20 | Ident(s) |
| 21 | } |
| 22 | } |
| 23 | |
David Tolnay | c168310 | 2016-09-27 08:17:58 -0700 | [diff] [blame] | 24 | impl From<usize> for Ident { |
| 25 | fn from(u: usize) -> Self { |
| 26 | Ident(u.to_string()) |
| 27 | } |
| 28 | } |
| 29 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 30 | impl AsRef<str> for Ident { |
| 31 | fn as_ref(&self) -> &str { |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 32 | &self.0 |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl Display for Ident { |
| 37 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 38 | self.0.fmt(formatter) |
| 39 | } |
| 40 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 41 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 42 | impl<T: ?Sized> PartialEq<T> for Ident |
| 43 | where T: AsRef<str> |
| 44 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 45 | fn eq(&self, other: &T) -> bool { |
| 46 | self.0 == other.as_ref() |
| 47 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 48 | } |
| 49 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 50 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 51 | pub mod parsing { |
| 52 | use super::*; |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 53 | use nom::IResult; |
| 54 | use space::skip_whitespace; |
| 55 | use unicode_xid::UnicodeXID; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 56 | |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 57 | pub fn ident(input: &str) -> IResult<&str, Ident> { |
| 58 | let (rest, id) = match word(input) { |
| 59 | IResult::Done(rest, id) => (rest, id), |
| 60 | IResult::Error => return IResult::Error, |
| 61 | }; |
| 62 | |
| 63 | match id.as_ref() { |
| 64 | // From https://doc.rust-lang.org/grammar.html#keywords |
| 65 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | |
| 66 | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | |
| 67 | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | |
| 68 | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | |
| 69 | "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | |
| 70 | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | |
| 71 | "while" | "yield" => IResult::Error, |
| 72 | _ => IResult::Done(rest, id), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn word(mut input: &str) -> IResult<&str, Ident> { |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 77 | input = skip_whitespace(input); |
| 78 | |
| 79 | let mut chars = input.char_indices(); |
| 80 | match chars.next() { |
| 81 | Some((_, ch)) if UnicodeXID::is_xid_start(ch) || ch == '_' => {} |
| 82 | _ => return IResult::Error, |
| 83 | } |
| 84 | |
| 85 | while let Some((i, ch)) = chars.next() { |
| 86 | if !UnicodeXID::is_xid_continue(ch) { |
| 87 | return IResult::Done(&input[i..], input[..i].into()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | IResult::Done("", input.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 92 | } |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame^] | 93 | |
| 94 | pub fn wordlike(mut input: &str) -> IResult<&str, Ident> { |
| 95 | input = skip_whitespace(input); |
| 96 | |
| 97 | for (i, ch) in input.char_indices() { |
| 98 | if !UnicodeXID::is_xid_start(ch) && !UnicodeXID::is_xid_continue(ch) { |
| 99 | return if i == 0 { |
| 100 | IResult::Error |
| 101 | } else { |
| 102 | IResult::Done(&input[i..], input[..i].into()) |
| 103 | }; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | IResult::Done("", input.into()) |
| 108 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 109 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 110 | |
| 111 | #[cfg(feature = "printing")] |
| 112 | mod printing { |
| 113 | use super::*; |
| 114 | use quote::{Tokens, ToTokens}; |
| 115 | |
| 116 | impl ToTokens for Ident { |
| 117 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 118 | tokens.append(self.as_ref()) |
| 119 | } |
| 120 | } |
| 121 | } |