Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Re-enable unquoting generics and where clauses
Browse files Browse the repository at this point in the history
erickt committed May 1, 2015
1 parent 7985f05 commit a53b893
Showing 3 changed files with 79 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "quasi"
version = "0.1.10"
version = "0.1.11"
authors = ["Erick Tryzelaar <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A quasi-quoting macro system"
repository = "https://github.com/erickt/rust-quasi"

[dev-dependencies.quasi_macros]
version = "*"
path = "quasi_macros"
[dev-dependencies]
aster = "*"
quasi_macros = { version = "*", path = "quasi_macros" }
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -107,6 +107,30 @@ impl ToTokens for P<ast::TraitItem> {
}
}

impl ToTokens for ast::Generics {
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
use syntax::print::pprust;
use syntax::parse::parse_tts_from_source_str;

let s = pprust::generics_to_string(self);

parse_tts_from_source_str("<quote expansion".to_string(), s, cx.cfg(), cx.parse_sess())
}
}

impl ToTokens for ast::WhereClause {
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
use syntax::print::pprust;
use syntax::parse::parse_tts_from_source_str;

let s = pprust::to_string(|s| {
s.print_where_clause(&self)
});

parse_tts_from_source_str("<quote expansion".to_string(), s, cx.cfg(), cx.parse_sess())
}
}

impl ToTokens for P<ast::Stmt> {
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
vec![ast::TtToken(self.span, token::Interpolated(token::NtStmt(self.clone())))]
51 changes: 51 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -11,13 +11,16 @@
#![feature(plugin, rustc_private)]
#![plugin(quasi_macros)]

extern crate aster;
extern crate syntax;
extern crate quasi;

use syntax::ast;
use syntax::ext::base::ExtCtxt;
use syntax::ext::expand;
use syntax::parse;
use syntax::print::pprust;
use syntax::owned_slice::OwnedSlice;

fn make_ext_ctxt(sess: &parse::ParseSess) -> ExtCtxt {
let info = syntax::codemap::ExpnInfo {
@@ -190,3 +193,51 @@ fn test_quote_with_macro() {

assert_eq!(pprust::block_to_string(&block), "{ value!() }");
}

#[test]
fn test_quote_with_generics_and_where_clause() {
let sess = parse::new_parse_sess();
let cx = make_ext_ctxt(&sess);

let builder = aster::AstBuilder::new();
let generics = builder.generics()
.ty_param("T")
.trait_bound("Clone").build()
.build()
.build();

let where_clause = ast::WhereClause {
id: ast::DUMMY_NODE_ID,
predicates: vec![
ast::WherePredicate::BoundPredicate(
ast::WhereBoundPredicate {
span: syntax::codemap::DUMMY_SP,
bound_lifetimes: vec![],
bounded_ty: quote_ty!(&cx, T),
bounds: OwnedSlice::from_vec(vec![
ast::TraitTyParamBound(
ast::PolyTraitRef {
bound_lifetimes: vec![],
trait_ref: ast::TraitRef {
path: builder.path().id("Clone").build(),
ref_id: ast::DUMMY_NODE_ID,
},
span: syntax::codemap::DUMMY_SP,
},
ast::TraitBoundModifier::None,
),
]),
}
),
],
};

let item = quote_item!(&cx,
impl $generics Clone for Foo $where_clause {}
).unwrap();

assert_eq!(
pprust::item_to_string(&item),
"impl <T: Clone> Clone for Foo where T: Clone { }"
);
}

0 comments on commit a53b893

Please sign in to comment.