summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-05-09 01:38:39 +1000
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-05-15 07:02:43 +1000
commitb9824e18c2b02d74bd1bf646fea5f05477ca5071 (patch)
tree855c8c7f4c9f59d9790a4ee9ac74bf7d0f587735 /src/libsyntax
parentd217174987466010cd8810179c5fdb7ae4a126d0 (diff)
downloadrust-b9824e18c2b02d74bd1bf646fea5f05477ca5071.tar.gz
rust-b9824e18c2b02d74bd1bf646fea5f05477ca5071.zip
Add Scheme-style `cond!` macro to syntax::ext::expand
Addresses issue #6037
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ext/expand.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index 9afbe1e479d..ad1da0a8685 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -542,7 +542,41 @@ pub fn core_macros() -> ~str {
         }
     )
 
-
+    //
+    // A scheme-style conditional that helps to improve code clarity in some instances when
+    // the `if`, `else if`, and `else` keywords obscure predicates undesirably.
+    //
+    // # Example
+    //
+    // ~~~
+    // let clamped =
+    //     if x > mx { mx }
+    //     else if x < mn { mn }
+    //     else { x };
+    // ~~~
+    //
+    // Using `cond!`, the above could be written as:
+    //
+    // ~~~
+    // let clamped = cond!(
+    //     | x > mx { mx }
+    //     | x < mn { mn }
+    //     _        { x  }
+    // );
+    // ~~~
+    //
+    // The optional default case is denoted by `_`.
+    //
+    macro_rules! cond (
+        ($( | $pred:expr $body:block)+ _ $default:block ) => (
+            $(if $pred $body else)+
+            $default
+        );
+        // for if the default case was ommitted
+        ( $( | $pred:expr $body:block )+ ) => (
+            $(if $pred $body)else+
+        );
+    )
 }";
 }