1 #[cfg(feature = "parsing")]
2 use crate::buffer::Cursor;
3 #[cfg(feature = "parsing")]
4 use crate::lookahead;
5 #[cfg(feature = "parsing")]
6 use crate::parse::{Parse, ParseStream, Result};
7 #[cfg(feature = "parsing")]
8 use crate::token::Token;
9 use unicode_xid::UnicodeXID;
10 
11 pub use proc_macro2::Ident;
12 
13 #[cfg(feature = "parsing")]
14 #[doc(hidden)]
15 #[allow(non_snake_case)]
Ident(marker: lookahead::TokenMarker) -> Ident16 pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
17     match marker {}
18 }
19 
20 #[cfg(feature = "parsing")]
accept_as_ident(ident: &Ident) -> bool21 fn accept_as_ident(ident: &Ident) -> bool {
22     match ident.to_string().as_str() {
23         "_" |
24         // Based on https://doc.rust-lang.org/grammar.html#keywords
25         // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md
26         // and https://github.com/rust-lang/rfcs/blob/master/text/2420-unreserve-proc.md
27         "abstract" | "as" | "become" | "box" | "break" | "const" | "continue" |
28         "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" |
29         "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" |
30         "mod" | "move" | "mut" | "override" | "priv" | "pub" | "ref" |
31         "return" | "Self" | "self" | "static" | "struct" | "super" | "trait" |
32         "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" |
33         "where" | "while" | "yield" => false,
34         _ => true,
35     }
36 }
37 
38 #[cfg(feature = "parsing")]
39 impl Parse for Ident {
parse(input: ParseStream) -> Result<Self>40     fn parse(input: ParseStream) -> Result<Self> {
41         input.step(|cursor| {
42             if let Some((ident, rest)) = cursor.ident() {
43                 if accept_as_ident(&ident) {
44                     return Ok((ident, rest));
45                 }
46             }
47             Err(cursor.error("expected identifier"))
48         })
49     }
50 }
51 
52 #[cfg(feature = "parsing")]
53 impl Token for Ident {
peek(cursor: Cursor) -> bool54     fn peek(cursor: Cursor) -> bool {
55         if let Some((ident, _rest)) = cursor.ident() {
56             accept_as_ident(&ident)
57         } else {
58             false
59         }
60     }
61 
display() -> &'static str62     fn display() -> &'static str {
63         "identifier"
64     }
65 }
66 
67 macro_rules! ident_from_token {
68     ($token:ident) => {
69         impl From<Token![$token]> for Ident {
70             fn from(token: Token![$token]) -> Ident {
71                 Ident::new(stringify!($token), token.span)
72             }
73         }
74     };
75 }
76 
77 ident_from_token!(self);
78 ident_from_token!(Self);
79 ident_from_token!(super);
80 ident_from_token!(crate);
81 ident_from_token!(extern);
82 
83 impl From<Token![_]> for Ident {
from(token: Token![_]) -> Ident84     fn from(token: Token![_]) -> Ident {
85         Ident::new("_", token.span)
86     }
87 }
88 
xid_ok(symbol: &str) -> bool89 pub fn xid_ok(symbol: &str) -> bool {
90     let mut chars = symbol.chars();
91     let first = chars.next().unwrap();
92     if !(UnicodeXID::is_xid_start(first) || first == '_') {
93         return false;
94     }
95     for ch in chars {
96         if !UnicodeXID::is_xid_continue(ch) {
97             return false;
98         }
99     }
100     true
101 }
102