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 | |
| 9 | use Span; |
| 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. |
| 71 | /// |
| 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 { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 90 | pub sym: 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 | |
| 96 | /// Creates a new `Ident` from the structured items. This is mainly used |
| 97 | /// by the parser to create `Ident`s from existing Rust source code. |
| 98 | /// |
| 99 | /// Creating new `Ident`s programmatically is easier with `Ident::from`. |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 100 | pub fn new(sym: Term, span: Span) -> Self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 101 | let s = sym.as_str(); |
| 102 | |
| 103 | if s.is_empty() { |
| 104 | panic!("ident is not allowed to be empty; use Option<Ident>"); |
| 105 | } |
| 106 | |
| 107 | if s.starts_with('\'') { |
| 108 | panic!("ident is not allowed to be a lifetime; use syn::Lifetime"); |
| 109 | } |
| 110 | |
| 111 | if s == "_" { |
| 112 | panic!("`_` is not a valid ident; use syn::tokens::Underscore"); |
| 113 | } |
| 114 | |
| 115 | fn xid_ok(s: &str) -> bool { |
| 116 | let mut chars = s.chars(); |
| 117 | let first = chars.next().unwrap(); |
| 118 | if !(UnicodeXID::is_xid_start(first) || first == '_') { |
| 119 | return false; |
| 120 | } |
| 121 | for ch in chars { |
| 122 | if !UnicodeXID::is_xid_continue(ch) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | true |
| 127 | } |
| 128 | |
| 129 | fn integer_ok(s: &str) -> bool { |
| 130 | s.bytes().all(|digit| digit >= b'0' && digit <= b'9') |
| 131 | } |
| 132 | |
| 133 | if !(xid_ok(s) || integer_ok(s)) { |
| 134 | panic!("{:?} is not a valid ident", s); |
| 135 | } |
| 136 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 137 | Ident { |
| 138 | sym: sym, |
| 139 | span: span, |
| 140 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | impl<'a> From<&'a str> for Ident { |
| 145 | fn from(s: &str) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 146 | Ident::new(Term::intern(s), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 150 | impl From<Token![self]> for Ident { |
| 151 | fn from(tok: Token![self]) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 152 | Ident::new(Term::intern("self"), tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 156 | impl From<Token![Self]> for Ident { |
| 157 | fn from(tok: Token![Self]) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 158 | Ident::new(Term::intern("Self"), tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 162 | impl From<Token![super]> for Ident { |
| 163 | fn from(tok: Token![super]) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 164 | Ident::new(Term::intern("super"), tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
David Tolnay | dfc6df7 | 2017-12-25 23:44:08 -0500 | [diff] [blame] | 168 | impl From<Token![crate]> for Ident { |
| 169 | fn from(tok: Token![crate]) -> Self { |
| 170 | Ident::new(Term::intern("crate"), tok.0) |
| 171 | } |
| 172 | } |
| 173 | |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 174 | impl<'a> From<Cow<'a, str>> for Ident { |
| 175 | fn from(s: Cow<'a, str>) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 176 | Ident::new(Term::intern(&s), Span::default()) |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 180 | impl From<String> for Ident { |
| 181 | fn from(s: String) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 182 | Ident::new(Term::intern(&s), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 186 | impl AsRef<str> for Ident { |
| 187 | fn as_ref(&self) -> &str { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 188 | self.sym.as_str() |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | impl Display for Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 193 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 194 | self.sym.as_str().fmt(formatter) |
| 195 | } |
| 196 | } |
| 197 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 198 | impl<T: ?Sized> PartialEq<T> for Ident |
| 199 | where T: AsRef<str> |
| 200 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 201 | fn eq(&self, other: &T) -> bool { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 202 | self.as_ref() == other.as_ref() |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl Eq for Ident {} |
| 207 | |
| 208 | impl PartialOrd for Ident { |
| 209 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 210 | Some(self.cmp(other)) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | impl Ord for Ident { |
| 215 | fn cmp(&self, other: &Ident) -> Ordering { |
| 216 | self.as_ref().cmp(other.as_ref()) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | impl Hash for Ident { |
| 221 | fn hash<H: Hasher>(&self, h: &mut H) { |
| 222 | self.as_ref().hash(h) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 223 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 224 | } |
| 225 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 226 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 227 | pub mod parsing { |
| 228 | use super::*; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 229 | use synom::{Synom, PResult, Cursor, parse_error}; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 230 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 231 | impl Synom for Ident { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 232 | fn parse(input: Cursor) -> PResult<Self> { |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 233 | let (rest, span, sym) = match input.word() { |
| 234 | Some(word) => word, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 235 | _ => return parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 236 | }; |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 237 | if sym.as_str().starts_with('\'') { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 238 | return parse_error(); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 239 | } |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 240 | match sym.as_str() { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 241 | // From https://doc.rust-lang.org/grammar.html#keywords |
| 242 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | |
| 243 | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | |
| 244 | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | |
| 245 | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | |
| 246 | "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | |
| 247 | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 248 | "while" | "yield" => return parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 249 | _ => {} |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 250 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 251 | |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 252 | Ok((rest, Ident { |
| 253 | span: Span(span), |
| 254 | sym: sym, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 255 | })) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | fn description() -> Option<&'static str> { |
| 259 | Some("identifier") |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 262 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 263 | |
| 264 | #[cfg(feature = "printing")] |
| 265 | mod printing { |
| 266 | use super::*; |
| 267 | use quote::{Tokens, ToTokens}; |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 268 | use proc_macro2::{TokenTree, TokenNode}; |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 269 | |
| 270 | impl ToTokens for Ident { |
| 271 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 272 | tokens.append(TokenTree { |
| 273 | span: self.span.0, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 274 | kind: TokenNode::Term(self.sym), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 275 | }) |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |