From 4ec1dd991405404725b5cc448295dc0be9cd17e5 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Mon, 13 Aug 2012 16:03:13 -0700 Subject: [PATCH] Got the select macro working. --- src/libsyntax/ext/tt/macro_rules.rs | 12 ++++- src/test/run-pass/pipe-select-macro.rs | 62 ++++++++++++-------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 4a7becf1919..8abe8d8309d 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -47,9 +47,16 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, }; // Given `lhses` and `rhses`, this is the new macro we create - fn generic_extension(cx: ext_ctxt, sp: span, arg: ~[ast::token_tree], + fn generic_extension(cx: ext_ctxt, sp: span, name: ident, + arg: ~[ast::token_tree], lhses: ~[@named_match], rhses: ~[@named_match]) -> mac_result { + + //io::println(fmt!("%s! { %s }", *name, + // print::pprust::unexpanded_tt_to_str( + // ast::tt_delim(arg), + // cx.parse_sess().interner))); + // Which arm's failure should we report? (the one furthest along) let mut best_fail_spot = {lo: 0u, hi: 0u, expn_info: none}; let mut best_fail_msg = ~"internal error: ran no matchers"; @@ -90,7 +97,8 @@ fn add_new_extension(cx: ext_ctxt, sp: span, name: ident, cx.span_fatal(best_fail_spot, best_fail_msg); } - let exp = |cx, sp, arg| generic_extension(cx, sp, arg, lhses, rhses); + let exp = |cx, sp, arg| generic_extension(cx, sp, name, + arg, lhses, rhses); return mr_def({ ident: name, diff --git a/src/test/run-pass/pipe-select-macro.rs b/src/test/run-pass/pipe-select-macro.rs index 0a50e9ef899..40c96ba8588 100644 --- a/src/test/run-pass/pipe-select-macro.rs +++ b/src/test/run-pass/pipe-select-macro.rs @@ -1,4 +1,3 @@ -// xfail-test // Protocols proto! foo { @@ -25,21 +24,28 @@ proto! bar { // select! macro_rules! select_if { + + { + $index:expr, + $count:expr + } => { + fail + }; + { $index:expr, $count:expr, $port:path => [ - $($message:path$(($($x: ident),+))dont_type_this* - -> $next:ident $e:expr),+ - ], - $( $ports:path => [ - $($messages:path$(($($xs: ident),+))dont_type_this* - -> $nexts:ident $es:expr),+ - ], )* + $(type_this $message:path$(($(x $x: ident),+))dont_type_this* + -> $next:ident => { $e:expr }),+ + ] + $(, $ports:path => [ + $(type_this $messages:path$(($(x $xs: ident),+))dont_type_this* + -> $nexts:ident => { $es:expr }),+ + ] )* } => { - log_syntax!{select_if1}; if $index == $count { - alt move pipes::try_recv($port) { + match move pipes::try_recv($port) { $(some($message($($($x,)+)* next)) => { // FIXME (#2329) we really want move out of enum here. let $next = unsafe { let x <- *ptr::addr_of(next); x }; @@ -50,22 +56,14 @@ macro_rules! select_if { } else { select_if!{ $index, - $count + 1, - $( $ports => [ - $($messages$(($($xs),+))dont_type_this* - -> $nexts $es),+ - ], )* + $count + 1 + $(, $ports => [ + $(type_this $messages$(($(x $xs),+))dont_type_this* + -> $nexts => { $es }),+ + ])* } } }; - - { - $index:expr, - $count:expr, - } => { - log_syntax!{select_if2}; - fail - } } macro_rules! select { @@ -76,20 +74,16 @@ macro_rules! select { } )+ } => { let index = pipes::selecti([$(($port).header()),+]/_); - log_syntax!{select}; - log_syntax!{ - select_if!{index, 0, $( $port => [ - $($message$(($($x),+))dont_type_this* -> $next $e),+ - ], )+} - }; - select_if!{index, 0, $( $port => [ - $($message$(($($x),+))dont_type_this* -> $next $e),+ - ], )+} + select_if!{index, 0 $(, $port => [ + $(type_this $message$(($(x $x),+))dont_type_this* -> $next => { $e }),+ + ])+} } } // Code fn test(+foo: foo::client::foo, +bar: bar::client::bar) { + import bar::do_baz; + select! { foo => { foo::do_foo -> _next { @@ -98,11 +92,11 @@ fn test(+foo: foo::client::foo, +bar: bar::client::bar) { bar => { bar::do_bar(x) -> _next { - //debug!("%?", x) + debug!("%?", x) }, do_baz(b) -> _next { - //if b { debug!("true") } else { debug!("false") } + if b { debug!("true") } else { debug!("false") } } } }