1#[cfg(feature = "parsing")]
2use crate::lookahead;
3
4pub use proc_macro2::Ident;
5
6#[cfg(feature = "parsing")]
7#[doc(hidden)]
#[allow(non_snake_case)]
pub(crate) fn Ident(marker: lookahead::TokenMarker) -> Ident {
match marker {}
}pub_if_not_doc! {
8 #[doc(hidden)]
9 #[allow(non_snake_case)]
10 pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
11 match marker {}
12 }
13}
14
15macro_rules! ident_from_token {
16 ($token:ident) => {
17 impl From<Token![$token]> for Ident {
18 fn from(token: Token![$token]) -> Ident {
19 Ident::new(stringify!($token), token.span)
20 }
21 }
22 };
23}
24
25impl From<crate::token::SelfValue> for Ident {
fn from(token: crate::token::SelfValue) -> Ident {
Ident::new("self", token.span)
}
}ident_from_token!(self);
26impl From<crate::token::SelfType> for Ident {
fn from(token: crate::token::SelfType) -> Ident {
Ident::new("Self", token.span)
}
}ident_from_token!(Self);
27impl From<crate::token::Super> for Ident {
fn from(token: crate::token::Super) -> Ident {
Ident::new("super", token.span)
}
}ident_from_token!(super);
28impl From<crate::token::Crate> for Ident {
fn from(token: crate::token::Crate) -> Ident {
Ident::new("crate", token.span)
}
}ident_from_token!(crate);
29impl From<crate::token::Extern> for Ident {
fn from(token: crate::token::Extern) -> Ident {
Ident::new("extern", token.span)
}
}ident_from_token!(extern);
30
31impl From<crate::token::UnderscoreToken![_]> for Ident {
32 fn from(token: crate::token::UnderscoreToken![_]) -> Ident {
33 Ident::new("_", token.span)
34 }
35}
36
37pub(crate) fn xid_ok(symbol: &str) -> bool {
38 let mut chars = symbol.chars();
39 let first = chars.next().unwrap();
40 if !(first == '_' || unicode_ident::is_xid_start(first)) {
41 return false;
42 }
43 for ch in chars {
44 if !unicode_ident::is_xid_continue(ch) {
45 return false;
46 }
47 }
48 true
49}
50
51#[cfg(feature = "parsing")]
52mod parsing {
53 use crate::buffer::Cursor;
54 use crate::error::Result;
55 use crate::parse::{Parse, ParseStream};
56 use crate::token::Token;
57 use alloc::string::ToString;
58 use proc_macro2::Ident;
59
60 fn accept_as_ident(ident: &Ident) -> bool {
61 match ident.to_string().as_str() {
62 "_" |
63 "abstract" | "as" | "async" | "await" | "become" | "box" | "break" |
65 "const" | "continue" | "crate" | "do" | "dyn" | "else" | "enum" |
66 "extern" | "false" | "final" | "fn" | "for" | "if" | "impl" | "in" |
67 "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" |
68 "override" | "priv" | "pub" | "ref" | "return" | "Self" | "self" |
69 "static" | "struct" | "super" | "trait" | "true" | "try" | "type" |
70 "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" |
71 "while" | "yield" => false,
72 _ => true,
73 }
74 }
75
76 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
77 impl Parse for Ident {
78 fn parse(input: ParseStream) -> Result<Self> {
79 input.step(|cursor| {
80 if let Some((ident, rest)) = cursor.ident() {
81 if accept_as_ident(&ident) {
82 Ok((ident, rest))
83 } else {
84 Err(cursor.error(format_args!("expected identifier, found keyword `{0}`", ident)format_args!(
85 "expected identifier, found keyword `{}`",
86 ident,
87 )))
88 }
89 } else {
90 Err(cursor.error("expected identifier"))
91 }
92 })
93 }
94 }
95
96 impl Token for Ident {
97 fn peek(cursor: Cursor) -> bool {
98 if let Some((ident, _rest)) = cursor.ident() {
99 accept_as_ident(&ident)
100 } else {
101 false
102 }
103 }
104
105 fn display() -> &'static str {
106 "identifier"
107 }
108 }
109}