1fad3a1d3Sopenharmony_ci#[cfg(feature = "parsing")] 2fad3a1d3Sopenharmony_ciuse crate::lookahead; 3fad3a1d3Sopenharmony_ci 4fad3a1d3Sopenharmony_cipub use proc_macro2::Ident; 5fad3a1d3Sopenharmony_ci 6fad3a1d3Sopenharmony_ci#[cfg(feature = "parsing")] 7fad3a1d3Sopenharmony_cipub_if_not_doc! { 8fad3a1d3Sopenharmony_ci #[doc(hidden)] 9fad3a1d3Sopenharmony_ci #[allow(non_snake_case)] 10fad3a1d3Sopenharmony_ci pub fn Ident(marker: lookahead::TokenMarker) -> Ident { 11fad3a1d3Sopenharmony_ci match marker {} 12fad3a1d3Sopenharmony_ci } 13fad3a1d3Sopenharmony_ci} 14fad3a1d3Sopenharmony_ci 15fad3a1d3Sopenharmony_cimacro_rules! ident_from_token { 16fad3a1d3Sopenharmony_ci ($token:ident) => { 17fad3a1d3Sopenharmony_ci impl From<Token![$token]> for Ident { 18fad3a1d3Sopenharmony_ci fn from(token: Token![$token]) -> Ident { 19fad3a1d3Sopenharmony_ci Ident::new(stringify!($token), token.span) 20fad3a1d3Sopenharmony_ci } 21fad3a1d3Sopenharmony_ci } 22fad3a1d3Sopenharmony_ci }; 23fad3a1d3Sopenharmony_ci} 24fad3a1d3Sopenharmony_ci 25fad3a1d3Sopenharmony_ciident_from_token!(self); 26fad3a1d3Sopenharmony_ciident_from_token!(Self); 27fad3a1d3Sopenharmony_ciident_from_token!(super); 28fad3a1d3Sopenharmony_ciident_from_token!(crate); 29fad3a1d3Sopenharmony_ciident_from_token!(extern); 30fad3a1d3Sopenharmony_ci 31fad3a1d3Sopenharmony_ciimpl From<Token![_]> for Ident { 32fad3a1d3Sopenharmony_ci fn from(token: Token![_]) -> Ident { 33fad3a1d3Sopenharmony_ci Ident::new("_", token.span) 34fad3a1d3Sopenharmony_ci } 35fad3a1d3Sopenharmony_ci} 36fad3a1d3Sopenharmony_ci 37fad3a1d3Sopenharmony_cipub(crate) fn xid_ok(symbol: &str) -> bool { 38fad3a1d3Sopenharmony_ci let mut chars = symbol.chars(); 39fad3a1d3Sopenharmony_ci let first = chars.next().unwrap(); 40fad3a1d3Sopenharmony_ci if !(first == '_' || unicode_ident::is_xid_start(first)) { 41fad3a1d3Sopenharmony_ci return false; 42fad3a1d3Sopenharmony_ci } 43fad3a1d3Sopenharmony_ci for ch in chars { 44fad3a1d3Sopenharmony_ci if !unicode_ident::is_xid_continue(ch) { 45fad3a1d3Sopenharmony_ci return false; 46fad3a1d3Sopenharmony_ci } 47fad3a1d3Sopenharmony_ci } 48fad3a1d3Sopenharmony_ci true 49fad3a1d3Sopenharmony_ci} 50fad3a1d3Sopenharmony_ci 51fad3a1d3Sopenharmony_ci#[cfg(feature = "parsing")] 52fad3a1d3Sopenharmony_cimod parsing { 53fad3a1d3Sopenharmony_ci use crate::buffer::Cursor; 54fad3a1d3Sopenharmony_ci use crate::parse::{Parse, ParseStream, Result}; 55fad3a1d3Sopenharmony_ci use crate::token::Token; 56fad3a1d3Sopenharmony_ci use proc_macro2::Ident; 57fad3a1d3Sopenharmony_ci 58fad3a1d3Sopenharmony_ci fn accept_as_ident(ident: &Ident) -> bool { 59fad3a1d3Sopenharmony_ci match ident.to_string().as_str() { 60fad3a1d3Sopenharmony_ci "_" | 61fad3a1d3Sopenharmony_ci // Based on https://doc.rust-lang.org/1.65.0/reference/keywords.html 62fad3a1d3Sopenharmony_ci "abstract" | "as" | "async" | "await" | "become" | "box" | "break" | 63fad3a1d3Sopenharmony_ci "const" | "continue" | "crate" | "do" | "dyn" | "else" | "enum" | 64fad3a1d3Sopenharmony_ci "extern" | "false" | "final" | "fn" | "for" | "if" | "impl" | "in" | 65fad3a1d3Sopenharmony_ci "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" | 66fad3a1d3Sopenharmony_ci "override" | "priv" | "pub" | "ref" | "return" | "Self" | "self" | 67fad3a1d3Sopenharmony_ci "static" | "struct" | "super" | "trait" | "true" | "try" | "type" | 68fad3a1d3Sopenharmony_ci "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | 69fad3a1d3Sopenharmony_ci "while" | "yield" => false, 70fad3a1d3Sopenharmony_ci _ => true, 71fad3a1d3Sopenharmony_ci } 72fad3a1d3Sopenharmony_ci } 73fad3a1d3Sopenharmony_ci 74fad3a1d3Sopenharmony_ci #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] 75fad3a1d3Sopenharmony_ci impl Parse for Ident { 76fad3a1d3Sopenharmony_ci fn parse(input: ParseStream) -> Result<Self> { 77fad3a1d3Sopenharmony_ci input.step(|cursor| { 78fad3a1d3Sopenharmony_ci if let Some((ident, rest)) = cursor.ident() { 79fad3a1d3Sopenharmony_ci if accept_as_ident(&ident) { 80fad3a1d3Sopenharmony_ci Ok((ident, rest)) 81fad3a1d3Sopenharmony_ci } else { 82fad3a1d3Sopenharmony_ci Err(cursor.error(format_args!( 83fad3a1d3Sopenharmony_ci "expected identifier, found keyword `{}`", 84fad3a1d3Sopenharmony_ci ident, 85fad3a1d3Sopenharmony_ci ))) 86fad3a1d3Sopenharmony_ci } 87fad3a1d3Sopenharmony_ci } else { 88fad3a1d3Sopenharmony_ci Err(cursor.error("expected identifier")) 89fad3a1d3Sopenharmony_ci } 90fad3a1d3Sopenharmony_ci }) 91fad3a1d3Sopenharmony_ci } 92fad3a1d3Sopenharmony_ci } 93fad3a1d3Sopenharmony_ci 94fad3a1d3Sopenharmony_ci impl Token for Ident { 95fad3a1d3Sopenharmony_ci fn peek(cursor: Cursor) -> bool { 96fad3a1d3Sopenharmony_ci if let Some((ident, _rest)) = cursor.ident() { 97fad3a1d3Sopenharmony_ci accept_as_ident(&ident) 98fad3a1d3Sopenharmony_ci } else { 99fad3a1d3Sopenharmony_ci false 100fad3a1d3Sopenharmony_ci } 101fad3a1d3Sopenharmony_ci } 102fad3a1d3Sopenharmony_ci 103fad3a1d3Sopenharmony_ci fn display() -> &'static str { 104fad3a1d3Sopenharmony_ci "identifier" 105fad3a1d3Sopenharmony_ci } 106fad3a1d3Sopenharmony_ci } 107fad3a1d3Sopenharmony_ci} 108