1extern crate proc_macro;
52
53pub mod syn_types;
54
55pub use parse_more_macros::{filler, parse_more};
57
58use syn::{
59 braced, bracketed, parenthesized,
60 parse::{Nothing, Parse},
61 punctuated::Punctuated,
62 LitInt, LitStr,
63};
64
65pub trait ParseMore: Sized {
68 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self>;
69}
70
71pub struct ParseMoreWrapper<T>(pub T);
95
96impl<T: ParseMore> Parse for ParseMoreWrapper<T> {
98 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
99 Ok(ParseMoreWrapper(ParseMore::parse(input)?))
100 }
101}
102
103#[macro_export]
130macro_rules! parse_more_auto_impl {
131 ($($ty:ty),*) => {
132 $(impl $crate::ParseMore for $ty {
133 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
134 <Self as syn::parse::Parse>::parse(input)
135 }
136 })*
137 }
138}
139
140macro_rules! tuple_impls {
142 ($first:ident) => {
143 impl<$first:ParseMore> ParseMore for ($first, ) {
144 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
145 let content;
146 parenthesized!(content in input);
147 let first = content.parse::<ParseMoreWrapper<$first>>()?.0;
148 content.parse::<syn::Token![,]>()?;
149 Ok((first,))
150 }
151 }
152 };
153 ($first:ident $($generics:ident)*) => {
154 impl<$first:ParseMore, $($generics: ParseMore),*> ParseMore for ($first, $($generics,)*) {
155 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
156 let content;
157 parenthesized!(content in input);
158 let first = content.parse::<ParseMoreWrapper<$first>>()?.0;
159 let res = Ok((first,
160 $({
161 content.parse::<syn::Token![,]>()?;
162 content.parse::<ParseMoreWrapper<$generics>>()?.0
163 },)*
164 ));
165
166 if content.peek(syn::Token![,]) {
168 content.parse::<syn::Token![,]>().unwrap();
169 }
170 res
171 }
172 }
173 };
174}
175
176macro_rules! for_each_tuple_ {
178 ($mac:ident =>) => {};
179 ($mac:ident => $first:ident, $($generics:ident,)*) => {
180 $mac! {
181 $first $($generics)*
182 }
183 for_each_tuple_ ! {
184 $mac => $($generics,)*
185 }
186 };
187}
188
189macro_rules! for_each_tuple {
191 ($mac:ident) => {
192 for_each_tuple_! {
193 $mac => A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T,
194 }
195 };
196}
197
198for_each_tuple! {
200 tuple_impls
201}
202
203impl<T: ParseMore, const N: usize> ParseMore for [T; N] {
223 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
224 let content;
225 bracketed!(content in input);
226
227 let mut res = vec![];
228 for i in 0..N {
229 if i != 0 {
230 content.parse::<syn::Token![,]>()?;
231 }
232 res.push(content.parse::<ParseMoreWrapper<T>>()?.0)
233 }
234
235 if content.peek(syn::Token![,]) {
237 content.parse::<syn::Token![,]>().unwrap();
238 }
239 Ok(unsafe { res.try_into().unwrap_unchecked() })
240 }
241}
242
243impl<T: ParseMore> ParseMore for Vec<T> {
262 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
263 let mut res = vec![];
264 while let Ok(parsed) = input.parse::<ParseMoreWrapper<T>>() {
265 res.push(parsed.0);
266 }
267 Ok(res)
268 }
269}
270
271impl<T: ParseMore, P: ParseMore> ParseMore for Punctuated<T, P> {
290 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
291 let mut res = Punctuated::new();
292 let wrapped =
293 Punctuated::<ParseMoreWrapper<T>, ParseMoreWrapper<P>>::parse_terminated(input)?;
294
295 wrapped.into_pairs().for_each(|pair| {
297 let pair = pair.into_tuple();
298 res.push_value(pair.0 .0);
299 if let Some(punct) = pair.1 {
300 res.push_punct(punct.0);
301 }
302 });
303
304 Ok(res)
305 }
306}
307
308#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
310pub struct Concat<A, B, C = Nothing, D = Nothing, E = Nothing, F = Nothing, G = Nothing> {
311 first: A,
312 second: B,
313 third: C,
314 fourth: D,
315 fifth: E,
316 sixth: F,
317 seventh: G,
318}
319
320impl<A, B, C, D, E, F, G> From<Concat<A, B, C, D, E, F, G>> for (A, B, C, D, E, F, G) {
321 fn from(value: Concat<A, B, C, D, E, F, G>) -> Self {
322 (
323 value.first,
324 value.second,
325 value.third,
326 value.fourth,
327 value.fifth,
328 value.sixth,
329 value.seventh,
330 )
331 }
332}
333impl<A, B, C, D, E, F> From<Concat<A, B, C, D, E, F>> for (A, B, C, D, E, F) {
334 fn from(value: Concat<A, B, C, D, E, F>) -> Self {
335 (
336 value.first,
337 value.second,
338 value.third,
339 value.fourth,
340 value.fifth,
341 value.sixth,
342 )
343 }
344}
345impl<A, B, C, D, E> From<Concat<A, B, C, D, E>> for (A, B, C, D, E) {
346 fn from(value: Concat<A, B, C, D, E>) -> Self {
347 (
348 value.first,
349 value.second,
350 value.third,
351 value.fourth,
352 value.fifth,
353 )
354 }
355}
356impl<A, B, C, D> From<Concat<A, B, C, D>> for (A, B, C, D) {
357 fn from(value: Concat<A, B, C, D>) -> Self {
358 (value.first, value.second, value.third, value.fourth)
359 }
360}
361impl<A, B, C> From<Concat<A, B, C>> for (A, B, C) {
362 fn from(value: Concat<A, B, C>) -> Self {
363 (value.first, value.second, value.third)
364 }
365}
366impl<A, B> From<Concat<A, B>> for (A, B) {
367 fn from(value: Concat<A, B>) -> Self {
368 (value.first, value.second)
369 }
370}
371impl<A, B> Concat<A, B> {
372 pub fn into_tuple2(self) -> (A, B) {
374 self.into()
375 }
376}
377impl<A, B, C> Concat<A, B, C> {
378 pub fn into_tuple3(self) -> (A, B, C) {
380 self.into()
381 }
382}
383impl<A, B, C, D> Concat<A, B, C, D> {
384 pub fn into_tuple4(self) -> (A, B, C, D) {
386 self.into()
387 }
388}
389impl<A, B, C, D, E> Concat<A, B, C, D, E> {
390 pub fn into_tuple5(self) -> (A, B, C, D, E) {
392 self.into()
393 }
394}
395impl<A, B, C, D, E, F> Concat<A, B, C, D, E, F> {
396 pub fn into_tuple6(self) -> (A, B, C, D, E, F) {
398 self.into()
399 }
400}
401impl<A, B, C, D, E, F, G> Concat<A, B, C, D, E, F, G> {
402 pub fn into_tuple7(self) -> (A, B, C, D, E, F, G) {
404 self.into()
405 }
406}
407
408impl<
410 A: ParseMore,
411 B: ParseMore,
412 C: ParseMore,
413 D: ParseMore,
414 E: ParseMore,
415 F: ParseMore,
416 G: ParseMore,
417 > ParseMore for Concat<A, B, C, D, E, F, G>
418{
419 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
420 Ok(Self {
421 first: input.parse::<ParseMoreWrapper<_>>()?.0,
422 second: input.parse::<ParseMoreWrapper<_>>()?.0,
423 third: input.parse::<ParseMoreWrapper<_>>()?.0,
424 fourth: input.parse::<ParseMoreWrapper<_>>()?.0,
425 fifth: input.parse::<ParseMoreWrapper<_>>()?.0,
426 sixth: input.parse::<ParseMoreWrapper<_>>()?.0,
427 seventh: input.parse::<ParseMoreWrapper<_>>()?.0,
428 })
429 }
430}
431
432#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
434pub struct Braced<T>(pub T);
435
436impl<T: ParseMore> ParseMore for Braced<T> {
438 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
439 let content;
440 braced!(content in input);
441 Ok(Self(content.parse::<ParseMoreWrapper<T>>()?.0))
442 }
443}
444
445impl<T> Braced<T> {
446 pub fn value(self) -> T {
448 self.0
449 }
450}
451
452#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
454pub struct Parenthesized<T>(pub T);
455
456impl<T: ParseMore> ParseMore for Parenthesized<T> {
458 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
459 let content;
460 parenthesized!(content in input);
461 Ok(Self(content.parse::<ParseMoreWrapper<T>>()?.0))
462 }
463}
464
465impl<T> Parenthesized<T> {
466 pub fn value(self) -> T {
468 self.0
469 }
470}
471
472#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
474pub struct Bracketed<T>(pub T);
475
476impl<T: ParseMore> ParseMore for Bracketed<T> {
478 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
479 let content;
480 bracketed!(content in input);
481 Ok(Self(content.parse::<ParseMoreWrapper<T>>()?.0))
482 }
483}
484
485impl<T> Bracketed<T> {
486 pub fn value(self) -> T {
488 self.0
489 }
490}
491
492impl<T: ParseMore> ParseMore for Option<T> {
494 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
495 let input_forked = input.fork();
497 if let Ok(parsed) = input_forked.parse::<ParseMoreWrapper<T>>() {
498 assert!(input.parse::<ParseMoreWrapper<T>>().is_ok());
501 Ok(Some(parsed.0))
502 } else {
503 Ok(None)
504 }
505 }
506}
507
508#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
510pub struct Invalid;
511
512impl ParseMore for Invalid {
514 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
515 Err(syn::Error::new(input.span(), "Invalid can never be parsed"))
516 }
517}
518
519#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
521pub enum Either<A, B, C = Invalid, D = Invalid, E = Invalid, F = Invalid, G = Invalid> {
522 First(A),
523 Second(B),
524 Third(C),
525 Fourth(D),
526 Fifth(E),
527 Sixth(F),
528 Seventh(G),
529}
530
531impl<A, B, C, D, E, F, G> Either<A, B, C, D, E, F, G> {
532 pub fn is_first(&self) -> bool {
534 matches!(self, Either::First(_))
535 }
536 pub fn is_second(&self) -> bool {
538 matches!(self, Either::Second(_))
539 }
540 pub fn is_third(&self) -> bool {
542 matches!(self, Either::Third(_))
543 }
544 pub fn is_fourth(&self) -> bool {
546 matches!(self, Either::Fourth(_))
547 }
548 pub fn is_fifth(&self) -> bool {
550 matches!(self, Either::Fifth(_))
551 }
552 pub fn is_sixth(&self) -> bool {
554 matches!(self, Either::Sixth(_))
555 }
556 pub fn is_seventh(&self) -> bool {
558 matches!(self, Either::Seventh(_))
559 }
560}
561
562impl<
564 A: ParseMore,
565 B: ParseMore,
566 C: ParseMore,
567 D: ParseMore,
568 E: ParseMore,
569 F: ParseMore,
570 G: ParseMore,
571 > ParseMore for Either<A, B, C, D, E, F, G>
572{
573 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
574 let mut err;
575 match input.fork().parse::<ParseMoreWrapper<A>>() {
576 Ok(_) => return Ok(Self::First(input.parse::<ParseMoreWrapper<A>>().unwrap().0)),
577 Err(e) => err = e,
578 }
579 match input.fork().parse::<ParseMoreWrapper<B>>() {
580 Ok(_) => {
581 return Ok(Self::Second(
582 input.parse::<ParseMoreWrapper<B>>().unwrap().0,
583 ))
584 }
585 Err(e) => err.combine(e),
586 }
587 match input.fork().parse::<ParseMoreWrapper<C>>() {
588 Ok(_) => return Ok(Self::Third(input.parse::<ParseMoreWrapper<C>>().unwrap().0)),
589 Err(e) => err.combine(e),
590 }
591 match input.fork().parse::<ParseMoreWrapper<D>>() {
592 Ok(_) => {
593 return Ok(Self::Fourth(
594 input.parse::<ParseMoreWrapper<D>>().unwrap().0,
595 ))
596 }
597 Err(e) => err.combine(e),
598 }
599 match input.fork().parse::<ParseMoreWrapper<E>>() {
600 Ok(_) => return Ok(Self::Fifth(input.parse::<ParseMoreWrapper<E>>().unwrap().0)),
601 Err(e) => err.combine(e),
602 }
603 match input.fork().parse::<ParseMoreWrapper<F>>() {
604 Ok(_) => return Ok(Self::Sixth(input.parse::<ParseMoreWrapper<F>>().unwrap().0)),
605 Err(e) => err.combine(e),
606 }
607 match input.fork().parse::<ParseMoreWrapper<G>>() {
608 Ok(_) => {
609 return Ok(Self::Seventh(
610 input.parse::<ParseMoreWrapper<G>>().unwrap().0,
611 ))
612 }
613 Err(e) => err.combine(e),
614 }
615 Err(err)
616 }
617}
618
619macro_rules! integer_impls {
620 ($($ty:ty),*) => {
621 $(
622 impl ParseMore for $ty {
624 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
625 input.parse::<LitInt>()?.base10_parse::<Self>()
626 }
627 }
628 )*
629 };
630}
631
632integer_impls! {
633 u8, u16, u32, u64, u128, usize,
634 i8, i16, i32, i64, i128, isize
635}
636
637impl ParseMore for String {
639 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
640 Ok(input.parse::<LitStr>()?.value())
641 }
642}
643
644#[macro_export]
646macro_rules! parse_more_quote {
647 ($($tt:tt)*) => {{
648 let __tmp: $crate::ParseMoreWrapper<_> = syn::parse_quote!($($tt:tt)*);
650 __tmp.0
651 }};
652}
653
654#[macro_export]
656macro_rules! parse_more_macro_input {
657 ($tokenstream:ident as $ty:ty) => {
658 match $crate::parse_more::<$ty>($tokenstream) {
659 Ok(data) => data,
660 Err(err) => {
661 return proc_macro::TokenStream::from(err.to_compile_error());
662 }
663 }
664 };
665 ($tokenstream:ident) => {
666 $crate::parse_more_macro_input!($tokenstream as _)
667 };
668}
669
670pub fn parse_more<T: ParseMore>(tokens: proc_macro::TokenStream) -> syn::Result<T> {
672 Ok(syn::parse::<ParseMoreWrapper<T>>(tokens)?.0)
673}
674
675pub fn parse2_more<T: ParseMore>(tokens: proc_macro2::TokenStream) -> syn::Result<T> {
677 Ok(syn::parse2::<ParseMoreWrapper<T>>(tokens)?.0)
678}
679
680pub fn parse_more_str<T: ParseMore>(s: &str) -> syn::Result<T> {
682 Ok(syn::parse_str::<ParseMoreWrapper<T>>(s)?.0)
683}