David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 1 | #[cfg(feature = "parsing")] |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 2 | use buffer::Cursor; |
| 3 | #[cfg(feature = "parsing")] |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 4 | use lookahead; |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 5 | #[cfg(feature = "parsing")] |
| 6 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 7 | #[cfg(feature = "parsing")] |
| 8 | use token::Token; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 9 | |
| 10 | pub use proc_macro2::Ident; |
| 11 | |
| 12 | #[cfg(feature = "parsing")] |
| 13 | #[doc(hidden)] |
| 14 | #[allow(non_snake_case)] |
| 15 | pub fn Ident(marker: lookahead::TokenMarker) -> Ident { |
| 16 | match marker {} |
| 17 | } |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 18 | |
| 19 | #[cfg(feature = "parsing")] |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 20 | 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 |
jchlapinski | 0436641 | 2018-10-01 01:00:44 +0200 | [diff] [blame] | 25 | // and https://github.com/rust-lang/rfcs/blob/master/text/2420-unreserve-proc.md |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 26 | | "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" |
jchlapinski | 0436641 | 2018-10-01 01:00:44 +0200 | [diff] [blame] | 29 | | "mod" | "move" | "mut" | "override" | "priv" | "pub" |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 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")] |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 38 | impl Parse for Ident { |
| 39 | fn parse(input: ParseStream) -> Result<Self> { |
| 40 | input.step(|cursor| { |
| 41 | if let Some((ident, rest)) = cursor.ident() { |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 42 | if accept_as_ident(&ident) { |
| 43 | return Ok((ident, rest)); |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | Err(cursor.error("expected identifier")) |
| 47 | }) |
| 48 | } |
| 49 | } |
David Tolnay | d3c93d5 | 2018-09-01 18:26:05 -0700 | [diff] [blame] | 50 | |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 51 | #[cfg(feature = "parsing")] |
| 52 | impl 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 Tolnay | d3c93d5 | 2018-09-01 18:26:05 -0700 | [diff] [blame] | 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); |
David Tolnay | d72b645 | 2018-09-01 18:27:54 -0700 | [diff] [blame] | 81 | |
| 82 | impl From<Token![_]> for Ident { |
| 83 | fn from(token: Token![_]) -> Ident { |
David Tolnay | cefc788 | 2019-03-09 19:47:16 -0800 | [diff] [blame^] | 84 | Ident::new("_", token.span) |
David Tolnay | d72b645 | 2018-09-01 18:27:54 -0700 | [diff] [blame] | 85 | } |
| 86 | } |