blob: adc545f06327cb53de74283bf642bca9d06c2537 [file] [log] [blame]
David Tolnay4fb71232018-08-25 23:14:50 -04001#[cfg(feature = "parsing")]
David Tolnay719ad5a2018-09-02 18:01:15 -07002use buffer::Cursor;
3#[cfg(feature = "parsing")]
David Tolnay4fb71232018-08-25 23:14:50 -04004use lookahead;
David Tolnayf07b3342018-09-01 11:58:11 -07005#[cfg(feature = "parsing")]
6use parse::{Parse, ParseStream, Result};
David Tolnay719ad5a2018-09-02 18:01:15 -07007#[cfg(feature = "parsing")]
8use token::Token;
David Tolnay4fb71232018-08-25 23:14:50 -04009
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}
David Tolnayf07b3342018-09-01 11:58:11 -070018
19#[cfg(feature = "parsing")]
David Tolnay719ad5a2018-09-02 18:01:15 -070020fn 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
jchlapinski04366412018-10-01 01:00:44 +020025 // and https://github.com/rust-lang/rfcs/blob/master/text/2420-unreserve-proc.md
David Tolnay719ad5a2018-09-02 18:01:15 -070026 | "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"
jchlapinski04366412018-10-01 01:00:44 +020029 | "mod" | "move" | "mut" | "override" | "priv" | "pub"
David Tolnay719ad5a2018-09-02 18:01:15 -070030 | "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")]
David Tolnayf07b3342018-09-01 11:58:11 -070038impl Parse for Ident {
39 fn parse(input: ParseStream) -> Result<Self> {
40 input.step(|cursor| {
41 if let Some((ident, rest)) = cursor.ident() {
David Tolnay719ad5a2018-09-02 18:01:15 -070042 if accept_as_ident(&ident) {
43 return Ok((ident, rest));
David Tolnayf07b3342018-09-01 11:58:11 -070044 }
45 }
46 Err(cursor.error("expected identifier"))
47 })
48 }
49}
David Tolnayd3c93d52018-09-01 18:26:05 -070050
David Tolnay719ad5a2018-09-02 18:01:15 -070051#[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
David Tolnayd3c93d52018-09-01 18:26:05 -070066macro_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);
David Tolnayd72b6452018-09-01 18:27:54 -070081
82impl From<Token![_]> for Ident {
83 fn from(token: Token![_]) -> Ident {
84 Ident::new("_", token.spans[0])
85 }
86}