1 #[cfg(feature = "parsing")]
2 use buffer::Cursor;
3 #[cfg(feature = "parsing")]
4 use parse::{Parse, ParseStream, Result};
5 #[cfg(feature = "parsing")]
6 use token::Token;
7 #[cfg(feature = "parsing")]
8 use {lookahead, private};
9
10 pub use proc_macro2::Ident;
11
12 #[cfg(feature = "parsing")]
13 #[doc(hidden)]
14 #[allow(non_snake_case)]
Ident(marker: lookahead::TokenMarker) -> Ident15 pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
16 match marker {}
17 }
18
19 #[cfg(feature = "parsing")]
accept_as_ident(ident: &Ident) -> bool20 fn accept_as_ident(ident: &Ident) -> bool {
21 match ident.to_string().as_str() {
22 "_" |
23 // Based on https://doc.rust-lang.org/grammar.html#keywords
24 // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md
25 // and https://github.com/rust-lang/rfcs/blob/master/text/2420-unreserve-proc.md
26 "abstract" | "as" | "become" | "box" | "break" | "const" | "continue" |
27 "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" |
28 "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" |
29 "mod" | "move" | "mut" | "override" | "priv" | "pub" | "ref" |
30 "return" | "Self" | "self" | "static" | "struct" | "super" | "trait" |
31 "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" |
32 "where" | "while" | "yield" => false,
33 _ => true,
34 }
35 }
36
37 #[cfg(feature = "parsing")]
38 impl Parse for Ident {
parse(input: ParseStream) -> Result<Self>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")]
52 impl Token for Ident {
peek(cursor: Cursor) -> bool53 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
display() -> &'static str61 fn display() -> &'static str {
62 "identifier"
63 }
64 }
65
66 macro_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
76 ident_from_token!(self);
77 ident_from_token!(Self);
78 ident_from_token!(super);
79 ident_from_token!(crate);
80 ident_from_token!(extern);
81
82 impl From<Token![_]> for Ident {
from(token: Token![_]) -> Ident83 fn from(token: Token![_]) -> Ident {
84 Ident::new("_", token.span)
85 }
86 }
87
88 #[cfg(feature = "parsing")]
89 impl private {
90 #[cfg(syn_can_use_associated_constants)]
peek_any_ident(input: ParseStream) -> bool91 pub fn peek_any_ident(input: ParseStream) -> bool {
92 use ext::IdentExt;
93 input.peek(Ident::peek_any)
94 }
95
96 #[cfg(not(syn_can_use_associated_constants))]
peek_any_ident(input: ParseStream) -> bool97 pub fn peek_any_ident(input: ParseStream) -> bool {
98 input.cursor().ident().is_some()
99 }
100 }
101