defmac/
lib.rs

1#![cfg_attr(not(test), no_std)]
2
3//! A macro to define lambda-like macros inline.
4//!
5//! Syntax:
6//!
7//! `defmac!(` *name* [ *pattern* [, *pattern* ... ]] `=>` *expression* `)`
8//!
9//! *name* is the name of the new macro, followed by 0 or more patterns
10//! separated by comma. A pattern can be just an argument name like `x`
11//! or a pattern like `ref value`, `(x, y)` etc. Note that there is no comma
12//! between the name and the first pattern.
13//!
14//! # Example
15//!
16//! ```
17//! #[macro_use] extern crate defmac;
18//!
19//! fn main() {
20//!     defmac!(mkvec iter => iter.into_iter().collect::<Vec<_>>());
21//!
22//!     let v = mkvec!((0..10).map(|x| x * 2));
23//!
24//!     defmac!(repeat ref s, n => (0..n).map(|_| &s[..]).collect::<String>());
25//!
26//!     let text = String::from("abc");
27//!     let s = repeat!(text, 10);
28//!     let t = repeat!("-", s.len());
29//!     println!("{}", s);
30//!     println!("{}", t);
31//!
32//! }
33//! ```
34//!
35//! Did you know that macros can “capture” variables that they have in scope?
36//! The capture is by name instead of by reference, so we can use
37//! defmac where we cannot use closures. See the example below:
38//!
39//! ```
40//! #[macro_use] extern crate defmac;
41//!
42//! fn main() {
43//!     let mut result = Vec::new();
44//!     let mut sum = 0.;
45//!     let input = "2 2 ^ 7 b ^";
46//!
47//!     defmac!(push elem => result.push(elem));
48//!     defmac!(double => *result.last_mut().unwrap() *= 2);
49//!
50//!     for ch in input.chars() {
51//!         match ch {
52//!             '^' => double!(),
53//!             '0'...'9' => push!(ch as u32 - '0' as u32),
54//!             'a'...'z' => push!(ch as u32 - 'a' as u32),
55//!             _ => { }
56//!         }
57//!     }
58//!
59//!     assert_eq!(
60//!         result,
61//!         vec![2, 4, 7, 2]);
62//! }
63//! ```
64//!
65//! ## Rust Version
66//!
67//! This crate requires Rust 1.20 or later.
68
69/// A macro to define lambda-like macros inline.
70///
71/// Syntax:
72///
73/// `defmac!(` *name* [ *pattern* [, *pattern* ... ]] `=>` *expression* `)`
74///
75/// *name* is the name of the new macro, followed by 0 or more patterns
76/// separated by comma. A pattern can be just an argument name like `x`
77/// or a pattern like `ref value`, `(x, y)` etc.
78///
79/// Supports arbitrary many arguments.
80#[macro_export(local_inner_macros)]
81macro_rules! defmac {
82    // nest matches final rule
83    (@nest $name:ident ($dol:tt) => (
84        [$($arg:ident)*] $($result_body:tt)+)
85    ) => {
86        macro_rules! $name {
87            ($($dol $arg : expr), *) => {
88                $($result_body)+
89            }
90        }
91    };
92
93    // nest matches entry point and recursive rule
94    (@nest $name:ident ($dol:tt) => (
95            [$($arg:ident)*] $($result_body:tt)+
96        )
97        $p1:pat $(, $p2:pat)*
98    ) => {
99        // `marg` is a hygienic macro argument name
100        defmac!{@nest $name ($dol) => (
101            [marg $($arg)*]
102            match {$dol marg} { $p1 => $($result_body)+ }
103        )
104        $($p2),* }
105    };
106
107    // reverse patterns before passing them on to @nest
108    // reverse patterns final rule
109    (@revpats [$($args:tt)*] [$($pr:pat),*]) => {
110        defmac!{@nest $($args)* $($pr),*}
111    };
112
113    // reverse patterns entry point and recursive rule
114    (@revpats [$($args:tt)*] [$($pr:pat),*] $p1:pat $(, $p2:pat)*) => {
115        defmac!{@revpats [$($args)*] [$p1 $(, $pr)*] $($p2),*}
116    };
117
118    // entry point
119    ($name:ident $($p1:pat),* => $result:expr) => {
120        defmac!{@revpats [$name ($) => ([] $result)] [] $($p1),*}
121    };
122}
123
124
125#[cfg(test)]
126mod tests {
127    #[test]
128    fn it_works() {
129
130        let value = "xyz";
131
132        defmac!(none => value);
133        assert_eq!(none!(), "xyz");
134
135        defmac!(one x => x);
136        assert_eq!(one!(2), 2);
137
138        defmac!(two x, y => x + y);
139        assert_eq!(two!(1., 2.), 3.);
140
141        defmac!(three x, y, z => (x, y, z));
142        assert_eq!(three!(1, (2, 3), (4, 5, 6)), (1, (2, 3), (4, 5, 6)));
143
144        defmac!(four w, x, y, z => (w + x, z, y));
145        assert_eq!(four!(3, 4, "a", "b"), (7, "b", "a"));
146
147        defmac!(many a, b, c, d, e, f, g, h, i, j, k => (a, b + c, d + e + f,
148                                                         g + h + i + j + k));
149        assert_eq!(many!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),
150                   (1, 5, 15, 45));
151    }
152
153    #[test]
154    fn eval_order() {
155        use std::cell::Cell;
156        let v = Cell::new(0);
157        let f = || {
158            let n = v.get();
159            v.set(n + 1);
160            n
161        };
162
163        defmac!(two x, y => (x, y));
164
165        let result = two!(f(), f());
166        assert_eq!(result, (0, 1));
167        assert_eq!(f(), 2);
168    }
169
170    // Test expansion at module level
171    defmac! { triple value => [value; 3] }
172
173    #[test]
174    fn module_macro() {
175        assert_eq!(triple!(3), [3, 3, 3]);
176    }
177}