summaryrefslogtreecommitdiffstats
path: root/vendor/clap_derive/src/utils
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/clap_derive/src/utils
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap_derive/src/utils')
-rw-r--r--vendor/clap_derive/src/utils/doc_comments.rs107
-rw-r--r--vendor/clap_derive/src/utils/mod.rs9
-rw-r--r--vendor/clap_derive/src/utils/spanned.rs92
-rw-r--r--vendor/clap_derive/src/utils/ty.rs119
4 files changed, 327 insertions, 0 deletions
diff --git a/vendor/clap_derive/src/utils/doc_comments.rs b/vendor/clap_derive/src/utils/doc_comments.rs
new file mode 100644
index 000000000..f0a5034d7
--- /dev/null
+++ b/vendor/clap_derive/src/utils/doc_comments.rs
@@ -0,0 +1,107 @@
+//! The preprocessing we apply to doc comments.
+//!
+//! #[derive(Parser)] works in terms of "paragraphs". Paragraph is a sequence of
+//! non-empty adjacent lines, delimited by sequences of blank (whitespace only) lines.
+
+use crate::attrs::Method;
+
+use quote::{format_ident, quote};
+use std::iter;
+
+pub fn process_doc_comment(lines: Vec<String>, name: &str, preprocess: bool) -> Vec<Method> {
+ // multiline comments (`/** ... */`) may have LFs (`\n`) in them,
+ // we need to split so we could handle the lines correctly
+ //
+ // we also need to remove leading and trailing blank lines
+ let mut lines: Vec<&str> = lines
+ .iter()
+ .skip_while(|s| is_blank(s))
+ .flat_map(|s| s.split('\n'))
+ .collect();
+
+ while let Some(true) = lines.last().map(|s| is_blank(s)) {
+ lines.pop();
+ }
+
+ // remove one leading space no matter what
+ for line in lines.iter_mut() {
+ if line.starts_with(' ') {
+ *line = &line[1..];
+ }
+ }
+
+ if lines.is_empty() {
+ return vec![];
+ }
+
+ let short_name = format_ident!("{}", name);
+ let long_name = format_ident!("long_{}", name);
+
+ if let Some(first_blank) = lines.iter().position(|s| is_blank(s)) {
+ let (short, long) = if preprocess {
+ let paragraphs = split_paragraphs(&lines);
+ let short = paragraphs[0].clone();
+ let long = paragraphs.join("\n\n");
+ (remove_period(short), long)
+ } else {
+ let short = lines[..first_blank].join("\n");
+ let long = lines.join("\n");
+ (short, long)
+ };
+
+ vec![
+ Method::new(short_name, quote!(#short)),
+ Method::new(long_name, quote!(#long)),
+ ]
+ } else {
+ let short = if preprocess {
+ let s = merge_lines(&lines);
+ remove_period(s)
+ } else {
+ lines.join("\n")
+ };
+
+ vec![
+ Method::new(short_name, quote!(#short)),
+ Method::new(long_name, quote!(None)),
+ ]
+ }
+}
+
+fn split_paragraphs(lines: &[&str]) -> Vec<String> {
+ let mut last_line = 0;
+ iter::from_fn(|| {
+ let slice = &lines[last_line..];
+ let start = slice.iter().position(|s| !is_blank(s)).unwrap_or(0);
+
+ let slice = &slice[start..];
+ let len = slice
+ .iter()
+ .position(|s| is_blank(s))
+ .unwrap_or(slice.len());
+
+ last_line += start + len;
+
+ if len != 0 {
+ Some(merge_lines(&slice[..len]))
+ } else {
+ None
+ }
+ })
+ .collect()
+}
+
+fn remove_period(mut s: String) -> String {
+ if s.ends_with('.') && !s.ends_with("..") {
+ s.pop();
+ }
+ s
+}
+
+fn is_blank(s: &str) -> bool {
+ s.trim().is_empty()
+}
+
+fn merge_lines(lines: &[&str]) -> String {
+ lines.iter().map(|s| s.trim()).collect::<Vec<_>>().join(" ")
+}
diff --git a/vendor/clap_derive/src/utils/mod.rs b/vendor/clap_derive/src/utils/mod.rs
new file mode 100644
index 000000000..77a467c75
--- /dev/null
+++ b/vendor/clap_derive/src/utils/mod.rs
@@ -0,0 +1,9 @@
+mod doc_comments;
+mod spanned;
+mod ty;
+
+pub use self::{
+ doc_comments::process_doc_comment,
+ spanned::Sp,
+ ty::{inner_type, is_simple_ty, sub_type, subty_if_name, Ty},
+};
diff --git a/vendor/clap_derive/src/utils/spanned.rs b/vendor/clap_derive/src/utils/spanned.rs
new file mode 100644
index 000000000..11415f6f0
--- /dev/null
+++ b/vendor/clap_derive/src/utils/spanned.rs
@@ -0,0 +1,92 @@
+use proc_macro2::{Ident, Span, TokenStream};
+use quote::ToTokens;
+use syn::LitStr;
+
+use std::ops::{Deref, DerefMut};
+
+/// An entity with a span attached.
+#[derive(Debug, Clone)]
+pub struct Sp<T> {
+ val: T,
+ span: Span,
+}
+
+impl<T> Sp<T> {
+ pub fn new(val: T, span: Span) -> Self {
+ Sp { val, span }
+ }
+
+ pub fn call_site(val: T) -> Self {
+ Sp {
+ val,
+ span: Span::call_site(),
+ }
+ }
+
+ pub fn span(&self) -> Span {
+ self.span
+ }
+}
+
+impl<T> Deref for Sp<T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ &self.val
+ }
+}
+
+impl<T> DerefMut for Sp<T> {
+ fn deref_mut(&mut self) -> &mut T {
+ &mut self.val
+ }
+}
+
+impl From<Ident> for Sp<String> {
+ fn from(ident: Ident) -> Self {
+ Sp {
+ val: ident.to_string(),
+ span: ident.span(),
+ }
+ }
+}
+
+impl From<LitStr> for Sp<String> {
+ fn from(lit: LitStr) -> Self {
+ Sp {
+ val: lit.value(),
+ span: lit.span(),
+ }
+ }
+}
+
+impl<'a> From<Sp<&'a str>> for Sp<String> {
+ fn from(sp: Sp<&'a str>) -> Self {
+ Sp::new(sp.val.into(), sp.span)
+ }
+}
+
+impl<U, T: PartialEq<U>> PartialEq<U> for Sp<T> {
+ fn eq(&self, other: &U) -> bool {
+ self.val == *other
+ }
+}
+
+impl<T: AsRef<str>> AsRef<str> for Sp<T> {
+ fn as_ref(&self) -> &str {
+ self.val.as_ref()
+ }
+}
+
+impl<T: ToTokens> ToTokens for Sp<T> {
+ fn to_tokens(&self, stream: &mut TokenStream) {
+ // this is the simplest way out of correct ones to change span on
+ // arbitrary token tree I could come up with
+ let tt = self.val.to_token_stream().into_iter().map(|mut tt| {
+ tt.set_span(self.span);
+ tt
+ });
+
+ stream.extend(tt);
+ }
+}
diff --git a/vendor/clap_derive/src/utils/ty.rs b/vendor/clap_derive/src/utils/ty.rs
new file mode 100644
index 000000000..0bcb59f27
--- /dev/null
+++ b/vendor/clap_derive/src/utils/ty.rs
@@ -0,0 +1,119 @@
+//! Special types handling
+
+use super::spanned::Sp;
+
+use syn::{
+ spanned::Spanned, GenericArgument, Path, PathArguments, PathArguments::AngleBracketed,
+ PathSegment, Type, TypePath,
+};
+
+#[derive(Copy, Clone, PartialEq, Debug)]
+pub enum Ty {
+ Vec,
+ Option,
+ OptionOption,
+ OptionVec,
+ Other,
+}
+
+impl Ty {
+ pub fn from_syn_ty(ty: &syn::Type) -> Sp<Self> {
+ use self::Ty::*;
+ let t = |kind| Sp::new(kind, ty.span());
+
+ if is_generic_ty(ty, "Vec") {
+ t(Vec)
+ } else if let Some(subty) = subty_if_name(ty, "Option") {
+ if is_generic_ty(subty, "Option") {
+ t(OptionOption)
+ } else if is_generic_ty(subty, "Vec") {
+ t(OptionVec)
+ } else {
+ t(Option)
+ }
+ } else {
+ t(Other)
+ }
+ }
+}
+
+pub fn inner_type(field_ty: &syn::Type) -> &syn::Type {
+ let ty = Ty::from_syn_ty(field_ty);
+ match *ty {
+ Ty::Vec | Ty::Option => sub_type(field_ty).unwrap_or(field_ty),
+ Ty::OptionOption | Ty::OptionVec => {
+ sub_type(field_ty).and_then(sub_type).unwrap_or(field_ty)
+ }
+ _ => field_ty,
+ }
+}
+
+pub fn sub_type(ty: &syn::Type) -> Option<&syn::Type> {
+ subty_if(ty, |_| true)
+}
+
+fn only_last_segment(mut ty: &syn::Type) -> Option<&PathSegment> {
+ while let syn::Type::Group(syn::TypeGroup { elem, .. }) = ty {
+ ty = elem;
+ }
+ match ty {
+ Type::Path(TypePath {
+ qself: None,
+ path:
+ Path {
+ leading_colon: None,
+ segments,
+ },
+ }) => only_one(segments.iter()),
+
+ _ => None,
+ }
+}
+
+fn subty_if<F>(ty: &syn::Type, f: F) -> Option<&syn::Type>
+where
+ F: FnOnce(&PathSegment) -> bool,
+{
+ only_last_segment(ty)
+ .filter(|segment| f(segment))
+ .and_then(|segment| {
+ if let AngleBracketed(args) = &segment.arguments {
+ only_one(args.args.iter()).and_then(|genneric| {
+ if let GenericArgument::Type(ty) = genneric {
+ Some(ty)
+ } else {
+ None
+ }
+ })
+ } else {
+ None
+ }
+ })
+}
+
+pub fn subty_if_name<'a>(ty: &'a syn::Type, name: &str) -> Option<&'a syn::Type> {
+ subty_if(ty, |seg| seg.ident == name)
+}
+
+pub fn is_simple_ty(ty: &syn::Type, name: &str) -> bool {
+ only_last_segment(ty)
+ .map(|segment| {
+ if let PathArguments::None = segment.arguments {
+ segment.ident == name
+ } else {
+ false
+ }
+ })
+ .unwrap_or(false)
+}
+
+fn is_generic_ty(ty: &syn::Type, name: &str) -> bool {
+ subty_if_name(ty, name).is_some()
+}
+
+fn only_one<I, T>(mut iter: I) -> Option<T>
+where
+ I: Iterator<Item = T>,
+{
+ iter.next().filter(|_| iter.next().is_none())
+}