Remove duplicate TokenStream
quoter tests (modulo imports).
This commit is contained in:
parent
e5b0829bb0
commit
ec29011346
4 changed files with 0 additions and 233 deletions
|
@ -1,65 +0,0 @@
|
||||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
#![allow(unused_parens)]
|
|
||||||
#![feature(plugin)]
|
|
||||||
#![feature(plugin_registrar)]
|
|
||||||
#![feature(rustc_private)]
|
|
||||||
#![plugin(proc_macro_plugin)]
|
|
||||||
|
|
||||||
extern crate rustc_plugin;
|
|
||||||
extern crate proc_macro_tokens;
|
|
||||||
extern crate syntax;
|
|
||||||
|
|
||||||
use proc_macro_tokens::build::ident_eq;
|
|
||||||
|
|
||||||
use syntax::ast::Ident;
|
|
||||||
use syntax::ext::base::{ExtCtxt, MacResult};
|
|
||||||
use syntax::ext::proc_macro_shim::build_block_emitter;
|
|
||||||
use syntax::tokenstream::{TokenTree, TokenStream};
|
|
||||||
use syntax::codemap::Span;
|
|
||||||
|
|
||||||
use rustc_plugin::Registry;
|
|
||||||
|
|
||||||
#[plugin_registrar]
|
|
||||||
pub fn plugin_registrar(reg: &mut Registry) {
|
|
||||||
reg.register_macro("cond", cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cond<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'cx> {
|
|
||||||
let output = cond_rec(TokenStream::from_tts(tts.clone().to_owned()));
|
|
||||||
build_block_emitter(cx, sp, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cond_rec(input: TokenStream) -> TokenStream {
|
|
||||||
if input.is_empty() {
|
|
||||||
return qquote!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let next = input.slice(0..1);
|
|
||||||
let rest = input.slice_from(1..);
|
|
||||||
|
|
||||||
let clause : TokenStream = match next.maybe_delimited() {
|
|
||||||
Some(ts) => ts,
|
|
||||||
_ => panic!("Invalid input"),
|
|
||||||
};
|
|
||||||
|
|
||||||
// clause is ([test]) [rhs]
|
|
||||||
if clause.len() < 2 { panic!("Invalid macro usage in cond: {:?}", clause) }
|
|
||||||
|
|
||||||
let test: TokenStream = clause.slice(0..1);
|
|
||||||
let rhs: TokenStream = clause.slice_from(1..);
|
|
||||||
|
|
||||||
if ident_eq(&test[0], Ident::from_str("else")) || rest.is_empty() {
|
|
||||||
qquote!({unquote(rhs)})
|
|
||||||
} else {
|
|
||||||
qquote!({if unquote(test) { unquote(rhs) } else { cond!(unquote(rest)) } })
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,60 +0,0 @@
|
||||||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
#![allow(unused_parens)]
|
|
||||||
#![feature(plugin)]
|
|
||||||
#![feature(plugin_registrar)]
|
|
||||||
#![feature(rustc_private)]
|
|
||||||
#![plugin(proc_macro_plugin)]
|
|
||||||
|
|
||||||
extern crate rustc_plugin;
|
|
||||||
extern crate proc_macro_tokens;
|
|
||||||
extern crate syntax;
|
|
||||||
|
|
||||||
use syntax::ext::proc_macro_shim::prelude::*;
|
|
||||||
use proc_macro_tokens::prelude::*;
|
|
||||||
|
|
||||||
use rustc_plugin::Registry;
|
|
||||||
|
|
||||||
#[plugin_registrar]
|
|
||||||
pub fn plugin_registrar(reg: &mut Registry) {
|
|
||||||
reg.register_macro("cond", cond);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cond<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<MacResult + 'cx> {
|
|
||||||
let output = cond_rec(TokenStream::from_tts(tts.clone().to_owned()));
|
|
||||||
build_block_emitter(cx, sp, output)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cond_rec(input: TokenStream) -> TokenStream {
|
|
||||||
if input.is_empty() {
|
|
||||||
return qquote!();
|
|
||||||
}
|
|
||||||
|
|
||||||
let next = input.slice(0..1);
|
|
||||||
let rest = input.slice_from(1..);
|
|
||||||
|
|
||||||
let clause : TokenStream = match next.maybe_delimited() {
|
|
||||||
Some(ts) => ts,
|
|
||||||
_ => panic!("Invalid input"),
|
|
||||||
};
|
|
||||||
|
|
||||||
// clause is ([test]) [rhs]
|
|
||||||
if clause.len() < 2 { panic!("Invalid macro usage in cond: {:?}", clause) }
|
|
||||||
|
|
||||||
let test: TokenStream = clause.slice(0..1);
|
|
||||||
let rhs: TokenStream = clause.slice_from(1..);
|
|
||||||
|
|
||||||
if ident_eq(&test[0], Ident::from_str("else")) || rest.is_empty() {
|
|
||||||
qquote!({unquote(rhs)})
|
|
||||||
} else {
|
|
||||||
qquote!({if unquote(test) { unquote(rhs) } else { cond!(unquote(rest)) } })
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,54 +0,0 @@
|
||||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
// aux-build:cond_noprelude_plugin.rs
|
|
||||||
// ignore-stage1
|
|
||||||
|
|
||||||
#![feature(plugin)]
|
|
||||||
#![feature(rustc_private)]
|
|
||||||
#![plugin(cond_noprelude_plugin)]
|
|
||||||
|
|
||||||
fn fact(n : i64) -> i64 {
|
|
||||||
if n == 0 {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
n * fact(n - 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fact_cond(n : i64) -> i64 {
|
|
||||||
cond!(
|
|
||||||
((n == 0) 1)
|
|
||||||
(else (n * fact_cond(n-1)))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fib(n : i64) -> i64 {
|
|
||||||
if n == 0 || n == 1 {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
fib(n-1) + fib(n-2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fib_cond(n : i64) -> i64 {
|
|
||||||
cond!(
|
|
||||||
((n == 0) 1)
|
|
||||||
((n == 1) 1)
|
|
||||||
(else (fib_cond(n-1) + fib_cond(n-2)))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
assert_eq!(fact(3), fact_cond(3));
|
|
||||||
assert_eq!(fact(5), fact_cond(5));
|
|
||||||
assert_eq!(fib(5), fib_cond(5));
|
|
||||||
assert_eq!(fib(8), fib_cond(8));
|
|
||||||
}
|
|
|
@ -1,54 +0,0 @@
|
||||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
||||||
// file at the top-level directory of this distribution and at
|
|
||||||
// http://rust-lang.org/COPYRIGHT.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
||||||
// option. This file may not be copied, modified, or distributed
|
|
||||||
// except according to those terms.
|
|
||||||
|
|
||||||
// aux-build:cond_prelude_plugin.rs
|
|
||||||
// ignore-stage1
|
|
||||||
|
|
||||||
#![feature(plugin)]
|
|
||||||
#![feature(rustc_private)]
|
|
||||||
#![plugin(cond_prelude_plugin)]
|
|
||||||
|
|
||||||
fn fact(n : i64) -> i64 {
|
|
||||||
if n == 0 {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
n * fact(n - 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fact_cond(n : i64) -> i64 {
|
|
||||||
cond!(
|
|
||||||
((n == 0) 1)
|
|
||||||
(else (n * fact_cond(n-1)))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fib(n : i64) -> i64 {
|
|
||||||
if n == 0 || n == 1 {
|
|
||||||
1
|
|
||||||
} else {
|
|
||||||
fib(n-1) + fib(n-2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fib_cond(n : i64) -> i64 {
|
|
||||||
cond!(
|
|
||||||
((n == 0) 1)
|
|
||||||
((n == 1) 1)
|
|
||||||
(else (fib_cond(n-1) + fib_cond(n-2)))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
assert_eq!(fact(3), fact_cond(3));
|
|
||||||
assert_eq!(fact(5), fact_cond(5));
|
|
||||||
assert_eq!(fib(5), fib_cond(5));
|
|
||||||
assert_eq!(fib(8), fib_cond(8));
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue