1#[cfg(feature = "parsing")]
2use crate::buffer::Cursor;
3#[cfg(feature = "parsing")]
4use crate::lookahead;
5#[cfg(feature = "parsing")]
6use crate::parse::{Parse, ParseStream, Result};
7#[cfg(feature = "parsing")]
8use crate::token::Token;
9use unicode_xid::UnicodeXID;
10
11pub use proc_macro2::Ident;
12
13#[cfg(feature = "parsing")]
14#[doc(hidden)]
15#[allow(non_snake_case)]
16pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
17 match marker {}
18}
19
20#[cfg(feature = "parsing")]
21fn accept_as_ident(ident: &Ident) -> bool {
22 match ident.to_string().as_str() {
23 "_" |
24 "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")]
39impl Parse for Ident {
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")]
53impl Token for Ident {
54 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
62 fn display() -> &'static str {
63 "identifier"
64 }
65}
66
67macro_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
77ident_from_token!(self);
78ident_from_token!(Self);
79ident_from_token!(super);
80ident_from_token!(crate);
81ident_from_token!(extern);
82
83impl From<Token![_]> for Ident {
84 fn from(token: Token![_]) -> Ident {
85 Ident::new("_", token.span)
86 }
87}
88
89pub 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}