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; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 10 | use tokens; |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 11 | |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 12 | /// A word of Rust code, such as a keyword or variable name. |
| 13 | /// |
| 14 | /// An identifier consists of at least one Unicode code point, the first of |
| 15 | /// which has the XID_Start property and the rest of which have the XID_Continue |
| 16 | /// property. An underscore may be used as the first character as long as it is |
| 17 | /// not the only character. |
| 18 | /// |
| 19 | /// - The empty string is not an identifier. Use `Option<Ident>`. |
| 20 | /// - An underscore by itself is not an identifier. Use |
| 21 | /// `syn::tokens::Underscore` instead. |
| 22 | /// - A lifetime is not an identifier. Use `syn::Lifetime` instead. |
| 23 | /// |
| 24 | /// An identifier constructed with `Ident::new` is permitted to be a Rust |
| 25 | /// keyword, though parsing an identifier with `syn!(Ident)` rejects Rust |
| 26 | /// keywords. |
| 27 | #[derive(Copy, Clone, Debug)] |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 28 | pub struct Ident { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 29 | pub sym: Term, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 30 | pub span: Span, |
| 31 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 32 | |
| 33 | impl Ident { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 34 | pub fn new(sym: Term, span: Span) -> Self { |
David Tolnay | 570695e | 2017-06-03 16:15:13 -0700 | [diff] [blame] | 35 | let s = sym.as_str(); |
| 36 | |
| 37 | if s.is_empty() { |
| 38 | panic!("ident is not allowed to be empty; use Option<Ident>"); |
| 39 | } |
| 40 | |
| 41 | if s.starts_with('\'') { |
| 42 | panic!("ident is not allowed to be a lifetime; use syn::Lifetime"); |
| 43 | } |
| 44 | |
| 45 | if s == "_" { |
| 46 | panic!("`_` is not a valid ident; use syn::tokens::Underscore"); |
| 47 | } |
| 48 | |
| 49 | fn xid_ok(s: &str) -> bool { |
| 50 | let mut chars = s.chars(); |
| 51 | let first = chars.next().unwrap(); |
| 52 | if !(UnicodeXID::is_xid_start(first) || first == '_') { |
| 53 | return false; |
| 54 | } |
| 55 | for ch in chars { |
| 56 | if !UnicodeXID::is_xid_continue(ch) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | true |
| 61 | } |
| 62 | |
| 63 | fn integer_ok(s: &str) -> bool { |
| 64 | s.bytes().all(|digit| digit >= b'0' && digit <= b'9') |
| 65 | } |
| 66 | |
| 67 | if !(xid_ok(s) || integer_ok(s)) { |
| 68 | panic!("{:?} is not a valid ident", s); |
| 69 | } |
| 70 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 71 | Ident { |
| 72 | sym: sym, |
| 73 | span: span, |
| 74 | } |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | impl<'a> From<&'a str> for Ident { |
| 79 | fn from(s: &str) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 80 | Ident::new(Term::intern(s), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 84 | impl From<tokens::Self_> for Ident { |
| 85 | fn from(tok: tokens::Self_) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 86 | Ident::new(Term::intern("self"), tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | impl From<tokens::CapSelf> for Ident { |
| 91 | fn from(tok: tokens::CapSelf) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 92 | Ident::new(Term::intern("Self"), tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | impl From<tokens::Super> for Ident { |
| 97 | fn from(tok: tokens::Super) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 98 | Ident::new(Term::intern("super"), tok.0) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 102 | impl<'a> From<Cow<'a, str>> for Ident { |
| 103 | fn from(s: Cow<'a, str>) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 104 | Ident::new(Term::intern(&s), Span::default()) |
David Tolnay | f733f7e | 2016-11-25 10:36:25 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 108 | impl From<String> for Ident { |
| 109 | fn from(s: String) -> Self { |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 110 | Ident::new(Term::intern(&s), Span::default()) |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 114 | impl AsRef<str> for Ident { |
| 115 | fn as_ref(&self) -> &str { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 116 | self.sym.as_str() |
David Tolnay | 42f5029 | 2016-09-04 13:54:21 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | impl Display for Ident { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 121 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 122 | self.sym.as_str().fmt(formatter) |
| 123 | } |
| 124 | } |
| 125 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 126 | impl<T: ?Sized> PartialEq<T> for Ident |
| 127 | where T: AsRef<str> |
| 128 | { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 129 | fn eq(&self, other: &T) -> bool { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 130 | self.as_ref() == other.as_ref() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl Eq for Ident {} |
| 135 | |
| 136 | impl PartialOrd for Ident { |
| 137 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 138 | Some(self.cmp(other)) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl Ord for Ident { |
| 143 | fn cmp(&self, other: &Ident) -> Ordering { |
| 144 | self.as_ref().cmp(other.as_ref()) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl Hash for Ident { |
| 149 | fn hash<H: Hasher>(&self, h: &mut H) { |
| 150 | self.as_ref().hash(h) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 151 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 154 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 155 | pub mod parsing { |
| 156 | use super::*; |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 157 | use synom::{Synom, PResult, Cursor, parse_error}; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 158 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 159 | impl Synom for Ident { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 160 | fn parse(input: Cursor) -> PResult<Self> { |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 161 | let (rest, span, sym) = match input.word() { |
| 162 | Some(word) => word, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 163 | _ => return parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 164 | }; |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 165 | if sym.as_str().starts_with('\'') { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 166 | return parse_error(); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 167 | } |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 168 | match sym.as_str() { |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 169 | // From https://doc.rust-lang.org/grammar.html#keywords |
| 170 | "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" | |
| 171 | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | |
| 172 | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | |
| 173 | "mut" | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | |
| 174 | "return" | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | |
| 175 | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 176 | "while" | "yield" => return parse_error(), |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 177 | _ => {} |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 178 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 179 | |
Michael Layzell | 589a8f4 | 2017-06-02 19:47:01 -0400 | [diff] [blame] | 180 | Ok((rest, Ident { |
| 181 | span: Span(span), |
| 182 | sym: sym, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 183 | })) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | fn description() -> Option<&'static str> { |
| 187 | Some("identifier") |
David Tolnay | 05f462f | 2016-10-24 22:19:42 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 190 | } |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 191 | |
| 192 | #[cfg(feature = "printing")] |
| 193 | mod printing { |
| 194 | use super::*; |
| 195 | use quote::{Tokens, ToTokens}; |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 196 | use proc_macro2::{TokenTree, TokenNode}; |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 197 | |
| 198 | impl ToTokens for Ident { |
| 199 | fn to_tokens(&self, tokens: &mut Tokens) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 200 | tokens.append(TokenTree { |
| 201 | span: self.span.0, |
Alex Crichton | f9e8f1a | 2017-07-05 18:20:44 -0700 | [diff] [blame] | 202 | kind: TokenNode::Term(self.sym), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 203 | }) |
David Tolnay | 2646907 | 2016-09-04 13:59:48 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |