David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 1 | use std::borrow::Cow; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 2 | use std::cmp::Ordering; |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 3 | use std::fmt::{self, Display}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 4 | use std::hash::{Hash, Hasher}; |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 5 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 6 | use proc_macro2::Symbol; |
| 7 | |
| 8 | use Span; |
| 9 | |
| 10 | #[derive(Clone)] |
| 11 | pub struct Ident { |
| 12 | pub sym: Symbol, |
| 13 | pub span: Span, |
| 14 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 15 | |
| 16 | impl Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 17 | pub fn new(sym: Symbol, span: Span) -> Self { |
| 18 | Ident { |
| 19 | sym: sym, |
| 20 | span: span, |
| 21 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 22 | } |
| 23 | } |
| 24 | |
| 25 | impl<'a> From<&'a str> for Ident { |
| 26 | fn from(s: &str) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 27 | Ident::new(s.into(), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 31 | impl<'a> From<Cow<'a, str>> for Ident { |
| 32 | fn from(s: Cow<'a, str>) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 33 | Ident::new(s[..].into(), Span::default()) |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 37 | impl From<String> for Ident { |
| 38 | fn from(s: String) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 39 | Ident::new(s[..].into(), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
David Tolnay | c168310 | 2016-09-27 08:17:58 -0700 | [diff] [blame] | 43 | impl From<usize> for Ident { |
| 44 | fn from(u: usize) -> Self { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 45 | Ident::new(u.to_string()[..].into(), Span::default()) |
David Tolnay | c168310 | 2016-09-27 08:17:58 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 49 | impl AsRef<str> for Ident { |
| 50 | fn as_ref(&self) -> &str { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 51 | self.sym.as_str() |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Display for Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 56 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 57 | self.sym.as_str().fmt(formatter) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl fmt::Debug for Ident { |
| 62 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 63 | fmt::Debug::fmt(self.sym.as_str(), formatter) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 66 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 67 | impl<T: ?Sized> PartialEq<T> for Ident |
| 68 | where T: AsRef<str> |
| 69 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 70 | fn eq(&self, other: &T) -> bool { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 71 | self.as_ref() == other.as_ref() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Eq for Ident {} |
| 76 | |
| 77 | impl PartialOrd for Ident { |
| 78 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 79 | Some(self.cmp(other)) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Ord for Ident { |
| 84 | fn cmp(&self, other: &Ident) -> Ordering { |
| 85 | self.as_ref().cmp(other.as_ref()) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Hash for Ident { |
| 90 | fn hash<H: Hasher>(&self, h: &mut H) { |
| 91 | self.as_ref().hash(h) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 92 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 93 | } |
| 94 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 95 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 96 | pub mod parsing { |
| 97 | use super::*; |
David Tolnay | 5fe14fc | 2017-01-27 16:22:08 -0800 | [diff] [blame] | 98 | use synom::IResult; |
| 99 | use synom::space::skip_whitespace; |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 100 | use unicode_xid::UnicodeXID; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 101 | |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 102 | pub fn ident(input: &str) -> IResult<&str, Ident> { |
| 103 | let (rest, id) = match word(input) { |
| 104 | IResult::Done(rest, id) => (rest, id), |
| 105 | IResult::Error => return IResult::Error, |
| 106 | }; |
| 107 | |
| 108 | match id.as_ref() { |
| 109 | // From https://doc.rust-lang.org/grammar.html#keywords |
| 110 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | |
| 111 | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | |
| 112 | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | |
| 113 | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | |
| 114 | "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | |
| 115 | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | |
| 116 | "while" | "yield" => IResult::Error, |
| 117 | _ => IResult::Done(rest, id), |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub fn word(mut input: &str) -> IResult<&str, Ident> { |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 122 | input = skip_whitespace(input); |
| 123 | |
| 124 | let mut chars = input.char_indices(); |
| 125 | match chars.next() { |
| 126 | Some((_, ch)) if UnicodeXID::is_xid_start(ch) || ch == '_' => {} |
| 127 | _ => return IResult::Error, |
| 128 | } |
| 129 | |
David Tolnay | 02a8d47 | 2017-02-19 12:59:44 -0800 | [diff] [blame] | 130 | for (i, ch) in chars { |
David Tolnay | e26baba | 2016-10-24 22:05:57 -0700 | [diff] [blame] | 131 | if !UnicodeXID::is_xid_continue(ch) { |
| 132 | return IResult::Done(&input[i..], input[..i].into()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | IResult::Done("", input.into()) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 137 | } |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 138 | |
David Tolnay | b19657b | 2016-10-30 12:48:32 -0700 | [diff] [blame] | 139 | #[cfg(feature = "full")] |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 140 | pub fn wordlike(mut input: &str) -> IResult<&str, Ident> { |
| 141 | input = skip_whitespace(input); |
| 142 | |
| 143 | for (i, ch) in input.char_indices() { |
| 144 | if !UnicodeXID::is_xid_start(ch) && !UnicodeXID::is_xid_continue(ch) { |
| 145 | return if i == 0 { |
David Tolnay | 05120ef | 2017-03-12 18:29:26 -0700 | [diff] [blame] | 146 | IResult::Error |
| 147 | } else { |
| 148 | IResult::Done(&input[i..], input[..i].into()) |
| 149 | }; |
David Tolnay | 3903944 | 2016-10-30 11:25:21 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | IResult::Done("", input.into()) |
| 154 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 155 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 156 | |
| 157 | #[cfg(feature = "printing")] |
| 158 | mod printing { |
| 159 | use super::*; |
| 160 | use quote::{Tokens, ToTokens}; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 161 | use proc_macro2::{TokenTree, TokenKind}; |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 162 | |
| 163 | impl ToTokens for Ident { |
| 164 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame^] | 165 | tokens.append(TokenTree { |
| 166 | span: self.span.0, |
| 167 | kind: TokenKind::Word(self.sym), |
| 168 | }) |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |