Skip to content

Instantly share code, notes, and snippets.

@SethDusek
Last active December 11, 2016 08:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SethDusek/7e3bf86d4df7a82e8de75e0376d7ebc5 to your computer and use it in GitHub Desktop.
Save SethDusek/7e3bf86d4df7a82e8de75e0376d7ebc5 to your computer and use it in GitHub Desktop.
[package]
authors = ["SethDusek <shibe@openmailbox.org>"]
name = "catch_match"
version = "0.1.0"
[dependencies]
quasi = "0.28.0"
quasi_macros = "0.28.0"
[lib]
crate-type = ["dylib"]
#![crate_type="dylib"]
#![feature(plugin_registrar, rustc_private, plugin)]
#![plugin(quasi_macros)]
extern crate syntax;
extern crate rustc;
extern crate rustc_plugin;
#[macro_use] extern crate quasi;
use syntax::tokenstream::TokenTree;
use syntax::parse::token::{Token, DelimToken};
use syntax::ext::base::{ExtCtxt, MacResult, MacEager};
use syntax::ext::quote::rt::Span;
use rustc_plugin::Registry;
fn catch(ctx: &mut ExtCtxt, span: Span, args: &[TokenTree]) -> Box<MacResult + 'static> {
let mut args = args.to_owned();
args.insert(0, TokenTree::Token(span, Token::OpenDelim(DelimToken::Brace)));
args.push(TokenTree::Token(span.end_point(), Token::CloseDelim(DelimToken::Brace)));
let expr = ctx.new_parser_from_tts(&args).parse_block().unwrap();
MacEager::expr(quote_expr!(ctx, (|| $expr)()))
}
#[plugin_registrar]
pub fn register(reg: &mut Registry) {
reg.register_macro("catch", catch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment