1#![cfg_attr(docsrs, feature(doc_cfg))]
7#![deny(
8 clippy::all,
9 clippy::pedantic,
10 clippy::nursery,
11 clippy::cargo,
12 rustdoc::all,
13 rustdoc::redundant_explicit_links,
14 invalid_doc_attributes,
15 unused_doc_comments,
16 missing_docs
17 )]
19#![cfg_attr(not(feature = "full"), allow(rustdoc::broken_intra_doc_links))]
21#![allow(
22 clippy::option_if_let_else,
23 clippy::same_functions_in_if_condition,
24 clippy::too_many_lines
25)]
26
27#[cfg(not(feature = "fallback"))]
30mod sealed {
31 pub extern crate proc_macro;
32}
33#[cfg(not(feature = "fallback"))]
34pub use sealed::proc_macro;
35
36#[cfg(feature = "fallback")]
39pub use parsyng_fallback as proc_macro;
40
41#[cfg(feature = "parsing")]
42pub mod ast;
43#[cfg(feature = "parsing")]
44pub mod combinator;
45pub mod error;
46#[cfg(feature = "parsing")]
47pub mod parse;
48pub mod proc_macro_ext;
50#[doc(hidden)]
52pub mod quote;
53
54#[cfg(feature = "parsing")]
55pub use parse::Parse;
56
57#[macro_export]
69macro_rules! format_ident {
70 ($($args:tt)*) => {
71 $crate::proc_macro::Ident::new(&format!($($args)*), $crate::proc_macro::Span::call_site())
72 };
73}
74
75pub trait ToTokens {
85 fn to_tokens(&self, tokens: &mut crate::proc_macro::TokenStream);
87
88 fn to_token_stream(&self) -> crate::proc_macro::TokenStream {
90 let mut token_stream = crate::proc_macro::TokenStream::new();
91 self.to_tokens(&mut token_stream);
92 token_stream
93 }
94}
95
96#[derive(Clone, Copy, Debug)]
112pub struct Index {
113 pub index: usize,
115 pub span: crate::proc_macro::Span,
117}
118
119impl From<usize> for Index {
120 fn from(index: usize) -> Self {
121 Self {
122 index,
123 span: crate::proc_macro::Span::call_site(),
124 }
125 }
126}
127
128impl ToTokens for Index {
129 fn to_tokens(&self, tokens: &mut crate::proc_macro::TokenStream) {
130 let mut literal = crate::proc_macro::Literal::usize_unsuffixed(self.index);
131 literal.set_span(self.span);
132 tokens.extend(Some(crate::proc_macro::TokenTree::Literal(literal)));
133 }
134}
135
136#[doc(hidden)]
137pub fn debug_stream(macro_name: &str, call_location: &str, input: &crate::proc_macro::TokenStream) {
139 let output;
140 #[cfg(feature = "debug-pretty")]
141 {
142 use std::{
143 io::Write,
144 path::PathBuf,
145 process::{Command, Stdio},
146 };
147
148 fn catch_rustfmt_errors(input: &crate::proc_macro::TokenStream) -> Option<String> {
149 let prefix = "fn __dummy() {\n";
151 let suffix = "\n}";
152 let input = format!("{prefix}{input}{suffix}");
153
154 let cargo = PathBuf::from(std::option_env!("CARGO")?);
155 let mut rustfmt = cargo.parent()?.to_owned();
156 rustfmt.push("rustfmt");
157
158 let mut command = Command::new(rustfmt);
159 let command = command.stdin(Stdio::piped()).stdout(Stdio::piped());
160 let mut exec = command.spawn().ok()?;
161 exec.stdin.take()?.write_all(input.as_bytes()).unwrap();
162 let output = exec.wait_with_output().ok().and_then(|output| {
163 if output.status.success() {
164 String::from_utf8(output.stdout).ok()
165 } else {
166 None
167 }
168 })?;
169
170 let output = output
171 .trim()
172 .strip_prefix(prefix)?
173 .strip_suffix(suffix)?
174 .trim();
175 let output = output.replace("\n ", "\n");
176
177 Some(output)
178 }
179
180 output = catch_rustfmt_errors(input).unwrap_or_else(|| input.to_string());
181 }
182 #[cfg(not(feature = "debug-pretty"))]
183 {
184 output = input;
185 }
186 eprintln!("[DEBUG] proc-macro `{macro_name}` called at {call_location}\n{output}");
187}