1#[cfg(feature = "parsing")]
2use buffer::Cursor;
3#[cfg(feature = "parsing")]
4use lookahead;
5#[cfg(feature = "parsing")]
6use parse::{Parse, ParseStream, Result};
7#[cfg(feature = "parsing")]
8use token::Token;
9
10pub use proc_macro2::Ident;
11
12#[cfg(feature = "parsing")]
13#[doc(hidden)]
14#[allow(non_snake_case)]
15pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
16 match marker {}
17}
18
19#[cfg(feature = "parsing")]
20fn accept_as_ident(ident: &Ident) -> bool {
21 match ident.to_string().as_str() {
22 "_"
23 | "abstract" | "as" | "become" | "box" | "break" | "const"
27 | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final"
28 | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match"
29 | "mod" | "move" | "mut" | "override" | "priv" | "pub"
30 | "ref" | "return" | "Self" | "self" | "static" | "struct"
31 | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use"
32 | "virtual" | "where" | "while" | "yield" => false,
33 _ => true,
34 }
35}
36
37#[cfg(feature = "parsing")]
38impl Parse for Ident {
39 fn parse(input: ParseStream) -> Result<Self> {
40 input.step(|cursor| {
41 if let Some((ident, rest)) = cursor.ident() {
42 if accept_as_ident(&ident) {
43 return Ok((ident, rest));
44 }
45 }
46 Err(cursor.error("expected identifier"))
47 })
48 }
49}
50
51#[cfg(feature = "parsing")]
52impl Token for Ident {
53 fn peek(cursor: Cursor) -> bool {
54 if let Some((ident, _rest)) = cursor.ident() {
55 accept_as_ident(&ident)
56 } else {
57 false
58 }
59 }
60
61 fn display() -> &'static str {
62 "identifier"
63 }
64}
65
66macro_rules! ident_from_token {
67 ($token:ident) => {
68 impl From<Token![$token]> for Ident {
69 fn from(token: Token![$token]) -> Ident {
70 Ident::new(stringify!($token), token.span)
71 }
72 }
73 };
74}
75
76ident_from_token!(self);
77ident_from_token!(Self);
78ident_from_token!(super);
79ident_from_token!(crate);
80ident_from_token!(extern);
81
82impl From<Token![_]> for Ident {
83 fn from(token: Token![_]) -> Ident {
84 Ident::new("_", token.span)
85 }
86}