Restrict grammer of 2.0 macros
The arguments must be parenthesized and the body must be braced.
diff --git a/src/item.rs b/src/item.rs
index 7fd97a3..7d0728c 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -514,7 +514,7 @@
what: syn!(Path) >>
bang: punct!(!) >>
ident: option!(syn!(Ident)) >>
- body: call!(mac::parsing::parse_tt_delimited) >>
+ body: call!(tt::delimited) >>
cond!(!mac::is_braced(&body), punct!(;)) >>
(ItemMacro {
attrs: attrs,
@@ -533,8 +533,8 @@
vis: syn!(Visibility) >>
macro_: keyword!(macro) >>
ident: syn!(Ident) >>
- args: call!(mac::parsing::parse_tt_delimited) >>
- body: call!(mac::parsing::parse_tt_delimited) >>
+ args: call!(tt::parenthesized) >>
+ body: call!(tt::braced) >>
(ItemMacro2 {
attrs: attrs,
vis: vis,
diff --git a/src/lib.rs b/src/lib.rs
index 106a045..edfea5c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -101,6 +101,8 @@
#[cfg(feature = "parsing")]
pub mod synom;
pub mod delimited;
+#[cfg(feature = "parsing")]
+mod tt;
mod gen {
#[cfg(feature = "visit")]
diff --git a/src/mac.rs b/src/mac.rs
index f093a8c..2316506 100644
--- a/src/mac.rs
+++ b/src/mac.rs
@@ -170,16 +170,13 @@
pub mod parsing {
use super::*;
- use proc_macro2::{TokenNode, TokenTree};
use synom::Synom;
- use cursor::Cursor;
- use {parse_error, PResult};
impl Synom for Macro {
named!(parse -> Self, do_parse!(
what: syn!(Path) >>
bang: punct!(!) >>
- body: call!(parse_tt_delimited) >>
+ body: call!(tt::delimited) >>
(Macro {
path: what,
bang_token: bang,
@@ -187,19 +184,6 @@
})
));
}
-
- pub fn parse_tt_delimited(input: Cursor) -> PResult<TokenTree> {
- match input.token_tree() {
- Some((
- rest,
- token @ TokenTree {
- kind: TokenNode::Group(..),
- ..
- },
- )) => Ok((rest, token)),
- _ => parse_error(),
- }
- }
}
#[cfg(feature = "printing")]
diff --git a/src/tt.rs b/src/tt.rs
new file mode 100644
index 0000000..8a7f0e5
--- /dev/null
+++ b/src/tt.rs
@@ -0,0 +1,47 @@
+use proc_macro2::{TokenNode, TokenTree};
+use cursor::Cursor;
+use {parse_error, PResult};
+
+#[cfg(feature = "full")]
+use proc_macro2::Delimiter;
+
+pub fn delimited(input: Cursor) -> PResult<TokenTree> {
+ match input.token_tree() {
+ Some((
+ rest,
+ token @ TokenTree {
+ kind: TokenNode::Group(..),
+ ..
+ },
+ )) => Ok((rest, token)),
+ _ => parse_error(),
+ }
+}
+
+#[cfg(feature = "full")]
+pub fn braced(input: Cursor) -> PResult<TokenTree> {
+ match input.token_tree() {
+ Some((
+ rest,
+ token @ TokenTree {
+ kind: TokenNode::Group(Delimiter::Brace, ..),
+ ..
+ },
+ )) => Ok((rest, token)),
+ _ => parse_error(),
+ }
+}
+
+#[cfg(feature = "full")]
+pub fn parenthesized(input: Cursor) -> PResult<TokenTree> {
+ match input.token_tree() {
+ Some((
+ rest,
+ token @ TokenTree {
+ kind: TokenNode::Group(Delimiter::Parenthesis, ..),
+ ..
+ },
+ )) => Ok((rest, token)),
+ _ => parse_error(),
+ }
+}