From 0c4efbb2b61a1309f144add2e680909d42530519 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 4 May 2019 21:57:20 +0800 Subject: Error out when parse_subtree is not a subtree --- crates/ra_mbe/src/mbe_parser.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index c7ab463e2..110783689 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -34,11 +34,14 @@ fn parse_subtree(tt: &tt::Subtree, transcriber: bool) -> Result match leaf { - tt::Leaf::Punct(tt::Punct { char: '$', .. }) => { + tt::Leaf::Punct(tt::Punct { char: '$', spacing }) => { if p.at_ident().is_some() { crate::Leaf::from(parse_var(&mut p, transcriber)?).into() - } else { + } else if let Some(tt::TokenTree::Subtree(_)) = p.current() { parse_repeat(&mut p, transcriber)?.into() + } else { + // Treat it as normal punct + crate::Leaf::from(tt::Punct { char: '$', spacing: *spacing }).into() } } tt::Leaf::Punct(punct) => crate::Leaf::from(*punct).into(), @@ -89,7 +92,7 @@ fn mk_repeat( } fn parse_repeat(p: &mut TtCursor, transcriber: bool) -> Result { - let subtree = p.eat_subtree().unwrap(); + let subtree = p.eat_subtree()?; let mut subtree = parse_subtree(subtree, transcriber)?; subtree.delimiter = crate::Delimiter::None; -- cgit v1.2.3 From 50f288db925c5473f45d43d355e6819032298d37 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 4 May 2019 22:32:48 +0800 Subject: Add test --- crates/ra_mbe/src/mbe_parser.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index 110783689..8e1e31e7d 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -124,6 +124,8 @@ mod tests { expect_err("invalid", "subtree"); is_valid("($i:ident) => ()"); + is_valid("($($i:ident)*) => ($_)"); + expect_err("$i:ident => ()", "subtree"); expect_err("($i:ident) ()", "`=`"); expect_err("($($i:ident)_) => ()", "repeat"); -- cgit v1.2.3