1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#[cfg(feature = "parsing")]
use crate::lookahead;

pub use proc_macro2::Ident;

#[cfg(not(doc))] // rustdoc bug: https://github.com/rust-lang/rust/issues/105735
#[cfg(feature = "parsing")]
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
    match marker {}
}

macro_rules! ident_from_token {
    ($token:ident) => {
        impl From<Token![$token]> for Ident {
            fn from(token: Token![$token]) -> Ident {
                Ident::new(stringify!($token), token.span)
            }
        }
    };
}

ident_from_token!(self);
ident_from_token!(Self);
ident_from_token!(super);
ident_from_token!(crate);
ident_from_token!(extern);

impl From<Token![_]> for Ident {
    fn from(token: Token![_]) -> Ident {
        Ident::new("_", token.span)
    }
}

pub(crate) fn xid_ok(symbol: &str) -> bool {
    let mut chars = symbol.chars();
    let first = chars.next().unwrap();
    if !(first == '_' || unicode_ident::is_xid_start(first)) {
        return false;
    }
    for ch in chars {
        if !unicode_ident::is_xid_continue(ch) {
            return false;
        }
    }
    true
}

#[cfg(feature = "parsing")]
mod parsing {
    use crate::buffer::Cursor;
    use crate::parse::{Parse, ParseStream, Result};
    use crate::token::Token;
    use proc_macro2::Ident;

    fn accept_as_ident(ident: &Ident) -> bool {
        match ident.to_string().as_str() {
            "_" |
            // Based on https://doc.rust-lang.org/1.65.0/reference/keywords.html
            "abstract" | "as" | "async" | "await" | "become" | "box" | "break" |
            "const" | "continue" | "crate" | "do" | "dyn" | "else" | "enum" |
            "extern" | "false" | "final" | "fn" | "for" | "if" | "impl" | "in" |
            "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" |
            "override" | "priv" | "pub" | "ref" | "return" | "Self" | "self" |
            "static" | "struct" | "super" | "trait" | "true" | "try" | "type" |
            "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" |
            "while" | "yield" => false,
            _ => true,
        }
    }

    #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
    impl Parse for Ident {
        fn parse(input: ParseStream) -> Result<Self> {
            input.step(|cursor| {
                if let Some((ident, rest)) = cursor.ident() {
                    if accept_as_ident(&ident) {
                        Ok((ident, rest))
                    } else {
                        Err(cursor.error(format_args!(
                            "expected identifier, found keyword `{}`",
                            ident,
                        )))
                    }
                } else {
                    Err(cursor.error("expected identifier"))
                }
            })
        }
    }

    impl Token for Ident {
        fn peek(cursor: Cursor) -> bool {
            if let Some((ident, _rest)) = cursor.ident() {
                accept_as_ident(&ident)
            } else {
                false
            }
        }

        fn display() -> &'static str {
            "identifier"
        }
    }
}