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 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
40 impl Parse for Ident {
parse(input: ParseStream) -> Result<Self>41 fn parse(input: ParseStream) -> Result<Self> {
42 input.step(|cursor| {
43 if let Some((ident, rest)) = cursor.ident() {
44 if accept_as_ident(&ident) {
45 return Ok((ident, rest));
46 }
47 }
48 Err(cursor.error("expected identifier"))
49 })
50 }
51 }
52
53 #[cfg(feature = "parsing")]
54 impl Token for Ident {
peek(cursor: Cursor) -> bool55 fn peek(cursor: Cursor) -> bool {
56 if let Some((ident, _rest)) = cursor.ident() {
57 accept_as_ident(&ident)
58 } else {
59 false
60 }
61 }
62
display() -> &'static str63 fn display() -> &'static str {
64 "identifier"
65 }
66 }
67
68 macro_rules! ident_from_token {
69 ($token:ident) => {
70 impl From<Token![$token]> for Ident {
71 fn from(token: Token![$token]) -> Ident {
72 Ident::new(stringify!($token), token.span)
73 }
74 }
75 };
76 }
77
78 ident_from_token!(self);
79 ident_from_token!(Self);
80 ident_from_token!(super);
81 ident_from_token!(crate);
82 ident_from_token!(extern);
83
84 impl From<Token![_]> for Ident {
from(token: Token![_]) -> Ident85 fn from(token: Token![_]) -> Ident {
86 Ident::new("_", token.span)
87 }
88 }
89
xid_ok(symbol: &str) -> bool90 pub fn xid_ok(symbol: &str) -> bool {
91 let mut chars = symbol.chars();
92 let first = chars.next().unwrap();
93 if !(UnicodeXID::is_xid_start(first) || first == '_') {
94 return false;
95 }
96 for ch in chars {
97 if !UnicodeXID::is_xid_continue(ch) {
98 return false;
99 }
100 }
101 true
102 }
103