Skip to content

Instantly share code, notes, and snippets.

@kevinmehall
Last active November 2, 2019 18:59
Show Gist options
  • Save kevinmehall/11351935 to your computer and use it in GitHub Desktop.
Save kevinmehall/11351935 to your computer and use it in GitHub Desktop.
Rust: Creating a ExtCtxt for quasiquote outside of a syntax extension
#![feature(quote)]
extern crate syntax;
use syntax::print::pprust;
use syntax::codemap::DUMMY_SP;
fn main() {
with_fake_extctxt(|cx| {
let x = quote_expr!(cx, (a+b));
let e = quote_expr!(cx, 2*$x+c);
println!("{}", pprust::expr_to_str(e));
});
}
fn with_fake_extctxt<T>(f: |&syntax::ext::base::ExtCtxt| -> T) -> T {
let ps = syntax::parse::new_parse_sess();
let mut cx = syntax::ext::base::ExtCtxt::new(&ps, Vec::new(), syntax::ext::expand::ExpansionConfig {
deriving_hash_type_parameter: false,
crate_id: from_str("test").unwrap(),
});
cx.bt_push(syntax::codemap::ExpnInfo{
call_site: DUMMY_SP,
callee: syntax::codemap::NameAndSpan {
name: "test".to_string(),
format: syntax::codemap::MacroBang,
span: None,
}
});
f(&cx)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment