summaryrefslogtreecommitdiffstats
path: root/third_party/rust/proc-macro-error-attr/src/parse.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/proc-macro-error-attr/src/parse.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/proc-macro-error-attr/src/parse.rs')
-rw-r--r--third_party/rust/proc-macro-error-attr/src/parse.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/third_party/rust/proc-macro-error-attr/src/parse.rs b/third_party/rust/proc-macro-error-attr/src/parse.rs
new file mode 100644
index 0000000000..6f4663f80e
--- /dev/null
+++ b/third_party/rust/proc-macro-error-attr/src/parse.rs
@@ -0,0 +1,89 @@
+use crate::{Error, Result};
+use proc_macro2::{Delimiter, Ident, Span, TokenStream, TokenTree};
+use quote::ToTokens;
+use std::iter::Peekable;
+
+pub(crate) fn parse_input(
+ input: TokenStream,
+) -> Result<(Vec<Attribute>, Vec<TokenTree>, TokenTree)> {
+ let mut input = input.into_iter().peekable();
+ let mut attrs = Vec::new();
+
+ while let Some(attr) = parse_next_attr(&mut input)? {
+ attrs.push(attr);
+ }
+
+ let sig = parse_signature(&mut input);
+ let body = input.next().ok_or_else(|| {
+ Error::new(
+ Span::call_site(),
+ "`#[proc_macro_error]` can be applied only to functions".to_string(),
+ )
+ })?;
+
+ Ok((attrs, sig, body))
+}
+
+fn parse_next_attr(
+ input: &mut Peekable<impl Iterator<Item = TokenTree>>,
+) -> Result<Option<Attribute>> {
+ let shebang = match input.peek() {
+ Some(TokenTree::Punct(ref punct)) if punct.as_char() == '#' => input.next().unwrap(),
+ _ => return Ok(None),
+ };
+
+ let group = match input.peek() {
+ Some(TokenTree::Group(ref group)) if group.delimiter() == Delimiter::Bracket => {
+ let res = group.clone();
+ input.next();
+ res
+ }
+ other => {
+ let span = other.map_or(Span::call_site(), |tt| tt.span());
+ return Err(Error::new(span, "expected `[`".to_string()));
+ }
+ };
+
+ let path = match group.stream().into_iter().next() {
+ Some(TokenTree::Ident(ident)) => Some(ident),
+ _ => None,
+ };
+
+ Ok(Some(Attribute {
+ shebang,
+ group: TokenTree::Group(group),
+ path,
+ }))
+}
+
+fn parse_signature(input: &mut Peekable<impl Iterator<Item = TokenTree>>) -> Vec<TokenTree> {
+ let mut sig = Vec::new();
+ loop {
+ match input.peek() {
+ Some(TokenTree::Group(ref group)) if group.delimiter() == Delimiter::Brace => {
+ return sig;
+ }
+ None => return sig,
+ _ => sig.push(input.next().unwrap()),
+ }
+ }
+}
+
+pub(crate) struct Attribute {
+ pub(crate) shebang: TokenTree,
+ pub(crate) group: TokenTree,
+ pub(crate) path: Option<Ident>,
+}
+
+impl Attribute {
+ pub(crate) fn path_is_ident(&self, ident: &str) -> bool {
+ self.path.as_ref().map_or(false, |p| *p == ident)
+ }
+}
+
+impl ToTokens for Attribute {
+ fn to_tokens(&self, ts: &mut TokenStream) {
+ self.shebang.to_tokens(ts);
+ self.group.to_tokens(ts);
+ }
+}