From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/syn/src/ident.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 vendor/syn/src/ident.rs (limited to 'vendor/syn/src/ident.rs') diff --git a/vendor/syn/src/ident.rs b/vendor/syn/src/ident.rs new file mode 100644 index 000000000..8e3d8bda9 --- /dev/null +++ b/vendor/syn/src/ident.rs @@ -0,0 +1,101 @@ +#[cfg(feature = "parsing")] +use crate::buffer::Cursor; +#[cfg(feature = "parsing")] +use crate::lookahead; +#[cfg(feature = "parsing")] +use crate::parse::{Parse, ParseStream, Result}; +#[cfg(feature = "parsing")] +use crate::token::Token; + +pub use proc_macro2::Ident; + +#[cfg(feature = "parsing")] +#[doc(hidden)] +#[allow(non_snake_case)] +pub fn Ident(marker: lookahead::TokenMarker) -> Ident { + match marker {} +} + +#[cfg(feature = "parsing")] +fn accept_as_ident(ident: &Ident) -> bool { + match ident.to_string().as_str() { + "_" | + // Based on https://doc.rust-lang.org/grammar.html#keywords + // and https://github.com/rust-lang/rfcs/blob/master/text/2421-unreservations-2018.md + // and https://github.com/rust-lang/rfcs/blob/master/text/2420-unreserve-proc.md + "abstract" | "as" | "become" | "box" | "break" | "const" | "continue" | + "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | + "for" | "if" | "impl" | "in" | "let" | "loop" | "macro" | "match" | + "mod" | "move" | "mut" | "override" | "priv" | "pub" | "ref" | + "return" | "Self" | "self" | "static" | "struct" | "super" | "trait" | + "true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | + "where" | "while" | "yield" => false, + _ => true, + } +} + +#[cfg(feature = "parsing")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] +impl Parse for Ident { + fn parse(input: ParseStream) -> Result { + input.step(|cursor| { + if let Some((ident, rest)) = cursor.ident() { + if accept_as_ident(&ident) { + return Ok((ident, rest)); + } + } + Err(cursor.error("expected identifier")) + }) + } +} + +#[cfg(feature = "parsing")] +impl Token for Ident { + fn peek(cursor: Cursor) -> bool { + if let Some((ident, _rest)) = cursor.ident() { + accept_as_ident(&ident) + } else { + false + } + } + + fn display() -> &'static str { + "identifier" + } +} + +macro_rules! ident_from_token { + ($token:ident) => { + impl From for Ident { + fn from(token: Token![$token]) -> Ident { + Ident::new(stringify!($token), token.span) + } + } + }; +} + +ident_from_token!(self); +ident_from_token!(Self); +ident_from_token!(super); +ident_from_token!(crate); +ident_from_token!(extern); + +impl From for Ident { + fn from(token: Token![_]) -> Ident { + Ident::new("_", token.span) + } +} + +pub fn xid_ok(symbol: &str) -> bool { + let mut chars = symbol.chars(); + let first = chars.next().unwrap(); + if !(first == '_' || unicode_ident::is_xid_start(first)) { + return false; + } + for ch in chars { + if !unicode_ident::is_xid_continue(ch) { + return false; + } + } + true +} -- cgit v1.2.3