From eaffdae3006ed0386f21dc31c499ffea24c32c98 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sat, 3 Apr 2021 03:08:31 +0200 Subject: Allow `,` to delimit macro 2.0 rules --- crates/mbe/src/lib.rs | 6 ++++-- crates/mbe/src/tests/expand.rs | 15 +++++++++++++++ crates/mbe/src/tt_iter.rs | 11 +++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) (limited to 'crates/mbe/src') diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index e74f8cf3f..3af5bc18b 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -220,9 +220,11 @@ impl MacroDef { while src.len() > 0 { let rule = Rule::parse(&mut src, true)?; rules.push(rule); - if let Err(()) = src.expect_char(';') { + if let Err(()) = src.expect_any_char(&[';', ',']) { if src.len() > 0 { - return Err(ParseError::Expected("expected `;`".to_string())); + return Err(ParseError::Expected( + "expected `;` or `,` to delimit rules".to_string(), + )); } break; } diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 8951f3813..e02d038b6 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -662,6 +662,21 @@ macro foo { .assert_expand_items("foo!(bar);", "fn bar () {}"); } +#[test] +fn test_macro_2_0_panic_2015() { + parse_macro2( + r#" +macro panic_2015 { + () => ( + ), + (bar) => ( + ), +} +"#, + ) + .assert_expand_items("panic_2015!(bar);", ""); +} + #[test] fn test_path() { parse_macro( diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs index 319a40f2a..195b8cf30 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs @@ -34,6 +34,17 @@ impl<'a> TtIter<'a> { } } + pub(crate) fn expect_any_char(&mut self, chars: &[char]) -> Result<(), ()> { + match self.next() { + Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. }))) + if chars.contains(c) => + { + Ok(()) + } + _ => Err(()), + } + } + pub(crate) fn expect_subtree(&mut self) -> Result<&'a tt::Subtree, ()> { match self.next() { Some(tt::TokenTree::Subtree(it)) => Ok(it), -- cgit v1.2.3