1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! # `metamatch!`
//!
//! A zero dependency proc-macro for practical metaprogramming.
//!
//! Macro expressions using familiar Rust syntax, evaluated by a tiny
//! interpreter.
//!
//! ## Supported Rust Syntax:
//! - `let` statements and pattern matching.
//! - `loop`, `while`, `while let`, and `for` loops, including `continue` and
//! `break`
//! - `if` and `else` blocks
//! - Functions (`fn`) and lambdas (`|..|`)
//! - Arrays (`[1,2,3]`)
//! - Ranges (`0..=10`)
//! - All expression operators like `+` or `<<`.
//!
//! ## Not supported:
//! - `struct`, `enum`, `type`, `trait`, generics, ...
//!
//! ## Builtin functions:
//! - `lowercase(str) -> str`
//! - `uppercase(str) -> str`
//! - `capitalize(str) -> str`
//! - `enumerate([T]) -> [(int, T)]`
//! - `zip([A], [B], ..) -> [(A, B, ..)]`
//! - `map([T], Fn(T) -> U) -> [U]`
//! - `chars(str) -> [char]`
//! - `bytes(str) -> [int]`
//! - `ident(str) -> token`
//! - `str(any) -> str`
//! - `len([T]) -> int`
//!
//! String functions also work on tokens.
//!
//! All functions support UFCS, so `[1,2,3].len() == len([1,2,3])`
//!
//! ## Special Purpose Macros:
//! - `quote! {..} -> [token]`: nested version of [`quote!`].
//! - `raw! {..} -> [token]`: raw Rust, no template tags or expanded meta
//! variables
//!
//! Just like Rust macros, you can use any of `{}`, `[]`, and `()`
//! interchangably for the macro invocations.
use TokenStream;
/// Generate repetitive syntax like trait impls without giving up on
/// `rustfmt` or rust-analyzer.
///
/// The last macro expression must be an unterminated block like `for` or
/// `while`.
///
/// See this crate's root for a basic [language documentation](`crate`).
///
///
/// ## Example
/// ```rust
/// use metamatch::replicate;
///
/// #[derive(PartialEq, Eq, PartialOrd, Ord)]
/// struct NodeIdx(usize);
///
/// #[replicate(
/// let traits = [Add, Sub, Mul, Div, Rem];
/// for (TRAIT, FUNC) in zip(traits, traits.map(lowercase))
/// )]
/// impl core::ops::TRAIT for NodeIdx {
/// type Output = Self;
/// fn FUNC(self, rhs: Self) -> Self {
/// NodeIdx(self.0.FUNC(rhs.0))
/// }
/// }
/// assert!(NodeIdx(1) + NodeIdx(2) == NodeIdx(3));
/// ```
/// Generate repetitive match arms for differently typed variants.
///
/// See this crate's root for a basic [language documentation](`crate`).
///
///
/// ## Example
/// ```rust
/// use metamatch::metamatch;
/// enum DynVec {
/// I8(Vec<i8>),
/// I16(Vec<i16>),
/// I32(Vec<i32>),
/// I64(Vec<i64>),
/// }
///
/// impl DynVec {
/// fn len(&self) -> usize {
/// metamatch!(match self {
/// #[expand(for X in [I8, I16, I32, I64])]
/// DynVec::X(v) => v.len(),
/// })
/// }
/// // v expands into v
/// fn len_(&self) -> usize {
/// match self {
/// DynVec::I8(v) => v.len(),
/// DynVec::I16(v) => v.len(),
/// DynVec::I32(v) => v.len(),
/// DynVec::I64(v) => v.len(),
/// }
/// }
/// }
/// ```
/// Evaluates arbitrary expressions.
///
/// See this crate's root for a basic [language documentation](`crate`).
///
/// ## Example
/// ```rust
/// use metamatch::unquote;
///
/// const ARRAY: [i32; 4] = unquote! {
/// let ELEMENTS = for X in 1..5 {
/// quote!(X,)
/// };
/// quote!([ELEMENTS])
/// };
/// assert_eq!(ARRAY, [1, 2, 3, 4]);
/// ```
/// Like [`unquote!`], but starts out in quoted mode, meaning that
/// tokens will be pasted verbatim.
///
/// Template tags `[< ... >]` can be used to construct dynamic elements.
///
/// See this crate's root for a basic [language documentation](`crate`).
///
/// ## Example
/// ```rust
/// use metamatch::quote;
///
/// quote! {
/// enum ErrorCode {
/// [<for err_id in 0..=42>]
/// [<ident("E" + str(err_id))>](String),
/// [</for>]
/// }
/// };
/// let err = ErrorCode::E42("oh noes!".to_owned());
/// ```
///
/// ## Supported template tags
/// - `[<for ..>] [</for>]`
/// - `[<while ..>] [</while>]`
/// - `[<while let ...>] [</while>]`
/// - `[<loop>] [</loop>]`
/// - `[<if ..>] [<else if ..>] [<else>] [</if>]`
/// - `[<fn .. (..)>] [</fn>]`
/// - `[<let .. = ..>]`;
/// - `[< ("arbitrary expressions") >]`
///
/// ## Special tags
/// - `[<unquote>][</unquote>]`: Evaluate expressions.
/// - `[<raw>]..[</raw>]`: Paste raw Rust without identifier replacements.