summaryrefslogtreecommitdiff
path: root/rust/syn/ident.rs
diff options
context:
space:
mode:
authorMiguel Ojeda <ojeda@kernel.org>2025-11-24 16:18:27 +0100
committerMiguel Ojeda <ojeda@kernel.org>2025-11-24 17:15:44 +0100
commit808c999fc9e7c366fd47da564e69d579c1dc8279 (patch)
treed81985de64150acef12e038e98ef950e1b41b2d6 /rust/syn/ident.rs
parent88de91cc1ce7b3069ccabc1a5fbe16d41c663093 (diff)
rust: syn: import crate
This is a subset of the Rust `syn` crate, version 2.0.106 (released 2025-08-16), licensed under "Apache-2.0 OR MIT", from: https://github.com/dtolnay/syn/raw/2.0.106/src The files are copied as-is, with no modifications whatsoever (not even adding the SPDX identifiers). For copyright details, please see: https://github.com/dtolnay/syn/blob/2.0.106/README.md#license https://github.com/dtolnay/syn/blob/2.0.106/LICENSE-APACHE https://github.com/dtolnay/syn/blob/2.0.106/LICENSE-MIT The next two patches modify these files as needed for use within the kernel. This patch split allows reviewers to double-check the import and to clearly see the differences introduced. The following script may be used to verify the contents: for path in $(cd rust/syn/ && find . -type f -name '*.rs'); do curl --silent --show-error --location \ https://github.com/dtolnay/syn/raw/2.0.106/src/$path \ | diff --unified rust/syn/$path - && echo $path: OK done Reviewed-by: Gary Guo <gary@garyguo.net> Tested-by: Gary Guo <gary@garyguo.net> Tested-by: Jesung Yang <y.j3ms.n@gmail.com> Link: https://patch.msgid.link/20251124151837.2184382-16-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/syn/ident.rs')
-rw-r--r--rust/syn/ident.rs108
1 files changed, 108 insertions, 0 deletions
diff --git a/rust/syn/ident.rs b/rust/syn/ident.rs
new file mode 100644
index 000000000000..8a8e8a50d9b0
--- /dev/null
+++ b/rust/syn/ident.rs
@@ -0,0 +1,108 @@
+#[cfg(feature = "parsing")]
+use crate::lookahead;
+
+pub use proc_macro2::Ident;
+
+#[cfg(feature = "parsing")]
+pub_if_not_doc! {
+ #[doc(hidden)]
+ #[allow(non_snake_case)]
+ pub fn Ident(marker: lookahead::TokenMarker) -> Ident {
+ match marker {}
+ }
+}
+
+macro_rules! ident_from_token {
+ ($token:ident) => {
+ impl From<Token![$token]> 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<Token![_]> for Ident {
+ fn from(token: Token![_]) -> Ident {
+ Ident::new("_", token.span)
+ }
+}
+
+pub(crate) 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
+}
+
+#[cfg(feature = "parsing")]
+mod parsing {
+ use crate::buffer::Cursor;
+ use crate::error::Result;
+ use crate::parse::{Parse, ParseStream};
+ use crate::token::Token;
+ use proc_macro2::Ident;
+
+ fn accept_as_ident(ident: &Ident) -> bool {
+ match ident.to_string().as_str() {
+ "_" |
+ // Based on https://doc.rust-lang.org/1.65.0/reference/keywords.html
+ "abstract" | "as" | "async" | "await" | "become" | "box" | "break" |
+ "const" | "continue" | "crate" | "do" | "dyn" | "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" | "try" | "type" |
+ "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" |
+ "while" | "yield" => false,
+ _ => true,
+ }
+ }
+
+ #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
+ impl Parse for Ident {
+ fn parse(input: ParseStream) -> Result<Self> {
+ input.step(|cursor| {
+ if let Some((ident, rest)) = cursor.ident() {
+ if accept_as_ident(&ident) {
+ Ok((ident, rest))
+ } else {
+ Err(cursor.error(format_args!(
+ "expected identifier, found keyword `{}`",
+ ident,
+ )))
+ }
+ } else {
+ Err(cursor.error("expected identifier"))
+ }
+ })
+ }
+ }
+
+ 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"
+ }
+ }
+}