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 | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 6 | use proc_macro2::Term; |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 7 | use unicode_xid::UnicodeXID; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 8 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 9 | use proc_macro2::Span; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 10 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 11 | /// A word of Rust code, such as a keyword or variable name. |
| 12 | /// |
| 13 | /// An identifier consists of at least one Unicode code point, the first of |
| 14 | /// which has the XID_Start property and the rest of which have the XID_Continue |
| 15 | /// property. An underscore may be used as the first character as long as it is |
| 16 | /// not the only character. |
| 17 | /// |
| 18 | /// - The empty string is not an identifier. Use `Option<Ident>`. |
| 19 | /// - An underscore by itself is not an identifier. Use |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 20 | /// `Token![_]` instead. |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 21 | /// - A lifetime is not an identifier. Use `syn::Lifetime` instead. |
| 22 | /// |
| 23 | /// An identifier constructed with `Ident::new` is permitted to be a Rust |
Mikko Rantanen | a87ac03 | 2017-10-30 08:19:00 +0200 | [diff] [blame] | 24 | /// keyword, though parsing input with [`parse`], [`parse_str`] or |
| 25 | /// [`parse_tokens`] rejects Rust keywords. |
| 26 | /// |
| 27 | /// [`parse`]: fn.parse.html |
| 28 | /// [`parse_str`]: fn.parse_str.html |
| 29 | /// [`parse_tokens`]: fn.parse_tokens.html |
| 30 | /// |
| 31 | /// # Examples |
| 32 | /// |
| 33 | /// A new ident can be created from a string using the `Ident::from` function. |
| 34 | /// |
| 35 | /// ```rust |
| 36 | /// extern crate syn; |
| 37 | /// use syn::Ident; |
| 38 | /// # |
| 39 | /// # fn main() { |
| 40 | /// |
| 41 | /// let ident = Ident::from("another_identifier"); |
| 42 | /// |
| 43 | /// # } |
| 44 | /// ``` |
| 45 | /// |
| 46 | /// When the ident is used in Macros 1.1 output, it needs to be turned into |
| 47 | /// a token stream. This is easy to do using the `quote!` macro from the `quote` |
| 48 | /// crate. |
| 49 | /// |
| 50 | /// ```rust |
| 51 | /// # #[macro_use] |
| 52 | /// # extern crate quote; |
| 53 | /// # extern crate syn; |
| 54 | /// # use syn::Ident; |
| 55 | /// # fn main() { |
| 56 | /// # let ident = Ident::from("another_identifier"); |
| 57 | /// # |
| 58 | /// // Create tokens using the ident. |
| 59 | /// let expanded = quote! { let #ident = 10; }; |
| 60 | /// |
| 61 | /// // Derive a new ident from the existing one. |
| 62 | /// let temp_ident = Ident::from(format!("new_{}", ident)); |
| 63 | /// let expanded = quote! { let $temp_ident = 10; }; |
| 64 | /// |
| 65 | /// # } |
| 66 | /// ``` |
| 67 | /// |
| 68 | /// If `syn` is used to parse existing Rust source code, it is often useful to |
| 69 | /// convert the `Ident` to a more generic string data type at some point. The |
| 70 | /// methods `as_ref()` and `to_string()` achieve this. |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 71 | /// |
Mikko Rantanen | a87ac03 | 2017-10-30 08:19:00 +0200 | [diff] [blame] | 72 | /// ```rust |
| 73 | /// # use syn::Ident; |
| 74 | /// # let ident = Ident::from("another_identifier"); |
| 75 | /// # |
| 76 | /// // Examine the ident as a &str. |
| 77 | /// let ident_str = ident.as_ref(); |
| 78 | /// if ident_str.len() > 60 { |
| 79 | /// println!("Very long identifier: {}", ident_str) |
| 80 | /// } |
| 81 | /// |
| 82 | /// // Create a String from the ident. |
| 83 | /// let ident_string = ident.to_string(); |
| 84 | /// give_away(ident_string); |
| 85 | /// |
| 86 | /// fn give_away(s: String) { /* ... */ } |
| 87 | /// ``` |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 88 | #[derive(Copy, Clone, Debug)] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 89 | pub struct Ident { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 90 | term: Term, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 91 | pub span: Span, |
| 92 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 93 | |
| 94 | impl Ident { |
Mikko Rantanen | a87ac03 | 2017-10-30 08:19:00 +0200 | [diff] [blame] | 95 | /// Creates a new `Ident` from the structured items. This is mainly used |
| 96 | /// by the parser to create `Ident`s from existing Rust source code. |
| 97 | /// |
| 98 | /// Creating new `Ident`s programmatically is easier with `Ident::from`. |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 99 | pub fn new(s: &str, span: Span) -> Self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 100 | if s.is_empty() { |
| 101 | panic!("ident is not allowed to be empty; use Option<Ident>"); |
| 102 | } |
| 103 | |
| 104 | if s.starts_with('\'') { |
| 105 | panic!("ident is not allowed to be a lifetime; use syn::Lifetime"); |
| 106 | } |
| 107 | |
| 108 | if s == "_" { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 109 | panic!("`_` is not a valid ident; use syn::token::Underscore"); |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 110 | } |
| 111 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 112 | if s.bytes().all(|digit| digit >= b'0' && digit <= b'9') { |
| 113 | panic!("ident cannot be a number, use syn::Index instead"); |
| 114 | } |
| 115 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 116 | fn xid_ok(s: &str) -> bool { |
| 117 | let mut chars = s.chars(); |
| 118 | let first = chars.next().unwrap(); |
| 119 | if !(UnicodeXID::is_xid_start(first) || first == '_') { |
| 120 | return false; |
| 121 | } |
| 122 | for ch in chars { |
| 123 | if !UnicodeXID::is_xid_continue(ch) { |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | true |
| 128 | } |
| 129 | |
David Tolnay | 1498201 | 2017-12-29 00:49:51 -0500 | [diff] [blame] | 130 | if !xid_ok(s) { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 131 | panic!("{:?} is not a valid ident", s); |
| 132 | } |
| 133 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 134 | Ident { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 135 | term: Term::intern(s), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 136 | span: span, |
| 137 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | impl<'a> From<&'a str> for Ident { |
| 142 | fn from(s: &str) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 143 | Ident::new(s, Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 147 | impl From<Token![self]> for Ident { |
| 148 | fn from(tok: Token![self]) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 149 | Ident::new("self", tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 153 | impl From<Token![Self]> for Ident { |
| 154 | fn from(tok: Token![Self]) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 155 | Ident::new("Self", tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 159 | impl From<Token![super]> for Ident { |
| 160 | fn from(tok: Token![super]) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 161 | Ident::new("super", tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
David Tolnay | dfc6df7 | 2017-12-25 23:44:08 -0500 | [diff] [blame] | 165 | impl From<Token![crate]> for Ident { |
| 166 | fn from(tok: Token![crate]) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 167 | Ident::new("crate", tok.0) |
David Tolnay | dfc6df7 | 2017-12-25 23:44:08 -0500 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 171 | impl<'a> From<Cow<'a, str>> for Ident { |
| 172 | fn from(s: Cow<'a, str>) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 173 | Ident::new(&s, Span::default()) |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 177 | impl From<String> for Ident { |
| 178 | fn from(s: String) -> Self { |
David Tolnay | eb771d7 | 2017-12-27 22:11:06 -0500 | [diff] [blame] | 179 | Ident::new(&s, Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 183 | impl AsRef<str> for Ident { |
| 184 | fn as_ref(&self) -> &str { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 185 | self.term.as_str() |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | impl Display for Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 190 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 191 | self.term.as_str().fmt(formatter) |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 195 | impl<T: ?Sized> PartialEq<T> for Ident |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 196 | where |
| 197 | T: AsRef<str>, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 198 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 199 | fn eq(&self, other: &T) -> bool { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 200 | self.as_ref() == other.as_ref() |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl Eq for Ident {} |
| 205 | |
| 206 | impl PartialOrd for Ident { |
| 207 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 208 | Some(self.cmp(other)) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | impl Ord for Ident { |
| 213 | fn cmp(&self, other: &Ident) -> Ordering { |
| 214 | self.as_ref().cmp(other.as_ref()) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | impl Hash for Ident { |
| 219 | fn hash<H: Hasher>(&self, h: &mut H) { |
David Tolnay | 85b69a4 | 2017-12-27 20:43:10 -0500 | [diff] [blame] | 220 | self.as_ref().hash(h); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 221 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 222 | } |
| 223 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 224 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 225 | pub mod parsing { |
| 226 | use super::*; |
David Tolnay | c5ab8c6 | 2017-12-26 16:43:39 -0500 | [diff] [blame] | 227 | use synom::Synom; |
| 228 | use cursor::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 229 | use parse_error; |
| 230 | use synom::PResult; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 231 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 232 | impl Synom for Ident { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 233 | fn parse(input: Cursor) -> PResult<Self> { |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 234 | let (span, term, rest) = match input.term() { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 235 | Some(term) => term, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 236 | _ => return parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 237 | }; |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 238 | if term.as_str().starts_with('\'') { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 239 | return parse_error(); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 240 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 241 | match term.as_str() { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 242 | // From https://doc.rust-lang.org/grammar.html#keywords |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 243 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" |
| 244 | | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" |
| 245 | | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" |
| 246 | | "mod" | "move" | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" |
| 247 | | "pure" | "ref" | "return" | "Self" | "self" | "sizeof" | "static" | "struct" |
| 248 | | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" |
| 249 | | "virtual" | "where" | "while" | "yield" => return parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 250 | _ => {} |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 251 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 252 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 253 | Ok(( |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 254 | Ident { |
| 255 | span: span, |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 256 | term: term, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 257 | }, |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 258 | rest, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 259 | )) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | fn description() -> Option<&'static str> { |
| 263 | Some("identifier") |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 266 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 267 | |
| 268 | #[cfg(feature = "printing")] |
| 269 | mod printing { |
| 270 | use super::*; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 271 | use quote::{ToTokens, Tokens}; |
| 272 | use proc_macro2::{TokenNode, TokenTree}; |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 273 | |
| 274 | impl ToTokens for Ident { |
| 275 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 276 | tokens.append(TokenTree { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 277 | span: self.span, |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 278 | kind: TokenNode::Term(self.term), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 279 | }) |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |