1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#![feature(proc_macro_def_site, proc_macro_quote, proc_macro_hygiene, trait_alias)]

mod rule;

extern crate proc_macro;

use proc_macro::{TokenStream, TokenTree};

trait TokenStreamExt {
    fn and(self, other: TokenStream) -> TokenStream;
}

impl TokenStreamExt for TokenStream {
    fn and(mut self, other: TokenStream) -> TokenStream {
        self.extend(Some(other));
        self
    }
}

impl TokenStreamExt for TokenTree {
    fn and(self, other: TokenStream) -> TokenStream {
        let mut this = TokenStream::from(self);
        this.extend(Some(other));
        this
    }
}

#[proc_macro]
pub fn rule(stream: TokenStream) -> TokenStream {
    rule::parse_rule(&mut stream.into_iter()).unwrap().into()
}