From 6646d49f238bb92d55fcb4900830f19faa2994a5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 13 Apr 2019 18:38:31 +0800 Subject: Fix bug and add expr , pat , ty matcher --- crates/ra_mbe/src/lib.rs | 97 +++++++++++++++++++++++++++++++++++++ crates/ra_mbe/src/mbe_expander.rs | 13 +++++ crates/ra_mbe/src/subtree_parser.rs | 12 +++++ crates/ra_mbe/src/subtree_source.rs | 14 ++++-- crates/ra_mbe/src/tt_cursor.rs | 15 ++++++ 5 files changed, 148 insertions(+), 3 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 4126854d1..a530f3b03 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -189,6 +189,14 @@ impl_froms!(TokenTree: Leaf, Subtree); rules.expand(&invocation_tt).unwrap() } + pub(crate) fn expand_to_syntax( + rules: &MacroRules, + invocation: &str, + ) -> ra_syntax::TreeArc { + let expanded = expand(rules, invocation); + token_tree_to_ast_item_list(&expanded) + } + pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { let expanded = expand(rules, invocation); assert_eq!(expanded.to_string(), expansion); @@ -485,4 +493,93 @@ SOURCE_FILE@[0; 40) ); assert_expansion(&rules, "foo! { foo }", "fn foo () {let a = foo :: bar ;}"); } + + #[test] + fn test_expr() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:expr) => { + fn bar() { $ i; } + } + } +"#, + ); + + assert_expansion( + &rules, + "foo! { 2 + 2 * baz(3).quux() }", + "fn bar () {2 + 2 * baz (3) . quux () ;}", + ); + } + + #[test] + fn test_expr_order() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:expr) => { + fn bar() { $ i * 2; } + } + } +"#, + ); + + assert_eq!( + expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), + r#"SOURCE_FILE@[0; 15) + FN_DEF@[0; 15) + FN_KW@[0; 2) "fn" + NAME@[2; 5) + IDENT@[2; 5) "bar" + PARAM_LIST@[5; 7) + L_PAREN@[5; 6) "(" + R_PAREN@[6; 7) ")" + BLOCK@[7; 15) + L_CURLY@[7; 8) "{" + EXPR_STMT@[8; 14) + BIN_EXPR@[8; 13) + BIN_EXPR@[8; 11) + LITERAL@[8; 9) + INT_NUMBER@[8; 9) "1" + PLUS@[9; 10) "+" + LITERAL@[10; 11) + INT_NUMBER@[10; 11) "1" + STAR@[11; 12) "*" + LITERAL@[12; 13) + INT_NUMBER@[12; 13) "2" + SEMI@[13; 14) ";" + R_CURLY@[14; 15) "}""#, + ); + } + + #[test] + fn test_ty() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:ty) => ( + fn bar() -> $ i { unimplemented!() } + ) + } +"#, + ); + assert_expansion( + &rules, + "foo! { Baz }", + "fn bar () -> Baz < u8 > {unimplemented ! ()}", + ); + } + + #[test] + fn test_pat_() { + let rules = create_rules( + r#" + macro_rules! foo { + ($ i:pat) => { fn foo() { let $ i; } } + } +"#, + ); + assert_expansion(&rules, "foo! { (a, b) }", "fn foo () {let (a , b) ;}"); + } } diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index ce41d7225..7a259f338 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -144,6 +144,19 @@ fn match_lhs(pattern: &crate::Subtree, input: &mut TtCursor) -> Result { + let expr = + input.eat_expr().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(expr.into())); + } + "ty" => { + let ty = input.eat_ty().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(ty.into())); + } + "pat" => { + let pat = input.eat_pat().ok_or(ExpandError::UnexpectedToken)?.clone(); + res.inner.insert(text.clone(), Binding::Simple(pat.into())); + } _ => return Err(ExpandError::UnexpectedToken), } } diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs index 164240d92..13d5d2169 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs @@ -30,6 +30,18 @@ impl<'a> Parser<'a> { self.parse(ra_parser::parse_path) } + pub fn parse_expr(self) -> Option { + self.parse(ra_parser::parse_expr) + } + + pub fn parse_ty(self) -> Option { + self.parse(ra_parser::parse_ty) + } + + pub fn parse_pat(self) -> Option { + self.parse(ra_parser::parse_pat) + } + fn parse(self, f: F) -> Option where F: FnOnce(&dyn TokenSource, &mut dyn TreeSink), diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 6aa20057e..0a070b46a 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -109,6 +109,8 @@ impl<'a> SubTreeWalker<'a> { self.cursor = match self.ts.get(0) { DelimToken::Token(token) => match token { tt::TokenTree::Subtree(subtree) => { + let ts = TokenSeq::from(subtree); + self.stack.push((ts, 0)); WalkCursor::Token(0, convert_delim(subtree.delimiter, false)) } tt::TokenTree::Leaf(leaf) => { @@ -254,7 +256,7 @@ impl<'a> WalkerOwner<'a> { } } } else if walker.stack.len() == 1 { - if let DelimToken::Delim(_, is_end) = walker.ts.get(*u) { + if let DelimToken::Delim(_, is_end) = walker.top().get(*u) { if !is_end { let (_, last_idx) = &walker.stack[0]; if let DelimToken::Token(token) = walker.ts.get(*last_idx) { @@ -310,10 +312,16 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { } } fn is_token_joint_to_next(&self, pos: usize) -> bool { - self.walker.get(pos).unwrap().is_joint_to_next + match self.walker.get(pos) { + Some(t) => t.is_joint_to_next, + _ => false, + } } fn is_keyword(&self, pos: usize, kw: &str) -> bool { - self.walker.get(pos).unwrap().text == *kw + match self.walker.get(pos) { + Some(t) => t.text == *kw, + _ => false, + } } } diff --git a/crates/ra_mbe/src/tt_cursor.rs b/crates/ra_mbe/src/tt_cursor.rs index d29faa77c..f6cefe087 100644 --- a/crates/ra_mbe/src/tt_cursor.rs +++ b/crates/ra_mbe/src/tt_cursor.rs @@ -84,6 +84,21 @@ impl<'a> TtCursor<'a> { parser.parse_path() } + pub(crate) fn eat_expr(&mut self) -> Option { + let parser = Parser::new(&mut self.pos, self.subtree); + parser.parse_expr() + } + + pub(crate) fn eat_ty(&mut self) -> Option { + let parser = Parser::new(&mut self.pos, self.subtree); + parser.parse_ty() + } + + pub(crate) fn eat_pat(&mut self) -> Option { + let parser = Parser::new(&mut self.pos, self.subtree); + parser.parse_pat() + } + pub(crate) fn expect_char(&mut self, char: char) -> Result<(), ParseError> { if self.at_char(char) { self.bump(); -- cgit v1.2.3