summaryrefslogtreecommitdiff
path: root/rust/syn/file.rs
diff options
context:
space:
mode:
authorMiguel Ojeda <ojeda@kernel.org>2025-11-24 18:18:27 +0300
committerMiguel Ojeda <ojeda@kernel.org>2025-11-24 19:15:44 +0300
commit808c999fc9e7c366fd47da564e69d579c1dc8279 (patch)
treed81985de64150acef12e038e98ef950e1b41b2d6 /rust/syn/file.rs
parent88de91cc1ce7b3069ccabc1a5fbe16d41c663093 (diff)
downloadlinux-808c999fc9e7c366fd47da564e69d579c1dc8279.tar.xz
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/file.rs')
-rw-r--r--rust/syn/file.rs125
1 files changed, 125 insertions, 0 deletions
diff --git a/rust/syn/file.rs b/rust/syn/file.rs
new file mode 100644
index 000000000000..8956d82eee93
--- /dev/null
+++ b/rust/syn/file.rs
@@ -0,0 +1,125 @@
+use crate::attr::Attribute;
+use crate::item::Item;
+
+ast_struct! {
+ /// A complete file of Rust source code.
+ ///
+ /// Typically `File` objects are created with [`parse_file`].
+ ///
+ /// [`parse_file`]: crate::parse_file
+ ///
+ /// # Example
+ ///
+ /// Parse a Rust source file into a `syn::File` and print out a debug
+ /// representation of the syntax tree.
+ ///
+ /// ```
+ /// use std::env;
+ /// use std::fs;
+ /// use std::process;
+ ///
+ /// fn main() {
+ /// # }
+ /// #
+ /// # fn fake_main() {
+ /// let mut args = env::args();
+ /// let _ = args.next(); // executable name
+ ///
+ /// let filename = match (args.next(), args.next()) {
+ /// (Some(filename), None) => filename,
+ /// _ => {
+ /// eprintln!("Usage: dump-syntax path/to/filename.rs");
+ /// process::exit(1);
+ /// }
+ /// };
+ ///
+ /// let src = fs::read_to_string(&filename).expect("unable to read file");
+ /// let syntax = syn::parse_file(&src).expect("unable to parse file");
+ ///
+ /// // Debug impl is available if Syn is built with "extra-traits" feature.
+ /// println!("{:#?}", syntax);
+ /// }
+ /// ```
+ ///
+ /// Running with its own source code as input, this program prints output
+ /// that begins with:
+ ///
+ /// ```text
+ /// File {
+ /// shebang: None,
+ /// attrs: [],
+ /// items: [
+ /// Use(
+ /// ItemUse {
+ /// attrs: [],
+ /// vis: Inherited,
+ /// use_token: Use,
+ /// leading_colon: None,
+ /// tree: Path(
+ /// UsePath {
+ /// ident: Ident(
+ /// std,
+ /// ),
+ /// colon2_token: Colon2,
+ /// tree: Name(
+ /// UseName {
+ /// ident: Ident(
+ /// env,
+ /// ),
+ /// },
+ /// ),
+ /// },
+ /// ),
+ /// semi_token: Semi,
+ /// },
+ /// ),
+ /// ...
+ /// ```
+ #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
+ pub struct File {
+ pub shebang: Option<String>,
+ pub attrs: Vec<Attribute>,
+ pub items: Vec<Item>,
+ }
+}
+
+#[cfg(feature = "parsing")]
+pub(crate) mod parsing {
+ use crate::attr::Attribute;
+ use crate::error::Result;
+ use crate::file::File;
+ use crate::parse::{Parse, ParseStream};
+
+ #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
+ impl Parse for File {
+ fn parse(input: ParseStream) -> Result<Self> {
+ Ok(File {
+ shebang: None,
+ attrs: input.call(Attribute::parse_inner)?,
+ items: {
+ let mut items = Vec::new();
+ while !input.is_empty() {
+ items.push(input.parse()?);
+ }
+ items
+ },
+ })
+ }
+ }
+}
+
+#[cfg(feature = "printing")]
+mod printing {
+ use crate::attr::FilterAttrs;
+ use crate::file::File;
+ use proc_macro2::TokenStream;
+ use quote::{ToTokens, TokenStreamExt};
+
+ #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
+ impl ToTokens for File {
+ fn to_tokens(&self, tokens: &mut TokenStream) {
+ tokens.append_all(self.attrs.inner());
+ tokens.append_all(&self.items);
+ }
+ }
+}