Created
May 1, 2018 21:37
-
-
Save alexcrichton/6b4c56549c6732e697eb0aa9986bf398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(proc_macro)] | |
extern crate proc_macro; | |
use proc_macro::*; | |
#[proc_macro] | |
pub fn foo(input: TokenStream) -> TokenStream { | |
let mut body = Vec::<TokenTree>::new(); | |
// input = `let a = 2;` | |
body.extend(input.into_iter()); | |
// manually add `let b = a;` | |
body.push(Term::new("let", Span::call_site()).into()); | |
body.push(Term::new("b", Span::call_site()).into()); | |
body.push(Op::new('=', Spacing::Alone).into()); | |
body.push(Term::new("a", Span::call_site()).into()); | |
body.push(Op::new(';', Spacing::Alone).into()); | |
// parse and add `let c = b;` | |
body.extend("let c = b;".parse::<TokenStream>().unwrap().into_iter()); | |
let mut ret = Vec::<TokenTree>::new(); | |
ret.push(Term::new("fn", Span::call_site()).into()); | |
ret.push(Term::new("main", Span::call_site()).into()); | |
ret.push(Group::new(Delimiter::Parenthesis, TokenStream::empty()).into()); | |
ret.push(Group::new(Delimiter::Brace, body.into_iter().collect()).into()); | |
ret.into_iter().collect() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(proc_macro)] | |
extern crate foo; | |
foo::foo! { | |
let a = 3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment