David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 1 | #[cfg(feature = "parsing")] |
| 2 | use lookahead; |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 3 | #[cfg(feature = "parsing")] |
| 4 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 5 | |
| 6 | pub use proc_macro2::Ident; |
| 7 | |
| 8 | #[cfg(feature = "parsing")] |
| 9 | #[doc(hidden)] |
| 10 | #[allow(non_snake_case)] |
| 11 | pub fn Ident(marker: lookahead::TokenMarker) -> Ident { |
| 12 | match marker {} |
| 13 | } |
David Tolnay | f07b334 | 2018-09-01 11:58:11 -0700 | [diff] [blame] | 14 | |
| 15 | #[cfg(feature = "parsing")] |
| 16 | impl Parse for Ident { |
| 17 | fn parse(input: ParseStream) -> Result<Self> { |
| 18 | input.step(|cursor| { |
| 19 | if let Some((ident, rest)) = cursor.ident() { |
| 20 | match ident.to_string().as_str() { |
| 21 | "_" |
| 22 | // Based on https://doc.rust-lang.org/grammar.html#keywords |
| 23 | // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md |
| 24 | | "abstract" | "as" | "become" | "box" | "break" | "const" |
| 25 | | "continue" | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" |
| 26 | | "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" |
| 27 | | "mod" | "move" | "mut" | "override" | "priv" | "proc" | "pub" |
| 28 | | "ref" | "return" | "Self" | "self" | "static" | "struct" |
| 29 | | "super" | "trait" | "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" |
| 30 | | "virtual" | "where" | "while" | "yield" => {} |
| 31 | _ => return Ok((ident, rest)), |
| 32 | } |
| 33 | } |
| 34 | Err(cursor.error("expected identifier")) |
| 35 | }) |
| 36 | } |
| 37 | } |