From 18f6a995d0fc1f45099f3cc810a5d55d5401b41b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 5 Dec 2019 15:10:33 +0100 Subject: Add expansion infrastructure for derive macros --- crates/ra_mbe/src/syntax_bridge.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 1de399fee..0fbcb2f66 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -2,7 +2,7 @@ use ra_parser::{FragmentKind, ParseError, TreeSink}; use ra_syntax::{ - ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, + ast, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxTreeBuilder, TextRange, TextUnit, T, }; use std::iter::successors; @@ -20,7 +20,7 @@ pub struct TokenMap { /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro /// will consume). -pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> { +pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> Option<(tt::Subtree, TokenMap)> { syntax_node_to_token_tree(ast.syntax()) } @@ -208,13 +208,8 @@ impl Convertor { } else if token.kind().is_trivia() { continue; } else if token.kind().is_punct() { - assert!( - token.text().len() == 1, - "Input ast::token punct must be single char." - ); - let char = token.text().chars().next().unwrap(); - - let spacing = match child_iter.peek() { + // we need to pull apart joined punctuation tokens + let last_spacing = match child_iter.peek() { Some(NodeOrToken::Token(token)) => { if token.kind().is_punct() { tt::Spacing::Joint @@ -224,8 +219,12 @@ impl Convertor { } _ => tt::Spacing::Alone, }; - - token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); + let spacing_iter = std::iter::repeat(tt::Spacing::Joint) + .take(token.text().len() - 1) + .chain(std::iter::once(last_spacing)); + for (char, spacing) in token.text().chars().zip(spacing_iter) { + token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); + } } else { let child: tt::TokenTree = if token.kind() == T![true] || token.kind() == T![false] { @@ -389,7 +388,10 @@ mod tests { use super::*; use crate::tests::{create_rules, expand}; use ra_parser::TokenSource; - use ra_syntax::algo::{insert_children, InsertPosition}; + use ra_syntax::{ + algo::{insert_children, InsertPosition}, + ast::AstNode, + }; #[test] fn convert_tt_token_source() { @@ -491,4 +493,12 @@ mod tests { assert_eq!(tt.delimiter, tt::Delimiter::Brace); } + + #[test] + fn test_token_tree_multi_char_punct() { + let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap(); + let struct_def = source_file.syntax().descendants().find_map(ast::StructDef::cast).unwrap(); + let tt = ast_to_token_tree(&struct_def).unwrap().0; + token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); + } } -- cgit v1.2.3 From ab4ecca210d8d280a4e216c2b6edfff303269144 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 5 Dec 2019 19:27:39 +0100 Subject: Don't wrap most syntax trees in invisible delimiters when converting to token tree Otherwise parsing them again doesn't work. --- crates/ra_mbe/src/syntax_bridge.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 0fbcb2f66..66c1f0337 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -245,8 +245,14 @@ impl Convertor { } } NodeOrToken::Node(node) => { - let child = self.go(&node)?.into(); - token_trees.push(child); + let child_subtree = self.go(&node)?; + if child_subtree.delimiter == tt::Delimiter::None + && node.kind() != SyntaxKind::TOKEN_TREE + { + token_trees.extend(child_subtree.token_trees); + } else { + token_trees.push(child_subtree.into()); + } } }; } -- cgit v1.2.3 From 98f98cbb5404385703a404547aa2477d4a2fd1cb Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 13 Dec 2019 21:53:34 +0800 Subject: Refactor tt::Delimiter --- crates/ra_mbe/src/lib.rs | 4 ++-- crates/ra_mbe/src/mbe_expander/matcher.rs | 6 ++--- crates/ra_mbe/src/mbe_expander/transcriber.rs | 10 ++++---- crates/ra_mbe/src/subtree_source.rs | 10 ++++---- crates/ra_mbe/src/syntax_bridge.rs | 34 +++++++++++++-------------- crates/ra_mbe/src/tests.rs | 2 +- 6 files changed, 32 insertions(+), 34 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index bbddebe67..0d2d43bef 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -159,14 +159,14 @@ impl Rule { .expect_subtree() .map_err(|()| ParseError::Expected("expected subtree".to_string()))? .clone(); - lhs.delimiter = tt::Delimiter::None; + lhs.delimiter = None; src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?; src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?; let mut rhs = src .expect_subtree() .map_err(|()| ParseError::Expected("expected subtree".to_string()))? .clone(); - rhs.delimiter = tt::Delimiter::None; + rhs.delimiter = None; Ok(crate::Rule { lhs, rhs }) } } diff --git a/crates/ra_mbe/src/mbe_expander/matcher.rs b/crates/ra_mbe/src/mbe_expander/matcher.rs index 33b9d483d..3f5136478 100644 --- a/crates/ra_mbe/src/mbe_expander/matcher.rs +++ b/crates/ra_mbe/src/mbe_expander/matcher.rs @@ -16,7 +16,7 @@ impl Bindings { fn push_optional(&mut self, name: &SmolStr) { // FIXME: Do we have a better way to represent an empty token ? // Insert an empty subtree for empty token - let tt = tt::Subtree { delimiter: tt::Delimiter::None, token_trees: vec![] }.into(); + let tt = tt::Subtree::default().into(); self.inner.insert(name.clone(), Binding::Fragment(Fragment::Tokens(tt))); } @@ -65,7 +65,7 @@ macro_rules! bail { } pub(super) fn match_(pattern: &tt::Subtree, src: &tt::Subtree) -> Result { - assert!(pattern.delimiter == tt::Delimiter::None); + assert!(pattern.delimiter == None); let mut res = Bindings::default(); let mut src = TtIter::new(src); @@ -210,7 +210,7 @@ impl<'a> TtIter<'a> { 0 => Err(()), 1 => Ok(res[0].clone()), _ => Ok(tt::TokenTree::Subtree(tt::Subtree { - delimiter: tt::Delimiter::None, + delimiter: None, token_trees: res.into_iter().cloned().collect(), })), } diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index ed094d5bb..f7636db11 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs @@ -50,7 +50,7 @@ pub(super) fn transcribe( template: &tt::Subtree, bindings: &Bindings, ) -> Result { - assert!(template.delimiter == tt::Delimiter::None); + assert!(template.delimiter == None); let mut ctx = ExpandCtx { bindings: &bindings, nesting: Vec::new(), var_expanded: false }; expand_subtree(&mut ctx, template) } @@ -106,7 +106,7 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result // ``` // We just treat it a normal tokens let tt = tt::Subtree { - delimiter: tt::Delimiter::None, + delimiter: None, token_trees: vec![ tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone }).into(), tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) @@ -147,7 +147,7 @@ fn expand_repeat( ctx.var_expanded = false; while let Ok(mut t) = expand_subtree(ctx, template) { - t.delimiter = tt::Delimiter::None; + t.delimiter = None; // if no var expanded in the child, we count it as a fail if !ctx.var_expanded { break; @@ -212,7 +212,7 @@ fn expand_repeat( // Check if it is a single token subtree without any delimiter // e.g {Delimiter:None> ['>'] /Delimiter:None>} - let tt = tt::Subtree { delimiter: tt::Delimiter::None, token_trees: buf }.into(); + let tt = tt::Subtree { delimiter: None, token_trees: buf }.into(); Ok(Fragment::Tokens(tt)) } @@ -225,7 +225,7 @@ fn push_fragment(buf: &mut Vec, fragment: Fragment) { fn push_subtree(buf: &mut Vec, tt: tt::Subtree) { match tt.delimiter { - tt::Delimiter::None => buf.extend(tt.token_trees), + None => buf.extend(tt.token_trees), _ => buf.push(tt.into()), } } diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 7ef45f6dc..061e9f20b 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -114,12 +114,12 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { } } -fn convert_delim(d: tt::Delimiter, closing: bool) -> TtToken { +fn convert_delim(d: Option, closing: bool) -> TtToken { let (kinds, texts) = match d { - tt::Delimiter::Parenthesis => ([T!['('], T![')']], "()"), - tt::Delimiter::Brace => ([T!['{'], T!['}']], "{}"), - tt::Delimiter::Bracket => ([T!['['], T![']']], "[]"), - tt::Delimiter::None => ([L_DOLLAR, R_DOLLAR], ""), + Some(tt::Delimiter::Parenthesis) => ([T!['('], T![')']], "()"), + Some(tt::Delimiter::Brace) => ([T!['{'], T!['}']], "{}"), + Some(tt::Delimiter::Bracket) => ([T!['['], T![']']], "[]"), + None => ([L_DOLLAR, R_DOLLAR], ""), }; let idx = closing as usize; diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 66c1f0337..b8e2cfc1d 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -51,7 +51,7 @@ pub fn token_tree_to_syntax_node( ) -> Result<(Parse, TokenMap), ExpandError> { let tmp; let tokens = match tt { - tt::Subtree { delimiter: tt::Delimiter::None, token_trees } => token_trees.as_slice(), + tt::Subtree { delimiter: None, token_trees } => token_trees.as_slice(), _ => { tmp = [tt.clone().into()]; &tmp[..] @@ -121,7 +121,7 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option Option { // This tree is empty if tt.first_child_or_token().is_none() { - return Some(tt::Subtree { token_trees: vec![], delimiter: tt::Delimiter::None }); + return Some(tt::Subtree { token_trees: vec![], delimiter: None }); } let first_child = tt.first_child_or_token()?; @@ -173,7 +173,7 @@ impl Convertor { .last() .unwrap(); if first_child.kind().is_trivia() { - return Some(tt::Subtree { token_trees: vec![], delimiter: tt::Delimiter::None }); + return Some(tt::Subtree { token_trees: vec![], delimiter: None }); } let last_child = successors(Some(last_child), |it| { @@ -187,10 +187,10 @@ impl Convertor { .unwrap(); let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) { - (T!['('], T![')']) => (tt::Delimiter::Parenthesis, true), - (T!['{'], T!['}']) => (tt::Delimiter::Brace, true), - (T!['['], T![']']) => (tt::Delimiter::Bracket, true), - _ => (tt::Delimiter::None, false), + (T!['('], T![')']) => (Some(tt::Delimiter::Parenthesis), true), + (T!['{'], T!['}']) => (Some(tt::Delimiter::Brace), true), + (T!['['], T![']']) => (Some(tt::Delimiter::Bracket), true), + _ => (None, false), }; let mut token_trees = Vec::new(); @@ -246,9 +246,7 @@ impl Convertor { } NodeOrToken::Node(node) => { let child_subtree = self.go(&node)?; - if child_subtree.delimiter == tt::Delimiter::None - && node.kind() != SyntaxKind::TOKEN_TREE - { + if child_subtree.delimiter.is_none() && node.kind() != SyntaxKind::TOKEN_TREE { token_trees.extend(child_subtree.token_trees); } else { token_trees.push(child_subtree.into()); @@ -299,16 +297,16 @@ impl<'a> TtTreeSink<'a> { } } -fn delim_to_str(d: tt::Delimiter, closing: bool) -> SmolStr { +fn delim_to_str(d: Option, closing: bool) -> SmolStr { let texts = match d { - tt::Delimiter::Parenthesis => "()", - tt::Delimiter::Brace => "{}", - tt::Delimiter::Bracket => "[]", - tt::Delimiter::None => "", + Some(tt::Delimiter::Parenthesis) => "()", + Some(tt::Delimiter::Brace) => "{}", + Some(tt::Delimiter::Bracket) => "[]", + None => return "".into(), }; let idx = closing as usize; - let text = if !texts.is_empty() { &texts[idx..texts.len() - (1 - idx)] } else { "" }; + let text = &texts[idx..texts.len() - (1 - idx)]; text.into() } @@ -497,7 +495,7 @@ mod tests { let token_tree = ast::TokenTree::cast(token_tree).unwrap(); let tt = ast_to_token_tree(&token_tree).unwrap().0; - assert_eq!(tt.delimiter, tt::Delimiter::Brace); + assert_eq!(tt.delimiter, Some(tt::Delimiter::Brace)); } #[test] diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 0109a4d98..148cc2625 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -1463,7 +1463,7 @@ pub(crate) fn assert_expansion( let wrapped = ast::SourceFile::parse(&wrapped); let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; - wrapped.delimiter = tt::Delimiter::None; + wrapped.delimiter = None; wrapped }; let (expanded_tree, expected_tree) = match kind { -- cgit v1.2.3 From b53587c7bdd67c63bd33a745fdaeb22a847b6c2f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 15 Dec 2019 01:46:39 +0800 Subject: Re-export Origin to replace ExpansionOrigin --- crates/ra_mbe/src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 0d2d43bef..ce2deadf6 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -104,6 +104,7 @@ impl Shift { } } +#[derive(Debug, Eq, PartialEq)] pub enum Origin { Def, Call, -- cgit v1.2.3 From aceb9d7fb0809ccf364514d9177342edea144c59 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 12 Dec 2019 21:47:54 +0800 Subject: Add token ids for all tt::Leaf --- crates/ra_mbe/src/mbe_expander/transcriber.rs | 7 ++++- crates/ra_mbe/src/syntax_bridge.rs | 45 ++++++++++++++++----------- crates/ra_mbe/src/tests.rs | 10 +++--- 3 files changed, 38 insertions(+), 24 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index f7636db11..eda66cd50 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs @@ -108,7 +108,12 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result let tt = tt::Subtree { delimiter: None, token_trees: vec![ - tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone }).into(), + tt::Leaf::from(tt::Punct { + char: '$', + spacing: tt::Spacing::Alone, + id: tt::TokenId::unspecified(), + }) + .into(), tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) .into(), ], diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index b8e2cfc1d..8f65ff125 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -136,11 +136,15 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option tt::TokenTree { - tt::TokenTree::from(tt::Leaf::from(tt::Punct { char: c, spacing: tt::Spacing::Alone })) + tt::TokenTree::from(tt::Leaf::from(tt::Punct { + char: c, + spacing: tt::Spacing::Alone, + id: tt::TokenId::unspecified(), + })) } fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { - let lit = tt::Literal { text: doc_comment_text(comment) }; + let lit = tt::Literal { text: doc_comment_text(comment), id: tt::TokenId::unspecified() }; tt::TokenTree::from(tt::Leaf::from(lit)) } @@ -223,24 +227,29 @@ impl Convertor { .take(token.text().len() - 1) .chain(std::iter::once(last_spacing)); for (char, spacing) in token.text().chars().zip(spacing_iter) { - token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); + let id = self.alloc(token.text_range()); + token_trees + .push(tt::Leaf::from(tt::Punct { char, spacing, id }).into()); } } else { - let child: tt::TokenTree = - if token.kind() == T![true] || token.kind() == T![false] { - tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() - } else if token.kind().is_keyword() - || token.kind() == IDENT - || token.kind() == LIFETIME - { - let id = self.alloc(token.text_range()); - let text = token.text().clone(); - tt::Leaf::from(tt::Ident { text, id }).into() - } else if token.kind().is_literal() { - tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() - } else { - return None; - }; + let child: tt::TokenTree = if token.kind() == T![true] + || token.kind() == T![false] + { + let id = self.alloc(token.text_range()); + tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() + } else if token.kind().is_keyword() + || token.kind() == IDENT + || token.kind() == LIFETIME + { + let id = self.alloc(token.text_range()); + let text = token.text().clone(); + tt::Leaf::from(tt::Ident { text, id }).into() + } else if token.kind().is_literal() { + let id = self.alloc(token.text_range()); + tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() + } else { + return None; + }; token_trees.push(child); } } diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 148cc2625..70e65bc74 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -78,12 +78,12 @@ macro_rules! foobar { assert_eq!(expansion.token_trees.len(), 3); // ($e:ident) => { foo bar $e } - // 0 1 2 3 4 - assert_eq!(get_id(&expansion.token_trees[0]), Some(2)); - assert_eq!(get_id(&expansion.token_trees[1]), Some(3)); + // 0123 45 6 7 89 + assert_eq!(get_id(&expansion.token_trees[0]), Some(6)); + assert_eq!(get_id(&expansion.token_trees[1]), Some(7)); - // So baz should be 5 - assert_eq!(get_id(&expansion.token_trees[2]), Some(5)); + // So baz should be 10 + assert_eq!(get_id(&expansion.token_trees[2]), Some(10)); } #[test] -- cgit v1.2.3 From 59295854f892b0a8f42a6fbc80b04d1f1c695828 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 13 Dec 2019 01:41:44 +0800 Subject: Add token id to delims --- crates/ra_mbe/src/mbe_expander/matcher.rs | 2 +- crates/ra_mbe/src/subtree_source.rs | 8 +- crates/ra_mbe/src/syntax_bridge.rs | 144 ++++++++++++++++++++---------- crates/ra_mbe/src/tests.rs | 16 ++-- 4 files changed, 113 insertions(+), 57 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/mbe_expander/matcher.rs b/crates/ra_mbe/src/mbe_expander/matcher.rs index 3f5136478..c67ae4110 100644 --- a/crates/ra_mbe/src/mbe_expander/matcher.rs +++ b/crates/ra_mbe/src/mbe_expander/matcher.rs @@ -106,7 +106,7 @@ fn match_subtree( } Op::TokenTree(tt::TokenTree::Subtree(lhs)) => { let rhs = src.expect_subtree().map_err(|()| err!("expected subtree"))?; - if lhs.delimiter != rhs.delimiter { + if lhs.delimiter.map(|it| it.kind) != rhs.delimiter.map(|it| it.kind) { bail!("mismatched delimiter") } let mut src = TtIter::new(rhs); diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 061e9f20b..5a03a372a 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -115,10 +115,10 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { } fn convert_delim(d: Option, closing: bool) -> TtToken { - let (kinds, texts) = match d { - Some(tt::Delimiter::Parenthesis) => ([T!['('], T![')']], "()"), - Some(tt::Delimiter::Brace) => ([T!['{'], T!['}']], "{}"), - Some(tt::Delimiter::Bracket) => ([T!['['], T![']']], "[]"), + let (kinds, texts) = match d.map(|it| it.kind) { + Some(tt::DelimiterKind::Parenthesis) => ([T!['('], T![')']], "()"), + Some(tt::DelimiterKind::Brace) => ([T!['{'], T!['}']], "{}"), + Some(tt::DelimiterKind::Bracket) => ([T!['['], T![']']], "[]"), None => ([L_DOLLAR, R_DOLLAR], ""), }; diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 8f65ff125..a85bb058b 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -5,6 +5,7 @@ use ra_syntax::{ ast, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxTreeBuilder, TextRange, TextUnit, T, }; +use rustc_hash::FxHashMap; use std::iter::successors; use tt::buffer::{Cursor, TokenBuffer}; @@ -83,6 +84,15 @@ impl TokenMap { fn insert(&mut self, token_id: tt::TokenId, relative_range: TextRange) { self.entries.push((token_id, relative_range)); } + + fn insert_delim( + &mut self, + _token_id: tt::TokenId, + _open_relative_range: TextRange, + _close_relative_range: TextRange, + ) { + // FIXME: Add entries for delimiter + } } /// Returns the textual content of a doc comment block as a quoted string @@ -121,7 +131,10 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option (Some(tt::Delimiter::Parenthesis), true), - (T!['{'], T!['}']) => (Some(tt::Delimiter::Brace), true), - (T!['['], T![']']) => (Some(tt::Delimiter::Bracket), true), + let (delimiter_kind, skip_first) = match (first_child.kind(), last_child.kind()) { + (T!['('], T![')']) => (Some(tt::DelimiterKind::Parenthesis), true), + (T!['{'], T!['}']) => (Some(tt::DelimiterKind::Brace), true), + (T!['['], T![']']) => (Some(tt::DelimiterKind::Bracket), true), _ => (None, false), }; + let delimiter = delimiter_kind.map(|kind| tt::Delimiter { + kind, + id: self.alloc_delim(first_child.text_range(), last_child.text_range()), + }); let mut token_trees = Vec::new(); let mut child_iter = tt.children_with_tokens().skip(skip_first as usize).peekable(); @@ -232,25 +249,31 @@ impl Convertor { .push(tt::Leaf::from(tt::Punct { char, spacing, id }).into()); } } else { - let child: tt::TokenTree = if token.kind() == T![true] - || token.kind() == T![false] - { - let id = self.alloc(token.text_range()); - tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() - } else if token.kind().is_keyword() - || token.kind() == IDENT - || token.kind() == LIFETIME - { - let id = self.alloc(token.text_range()); - let text = token.text().clone(); - tt::Leaf::from(tt::Ident { text, id }).into() - } else if token.kind().is_literal() { - let id = self.alloc(token.text_range()); - tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() - } else { - return None; + let child: tt::Leaf = match token.kind() { + T![true] | T![false] => { + let id = self.alloc(token.text_range()); + let text = token.text().clone(); + tt::Literal { text, id }.into() + } + IDENT | LIFETIME => { + let id = self.alloc(token.text_range()); + let text = token.text().clone(); + tt::Ident { text, id }.into() + } + k if k.is_keyword() => { + let id = self.alloc(token.text_range()); + let text = token.text().clone(); + tt::Ident { text, id }.into() + } + k if k.is_literal() => { + let id = self.alloc(token.text_range()); + let text = token.text().clone(); + tt::Literal { text, id }.into() + } + _ => return None, }; - token_trees.push(child); + + token_trees.push(child.into()); } } NodeOrToken::Node(node) => { @@ -275,11 +298,26 @@ impl Convertor { self.map.insert(token_id, relative_range); token_id } + + fn alloc_delim( + &mut self, + open_abs_range: TextRange, + close_abs_range: TextRange, + ) -> tt::TokenId { + let open_relative_range = open_abs_range - self.global_offset; + let close_relative_range = close_abs_range - self.global_offset; + let token_id = tt::TokenId(self.next_id); + self.next_id += 1; + + self.map.insert_delim(token_id, open_relative_range, close_relative_range); + token_id + } } struct TtTreeSink<'a> { buf: String, cursor: Cursor<'a>, + open_delims: FxHashMap, text_pos: TextUnit, inner: SyntaxTreeBuilder, token_map: TokenMap, @@ -294,6 +332,7 @@ impl<'a> TtTreeSink<'a> { TtTreeSink { buf: String::new(), cursor, + open_delims: FxHashMap::default(), text_pos: 0.into(), inner: SyntaxTreeBuilder::default(), roots: smallvec::SmallVec::new(), @@ -307,10 +346,10 @@ impl<'a> TtTreeSink<'a> { } fn delim_to_str(d: Option, closing: bool) -> SmolStr { - let texts = match d { - Some(tt::Delimiter::Parenthesis) => "()", - Some(tt::Delimiter::Brace) => "{}", - Some(tt::Delimiter::Bracket) => "[]", + let texts = match d.map(|it| it.kind) { + Some(tt::DelimiterKind::Parenthesis) => "()", + Some(tt::DelimiterKind::Brace) => "{}", + Some(tt::DelimiterKind::Bracket) => "[]", None => return "".into(), }; @@ -331,34 +370,49 @@ impl<'a> TreeSink for TtTreeSink<'a> { break; } - match self.cursor.token_tree() { + let text: Option = match self.cursor.token_tree() { Some(tt::TokenTree::Leaf(leaf)) => { // Mark the range if needed - if let tt::Leaf::Ident(ident) = leaf { - if kind == IDENT { - let range = - TextRange::offset_len(self.text_pos, TextUnit::of_str(&ident.text)); - self.token_map.insert(ident.id, range); - } - } - + let id = match leaf { + tt::Leaf::Ident(ident) => ident.id, + tt::Leaf::Punct(punct) => punct.id, + tt::Leaf::Literal(lit) => lit.id, + }; + let text = SmolStr::new(format!("{}", leaf)); + let range = TextRange::offset_len(self.text_pos, TextUnit::of_str(&text)); + self.token_map.insert(id, range); self.cursor = self.cursor.bump(); - self.buf += &format!("{}", leaf); + Some(text) } Some(tt::TokenTree::Subtree(subtree)) => { self.cursor = self.cursor.subtree().unwrap(); - self.buf += &delim_to_str(subtree.delimiter, false); - } - None => { - if let Some(parent) = self.cursor.end() { - self.cursor = self.cursor.bump(); - self.buf += &delim_to_str(parent.delimiter, true); + if let Some(id) = subtree.delimiter.map(|it| it.id) { + self.open_delims.insert(id, self.text_pos); } + Some(delim_to_str(subtree.delimiter, false)) } + None => self.cursor.end().and_then(|parent| { + self.cursor = self.cursor.bump(); + if let Some(id) = parent.delimiter.map(|it| it.id) { + if let Some(open_delim) = self.open_delims.get(&id) { + let open_range = + TextRange::offset_len(*open_delim, TextUnit::from_usize(1)); + let close_range = + TextRange::offset_len(self.text_pos, TextUnit::from_usize(1)); + self.token_map.insert_delim(id, open_range, close_range); + } + } + + Some(delim_to_str(parent.delimiter, true)) + }), }; + + if let Some(text) = text { + self.buf += &text; + self.text_pos += TextUnit::of_str(&text); + } } - self.text_pos += TextUnit::of_str(&self.buf); let text = SmolStr::new(self.buf.as_str()); self.buf.clear(); self.inner.token(kind, text); @@ -504,7 +558,7 @@ mod tests { let token_tree = ast::TokenTree::cast(token_tree).unwrap(); let tt = ast_to_token_tree(&token_tree).unwrap().0; - assert_eq!(tt.delimiter, Some(tt::Delimiter::Brace)); + assert_eq!(tt.delimiter.map(|it| it.kind), Some(tt::DelimiterKind::Brace)); } #[test] diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 70e65bc74..6bcfedcac 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -77,13 +77,15 @@ macro_rules! foobar { } assert_eq!(expansion.token_trees.len(), 3); - // ($e:ident) => { foo bar $e } - // 0123 45 6 7 89 - assert_eq!(get_id(&expansion.token_trees[0]), Some(6)); - assert_eq!(get_id(&expansion.token_trees[1]), Some(7)); - - // So baz should be 10 - assert_eq!(get_id(&expansion.token_trees[2]), Some(10)); + // {($e:ident) => { foo bar $e }} + // 012345 67 8 9 T 12 + assert_eq!(get_id(&expansion.token_trees[0]), Some(9)); + assert_eq!(get_id(&expansion.token_trees[1]), Some(10)); + + // The input args of macro call include parentheses: + // (baz) + // So baz should be 12+1+1 + assert_eq!(get_id(&expansion.token_trees[2]), Some(14)); } #[test] -- cgit v1.2.3 From 320416d7561e9926ecbbc392cc2efd48740610e0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 13 Dec 2019 23:55:51 +0800 Subject: Add TokenTextRange --- crates/ra_mbe/src/syntax_bridge.rs | 41 ++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index a85bb058b..44a51b7a5 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -12,11 +12,30 @@ use tt::buffer::{Cursor, TokenBuffer}; use crate::subtree_source::SubtreeTokenSource; use crate::ExpandError; +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum TokenTextRange { + Token(TextRange), + Delimiter(TextRange, TextRange), +} + +impl TokenTextRange { + pub fn range(self, kind: SyntaxKind) -> Option { + match self { + TokenTextRange::Token(it) => Some(it), + TokenTextRange::Delimiter(open, close) => match kind { + T!['{'] | T!['('] | T!['['] => Some(open), + T!['}'] | T![')'] | T![']'] => Some(close), + _ => None, + }, + } + } +} + /// Maps `tt::TokenId` to the relative range of the original token. #[derive(Debug, PartialEq, Eq, Default)] pub struct TokenMap { /// Maps `tt::TokenId` to the *relative* source range. - entries: Vec<(tt::TokenId, TextRange)>, + entries: Vec<(tt::TokenId, TokenTextRange)>, } /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro @@ -72,26 +91,32 @@ pub fn token_tree_to_syntax_node( impl TokenMap { pub fn token_by_range(&self, relative_range: TextRange) -> Option { - let &(token_id, _) = self.entries.iter().find(|(_, range)| *range == relative_range)?; + let &(token_id, _) = self.entries.iter().find(|(_, range)| match range { + TokenTextRange::Token(it) => *it == relative_range, + TokenTextRange::Delimiter(open, close) => { + *open == relative_range || *close == relative_range + } + })?; Some(token_id) } - pub fn range_by_token(&self, token_id: tt::TokenId) -> Option { + pub fn range_by_token(&self, token_id: tt::TokenId) -> Option { let &(_, range) = self.entries.iter().find(|(tid, _)| *tid == token_id)?; Some(range) } fn insert(&mut self, token_id: tt::TokenId, relative_range: TextRange) { - self.entries.push((token_id, relative_range)); + self.entries.push((token_id, TokenTextRange::Token(relative_range))); } fn insert_delim( &mut self, - _token_id: tt::TokenId, - _open_relative_range: TextRange, - _close_relative_range: TextRange, + token_id: tt::TokenId, + open_relative_range: TextRange, + close_relative_range: TextRange, ) { - // FIXME: Add entries for delimiter + self.entries + .push((token_id, TokenTextRange::Delimiter(open_relative_range, close_relative_range))); } } -- cgit v1.2.3 From 325532d11960733bebdd38b19a6f33a891872803 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 14 Dec 2019 03:37:04 +0800 Subject: Fix shift id for delim and other tokens --- crates/ra_mbe/src/lib.rs | 18 +++++++++++++++--- 1 file changed, 15 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 ce2deadf6..45dad2d10 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -67,7 +67,15 @@ impl Shift { .token_trees .iter() .filter_map(|tt| match tt { - tt::TokenTree::Subtree(subtree) => max_id(subtree), + tt::TokenTree::Subtree(subtree) => { + let tree_id = max_id(subtree); + match subtree.delimiter { + Some(it) if it.id != tt::TokenId::unspecified() => { + Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0))) + } + _ => tree_id, + } + } tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) if ident.id != tt::TokenId::unspecified() => { @@ -85,9 +93,13 @@ impl Shift { match t { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id), - _ => (), + tt::Leaf::Punct(punct) => punct.id = self.shift(punct.id), + tt::Leaf::Literal(lit) => lit.id = self.shift(lit.id), }, - tt::TokenTree::Subtree(tt) => self.shift_all(tt), + tt::TokenTree::Subtree(tt) => { + tt.delimiter.as_mut().map(|it: &mut Delimiter| it.id = self.shift(it.id)); + self.shift_all(tt) + } } } } -- cgit v1.2.3 From e16f3a5ee2f7df3f42827f3f279b5ed6774bde8e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 14 Dec 2019 03:39:15 +0800 Subject: Add test for token map --- crates/ra_mbe/src/tests.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 6bcfedcac..ae7f3dfd4 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -88,6 +88,32 @@ macro_rules! foobar { assert_eq!(get_id(&expansion.token_trees[2]), Some(14)); } +#[test] +fn test_token_map() { + use ra_parser::SyntaxKind::*; + use ra_syntax::T; + + let macro_definition = r#" +macro_rules! foobar { + ($e:ident) => { fn $e() {} } +} +"#; + let rules = create_rules(macro_definition); + let (expansion, (token_map, content)) = expand_and_map(&rules, "foobar!(baz);"); + + let get_text = |id, kind| -> String { + content[token_map.range_by_token(id).unwrap().range(kind).unwrap()].to_string() + }; + + assert_eq!(expansion.token_trees.len(), 4); + // {($e:ident) => { fn $e() {} }} + // 012345 67 8 9 T12 3 + + assert_eq!(get_text(tt::TokenId(9), IDENT), "fn"); + assert_eq!(get_text(tt::TokenId(12), T!['(']), "("); + assert_eq!(get_text(tt::TokenId(13), T!['{']), "{"); +} + #[test] fn test_convert_tt() { let macro_definition = r#" @@ -1443,6 +1469,23 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree { rules.expand(&invocation_tt).unwrap() } +pub(crate) fn expand_and_map( + rules: &MacroRules, + invocation: &str, +) -> (tt::Subtree, (TokenMap, String)) { + let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); + let macro_invocation = + source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + + let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); + let expanded = rules.expand(&invocation_tt).unwrap(); + + let (node, expanded_token_tree) = + token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap(); + + (expanded, (expanded_token_tree, node.syntax_node().to_string())) +} + pub(crate) enum MacroKind { Items, Stmts, -- cgit v1.2.3 From 2ea1cfd7804dd57d63196e71f66c22e56cfd79a8 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 18 Dec 2019 11:36:10 +0800 Subject: Rename range to by_kind --- crates/ra_mbe/src/syntax_bridge.rs | 2 +- crates/ra_mbe/src/tests.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 44a51b7a5..d585d57af 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -19,7 +19,7 @@ pub enum TokenTextRange { } impl TokenTextRange { - pub fn range(self, kind: SyntaxKind) -> Option { + pub fn by_kind(self, kind: SyntaxKind) -> Option { match self { TokenTextRange::Token(it) => Some(it), TokenTextRange::Delimiter(open, close) => match kind { diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index ae7f3dfd4..ff225f0db 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -102,7 +102,7 @@ macro_rules! foobar { let (expansion, (token_map, content)) = expand_and_map(&rules, "foobar!(baz);"); let get_text = |id, kind| -> String { - content[token_map.range_by_token(id).unwrap().range(kind).unwrap()].to_string() + content[token_map.range_by_token(id).unwrap().by_kind(kind).unwrap()].to_string() }; assert_eq!(expansion.token_trees.len(), 4); -- cgit v1.2.3 From 41544a40883874553f570e2999bf56d172bd6246 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 18 Dec 2019 11:47:26 +0800 Subject: Refactoring --- crates/ra_mbe/src/mbe_expander/matcher.rs | 2 +- crates/ra_mbe/src/subtree_source.rs | 8 +-- crates/ra_mbe/src/syntax_bridge.rs | 92 +++++++++++++++---------------- 3 files changed, 50 insertions(+), 52 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/mbe_expander/matcher.rs b/crates/ra_mbe/src/mbe_expander/matcher.rs index c67ae4110..e36b5a412 100644 --- a/crates/ra_mbe/src/mbe_expander/matcher.rs +++ b/crates/ra_mbe/src/mbe_expander/matcher.rs @@ -106,7 +106,7 @@ fn match_subtree( } Op::TokenTree(tt::TokenTree::Subtree(lhs)) => { let rhs = src.expect_subtree().map_err(|()| err!("expected subtree"))?; - if lhs.delimiter.map(|it| it.kind) != rhs.delimiter.map(|it| it.kind) { + if lhs.delimiter_kind() != rhs.delimiter_kind() { bail!("mismatched delimiter") } let mut src = TtIter::new(rhs); diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index 5a03a372a..b841c39d3 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -70,11 +70,11 @@ impl<'a> SubtreeTokenSource<'a> { } Some(tt::TokenTree::Subtree(subtree)) => { self.cached_cursor.set(cursor.subtree().unwrap()); - cached.push(Some(convert_delim(subtree.delimiter, false))); + cached.push(Some(convert_delim(subtree.delimiter_kind(), false))); } None => { if let Some(subtree) = cursor.end() { - cached.push(Some(convert_delim(subtree.delimiter, true))); + cached.push(Some(convert_delim(subtree.delimiter_kind(), true))); self.cached_cursor.set(cursor.bump()); } } @@ -114,8 +114,8 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { } } -fn convert_delim(d: Option, closing: bool) -> TtToken { - let (kinds, texts) = match d.map(|it| it.kind) { +fn convert_delim(d: Option, closing: bool) -> TtToken { + let (kinds, texts) = match d { Some(tt::DelimiterKind::Parenthesis) => ([T!['('], T![')']], "()"), Some(tt::DelimiterKind::Brace) => ([T!['{'], T!['}']], "{}"), Some(tt::DelimiterKind::Bracket) => ([T!['['], T![']']], "[]"), diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index d585d57af..2c60430d1 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -269,35 +269,33 @@ impl Convertor { .take(token.text().len() - 1) .chain(std::iter::once(last_spacing)); for (char, spacing) in token.text().chars().zip(spacing_iter) { - let id = self.alloc(token.text_range()); - token_trees - .push(tt::Leaf::from(tt::Punct { char, spacing, id }).into()); + token_trees.push( + tt::Leaf::from(tt::Punct { + char, + spacing, + id: self.alloc(token.text_range()), + }) + .into(), + ); } } else { + macro_rules! make_leaf { + ($i:ident) => { + tt::$i { + id: self.alloc(token.text_range()), + text: token.text().clone(), + } + .into() + }; + } + let child: tt::Leaf = match token.kind() { - T![true] | T![false] => { - let id = self.alloc(token.text_range()); - let text = token.text().clone(); - tt::Literal { text, id }.into() - } - IDENT | LIFETIME => { - let id = self.alloc(token.text_range()); - let text = token.text().clone(); - tt::Ident { text, id }.into() - } - k if k.is_keyword() => { - let id = self.alloc(token.text_range()); - let text = token.text().clone(); - tt::Ident { text, id }.into() - } - k if k.is_literal() => { - let id = self.alloc(token.text_range()); - let text = token.text().clone(); - tt::Literal { text, id }.into() - } + T![true] | T![false] => make_leaf!(Literal), + IDENT | LIFETIME => make_leaf!(Ident), + k if k.is_keyword() => make_leaf!(Ident), + k if k.is_literal() => make_leaf!(Literal), _ => return None, }; - token_trees.push(child.into()); } } @@ -370,8 +368,8 @@ impl<'a> TtTreeSink<'a> { } } -fn delim_to_str(d: Option, closing: bool) -> SmolStr { - let texts = match d.map(|it| it.kind) { +fn delim_to_str(d: Option, closing: bool) -> SmolStr { + let texts = match d { Some(tt::DelimiterKind::Parenthesis) => "()", Some(tt::DelimiterKind::Brace) => "{}", Some(tt::DelimiterKind::Bracket) => "[]", @@ -395,7 +393,7 @@ impl<'a> TreeSink for TtTreeSink<'a> { break; } - let text: Option = match self.cursor.token_tree() { + let text: SmolStr = match self.cursor.token_tree() { Some(tt::TokenTree::Leaf(leaf)) => { // Mark the range if needed let id = match leaf { @@ -407,35 +405,35 @@ impl<'a> TreeSink for TtTreeSink<'a> { let range = TextRange::offset_len(self.text_pos, TextUnit::of_str(&text)); self.token_map.insert(id, range); self.cursor = self.cursor.bump(); - Some(text) + text } Some(tt::TokenTree::Subtree(subtree)) => { self.cursor = self.cursor.subtree().unwrap(); if let Some(id) = subtree.delimiter.map(|it| it.id) { self.open_delims.insert(id, self.text_pos); } - Some(delim_to_str(subtree.delimiter, false)) + delim_to_str(subtree.delimiter_kind(), false) } - None => self.cursor.end().and_then(|parent| { - self.cursor = self.cursor.bump(); - if let Some(id) = parent.delimiter.map(|it| it.id) { - if let Some(open_delim) = self.open_delims.get(&id) { - let open_range = - TextRange::offset_len(*open_delim, TextUnit::from_usize(1)); - let close_range = - TextRange::offset_len(self.text_pos, TextUnit::from_usize(1)); - self.token_map.insert_delim(id, open_range, close_range); + None => { + if let Some(parent) = self.cursor.end() { + self.cursor = self.cursor.bump(); + if let Some(id) = parent.delimiter.map(|it| it.id) { + if let Some(open_delim) = self.open_delims.get(&id) { + let open_range = + TextRange::offset_len(*open_delim, TextUnit::from_usize(1)); + let close_range = + TextRange::offset_len(self.text_pos, TextUnit::from_usize(1)); + self.token_map.insert_delim(id, open_range, close_range); + } } + delim_to_str(parent.delimiter_kind(), true) + } else { + continue; } - - Some(delim_to_str(parent.delimiter, true)) - }), + } }; - - if let Some(text) = text { - self.buf += &text; - self.text_pos += TextUnit::of_str(&text); - } + self.buf += &text; + self.text_pos += TextUnit::of_str(&text); } let text = SmolStr::new(self.buf.as_str()); @@ -583,7 +581,7 @@ mod tests { let token_tree = ast::TokenTree::cast(token_tree).unwrap(); let tt = ast_to_token_tree(&token_tree).unwrap().0; - assert_eq!(tt.delimiter.map(|it| it.kind), Some(tt::DelimiterKind::Brace)); + assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); } #[test] -- cgit v1.2.3 From 5c3c2b869085a26187c0f7c7bcd4ee58bc1075a9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Dec 2019 14:43:19 +0100 Subject: Fix parsing of interpolated expressions --- crates/ra_mbe/src/tests.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index ff225f0db..b5a10a9d2 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -1450,6 +1450,24 @@ macro_rules! delegate_impl { ); } +#[test] +fn expr_interpolation() { + let rules = create_rules( + r#" + macro_rules! id { + ($expr:expr) => { + map($expr) + } + } + "#, + ); + + let expanded = expand(&rules, "id!(x + foo);"); + let expanded = + token_tree_to_syntax_node(&expanded, FragmentKind::Expr).unwrap().0.syntax_node(); + assert_eq!(expanded.to_string(), "map(x+foo)"); +} + pub(crate) fn create_rules(macro_definition: &str) -> MacroRules { let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); let macro_definition = -- cgit v1.2.3 From 6edc54a1e6a48f6fe3191c549befe91674342d9a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Dec 2019 16:17:22 +0100 Subject: Refactor macro tests --- crates/ra_mbe/src/syntax_bridge.rs | 14 +- crates/ra_mbe/src/tests.rs | 665 +++++++++++++++---------------------- 2 files changed, 282 insertions(+), 397 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 2c60430d1..ea2cac069 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -476,7 +476,7 @@ impl<'a> TreeSink for TtTreeSink<'a> { #[cfg(test)] mod tests { use super::*; - use crate::tests::{create_rules, expand}; + use crate::tests::parse_macro; use ra_parser::TokenSource; use ra_syntax::{ algo::{insert_children, InsertPosition}, @@ -485,7 +485,7 @@ mod tests { #[test] fn convert_tt_token_source() { - let rules = create_rules( + let expansion = parse_macro( r#" macro_rules! literals { ($i:ident) => { @@ -498,8 +498,8 @@ mod tests { } } "#, - ); - let expansion = expand(&rules, "literals!(foo);"); + ) + .expand_tt("literals!(foo);"); let tts = &[expansion.into()]; let buffer = tt::buffer::TokenBuffer::new(tts); let mut tt_src = SubtreeTokenSource::new(&buffer); @@ -527,7 +527,7 @@ mod tests { #[test] fn stmts_token_trees_to_expr_is_err() { - let rules = create_rules( + let expansion = parse_macro( r#" macro_rules! stmts { () => { @@ -538,8 +538,8 @@ mod tests { } } "#, - ); - let expansion = expand(&rules, "stmts!();"); + ) + .expand_tt("stmts!();"); assert!(token_tree_to_syntax_node(&expansion, FragmentKind::Expr).is_err()); } diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index b5a10a9d2..e640d115b 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -1,5 +1,7 @@ +use std::fmt::Write; + use ra_parser::FragmentKind; -use ra_syntax::{ast, AstNode, NodeOrToken, WalkEvent}; +use ra_syntax::{ast, AstNode, NodeOrToken, SyntaxKind::IDENT, SyntaxNode, WalkEvent, T}; use test_utils::assert_eq_text; use super::*; @@ -61,13 +63,14 @@ mod rule_parsing { #[test] fn test_token_id_shift() { - let macro_definition = r#" + let expansion = parse_macro( + r#" macro_rules! foobar { ($e:ident) => { foo bar $e } } -"#; - let rules = create_rules(macro_definition); - let expansion = expand(&rules, "foobar!(baz);"); +"#, + ) + .expand_tt("foobar!(baz);"); fn get_id(t: &tt::TokenTree) -> Option { if let tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) = t { @@ -90,22 +93,23 @@ macro_rules! foobar { #[test] fn test_token_map() { - use ra_parser::SyntaxKind::*; - use ra_syntax::T; - - let macro_definition = r#" + let expanded = parse_macro( + r#" macro_rules! foobar { ($e:ident) => { fn $e() {} } } -"#; - let rules = create_rules(macro_definition); - let (expansion, (token_map, content)) = expand_and_map(&rules, "foobar!(baz);"); +"#, + ) + .expand_tt("foobar!(baz);"); + + let (node, token_map) = token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap(); + let content = node.syntax_node().to_string(); let get_text = |id, kind| -> String { content[token_map.range_by_token(id).unwrap().by_kind(kind).unwrap()].to_string() }; - assert_eq!(expansion.token_trees.len(), 4); + assert_eq!(expanded.token_trees.len(), 4); // {($e:ident) => { fn $e() {} }} // 012345 67 8 9 T12 3 @@ -116,7 +120,7 @@ macro_rules! foobar { #[test] fn test_convert_tt() { - let macro_definition = r#" + parse_macro(r#" macro_rules! impl_froms { ($e:ident: $($v:ident),*) => { $( @@ -128,24 +132,17 @@ macro_rules! impl_froms { )* } } -"#; - - let macro_invocation = r#" -impl_froms!(TokenTree: Leaf, Subtree); -"#; - - let rules = create_rules(macro_definition); - let expansion = expand(&rules, macro_invocation); - assert_eq!( - expansion.to_string(), - "impl From for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree ::Leaf (it)}} \ - impl From for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree ::Subtree (it)}}" - ) +"#) + .assert_expand_tt( + "impl_froms!(TokenTree: Leaf, Subtree);", + "impl From for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree ::Leaf (it)}} \ + impl From for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree ::Subtree (it)}}" + ); } #[test] fn test_expr_order() { - let rules = create_rules( + let expanded = parse_macro( r#" macro_rules! foo { ($ i:expr) => { @@ -153,11 +150,10 @@ fn test_expr_order() { } } "#, - ); - let expanded = expand(&rules, "foo! { 1 + 1}"); - let tree = token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node(); + ) + .expand_items("foo! { 1 + 1}"); - let dump = format!("{:#?}", tree); + let dump = format!("{:#?}", expanded); assert_eq_text!( dump.trim(), r#"MACRO_ITEMS@[0; 15) @@ -189,7 +185,7 @@ fn test_expr_order() { #[test] fn test_fail_match_pattern_by_first_token() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:ident) => ( @@ -203,16 +199,15 @@ fn test_fail_match_pattern_by_first_token() { ) } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "mod foo {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { = bar }", "fn bar () {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { + Baz }", "struct Baz ;"); + ) + .assert_expand_items("foo! { foo }", "mod foo {}") + .assert_expand_items("foo! { = bar }", "fn bar () {}") + .assert_expand_items("foo! { + Baz }", "struct Baz ;"); } #[test] fn test_fail_match_pattern_by_last_token() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:ident) => ( @@ -226,16 +221,15 @@ fn test_fail_match_pattern_by_last_token() { ) } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "mod foo {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { bar = }", "fn bar () {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { Baz + }", "struct Baz ;"); + ) + .assert_expand_items("foo! { foo }", "mod foo {}") + .assert_expand_items("foo! { bar = }", "fn bar () {}") + .assert_expand_items("foo! { Baz + }", "struct Baz ;"); } #[test] fn test_fail_match_pattern_by_word_token() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:ident) => ( @@ -249,16 +243,15 @@ fn test_fail_match_pattern_by_word_token() { ) } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "mod foo {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { spam bar }", "fn bar () {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { eggs Baz }", "struct Baz ;"); + ) + .assert_expand_items("foo! { foo }", "mod foo {}") + .assert_expand_items("foo! { spam bar }", "fn bar () {}") + .assert_expand_items("foo! { eggs Baz }", "struct Baz ;"); } #[test] fn test_match_group_pattern_by_separator_token() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ ($ i:ident),*) => ($ ( @@ -273,16 +266,15 @@ fn test_match_group_pattern_by_separator_token() { ) } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! { foo, bar }", "mod foo {} mod bar {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { foo# bar }", "fn foo () {} fn bar () {}"); - assert_expansion(MacroKind::Items, &rules, "foo! { Foo,# Bar }", "struct Foo ; struct Bar ;"); + ) + .assert_expand_items("foo! { foo, bar }", "mod foo {} mod bar {}") + .assert_expand_items("foo! { foo# bar }", "fn foo () {} fn bar () {}") + .assert_expand_items("foo! { Foo,# Bar }", "struct Foo ; struct Bar ;"); } #[test] fn test_match_group_pattern_with_multiple_defs() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ ($ i:ident),*) => ( struct Bar { $ ( @@ -290,19 +282,13 @@ fn test_match_group_pattern_with_multiple_defs() { )*} ); } "#, - ); - - assert_expansion( - MacroKind::Items, - &rules, - "foo! { foo, bar }", - "struct Bar {fn foo {} fn bar {}}", - ); + ) + .assert_expand_items("foo! { foo, bar }", "struct Bar {fn foo {} fn bar {}}"); } #[test] fn test_match_group_pattern_with_multiple_statement() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ ($ i:ident),*) => ( fn baz { $ ( @@ -310,14 +296,13 @@ fn test_match_group_pattern_with_multiple_statement() { )*} ); } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! { foo, bar }", "fn baz {foo () ; bar () ;}"); + ) + .assert_expand_items("foo! { foo, bar }", "fn baz {foo () ; bar () ;}"); } #[test] fn test_match_group_pattern_with_multiple_statement_without_semi() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ ($ i:ident),*) => ( fn baz { $ ( @@ -325,14 +310,13 @@ fn test_match_group_pattern_with_multiple_statement_without_semi() { );*} ); } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! { foo, bar }", "fn baz {foo () ;bar ()}"); + ) + .assert_expand_items("foo! { foo, bar }", "fn baz {foo () ;bar ()}"); } #[test] fn test_match_group_empty_fixed_token() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ ($ i:ident)* #abc) => ( fn baz { $ ( @@ -340,69 +324,59 @@ fn test_match_group_empty_fixed_token() { )*} ); } "#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! {#abc}", "fn baz {}"); + ) + .assert_expand_items("foo! {#abc}", "fn baz {}"); } #[test] fn test_match_group_in_subtree() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { (fn $name:ident {$($i:ident)*} ) => ( fn $name() { $ ( $ i (); )*} ); }"#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! {fn baz {a b} }", "fn baz () {a () ; b () ;}"); + ) + .assert_expand_items("foo! {fn baz {a b} }", "fn baz () {a () ; b () ;}"); } #[test] fn test_match_group_with_multichar_sep() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { (fn $name:ident {$($i:literal)*} ) => ( fn $name() -> bool { $($i)&&*} ); }"#, - ); - - assert_expansion( - MacroKind::Items, - &rules, - "foo! (fn baz {true true} );", - "fn baz () -> bool {true &&true}", - ); + ) + .assert_expand_items("foo! (fn baz {true true} );", "fn baz () -> bool {true &&true}"); } #[test] fn test_match_group_zero_match() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ( $($i:ident)* ) => (); }"#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! ();", ""); + ) + .assert_expand_items("foo! ();", ""); } #[test] fn test_match_group_in_group() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { { $( ( $($i:ident)* ) )* } => ( $( ( $($i)* ) )* ); }"#, - ); - - assert_expansion(MacroKind::Items, &rules, "foo! ( (a b) );", "(a b)"); + ) + .assert_expand_items("foo! ( (a b) );", "(a b)"); } #[test] fn test_expand_to_item_list() { - let rules = create_rules( + let tree = parse_macro( " macro_rules! structs { ($($i:ident),*) => { @@ -410,9 +384,8 @@ fn test_expand_to_item_list() { } } ", - ); - let expansion = expand(&rules, "structs!(Foo, Bar);"); - let tree = token_tree_to_syntax_node(&expansion, FragmentKind::Items).unwrap().0.syntax_node(); + ) + .expand_items("structs!(Foo, Bar);"); assert_eq!( format!("{:#?}", tree).trim(), r#" @@ -469,7 +442,7 @@ fn test_expand_literals_to_token_tree() { unreachable!("It is not a literal"); } - let rules = create_rules( + let expansion = parse_macro( r#" macro_rules! literals { ($i:ident) => { @@ -482,8 +455,8 @@ fn test_expand_literals_to_token_tree() { } } "#, - ); - let expansion = expand(&rules, "literals!(foo);"); + ) + .expand_tt("literals!(foo);"); let stm_tokens = &to_subtree(&expansion.token_trees[0]).token_trees; // [let] [a] [=] ['c'] [;] @@ -498,7 +471,7 @@ fn test_expand_literals_to_token_tree() { #[test] fn test_two_idents() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:ident, $ j:ident) => { @@ -506,18 +479,13 @@ fn test_two_idents() { } } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, - "foo! { foo, bar }", - "fn foo () {let a = foo ; let b = bar ;}", - ); + ) + .assert_expand_items("foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); } #[test] fn test_tt_to_stmts() { - let rules = create_rules( + let stmts = parse_macro( r#" macro_rules! foo { () => { @@ -527,11 +495,8 @@ fn test_tt_to_stmts() { } } "#, - ); - - let expanded = expand(&rules, "foo!{}"); - let stmts = - token_tree_to_syntax_node(&expanded, FragmentKind::Statements).unwrap().0.syntax_node(); + ) + .expand_statements("foo!{}"); assert_eq!( format!("{:#?}", stmts).trim(), @@ -571,7 +536,7 @@ fn test_tt_to_stmts() { #[test] fn test_match_literal() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ('(') => { @@ -579,8 +544,8 @@ fn test_match_literal() { } } "#, - ); - assert_expansion(MacroKind::Items, &rules, "foo! ['('];", "fn foo () {}"); + ) + .assert_expand_items("foo! ['('];", "fn foo () {}"); } // The following tests are port from intellij-rust directly @@ -588,7 +553,7 @@ fn test_match_literal() { #[test] fn test_path() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:path) => { @@ -596,11 +561,9 @@ fn test_path() { } } "#, - ); - assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "fn foo () {let a = foo ;}"); - assert_expansion( - MacroKind::Items, - &rules, + ) + .assert_expand_items("foo! { foo }", "fn foo () {let a = foo ;}") + .assert_expand_items( "foo! { bar::::baz:: }", "fn foo () {let a = bar ::< u8 >:: baz ::< u8 > ;}", ); @@ -608,7 +571,7 @@ fn test_path() { #[test] fn test_two_paths() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:path, $ j:path) => { @@ -616,18 +579,13 @@ fn test_two_paths() { } } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, - "foo! { foo, bar }", - "fn foo () {let a = foo ; let b = bar ;}", - ); + ) + .assert_expand_items("foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); } #[test] fn test_path_with_path() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:path) => { @@ -635,13 +593,13 @@ fn test_path_with_path() { } } "#, - ); - assert_expansion(MacroKind::Items, &rules, "foo! { foo }", "fn foo () {let a = foo :: bar ;}"); + ) + .assert_expand_items("foo! { foo }", "fn foo () {let a = foo :: bar ;}"); } #[test] fn test_expr() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:expr) => { @@ -649,11 +607,8 @@ fn test_expr() { } } "#, - ); - - assert_expansion( - MacroKind::Items, - &rules, + ) + .assert_expand_items( "foo! { 2 + 2 * baz(3).quux() }", "fn bar () {2 + 2 * baz (3) . quux () ;}", ); @@ -661,7 +616,7 @@ fn test_expr() { #[test] fn test_last_expr() { - let rules = create_rules( + parse_macro( r#" macro_rules! vec { ($($item:expr),*) => { @@ -675,10 +630,8 @@ fn test_last_expr() { }; } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, + ) + .assert_expand_items( "vec!(1,2,3);", "{let mut v = Vec :: new () ; v . push (1) ; v . push (2) ; v . push (3) ; v}", ); @@ -686,7 +639,7 @@ fn test_last_expr() { #[test] fn test_ty() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:ty) => ( @@ -694,18 +647,13 @@ fn test_ty() { ) } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, - "foo! { Baz }", - "fn bar () -> Baz < u8 > {unimplemented ! ()}", - ); + ) + .assert_expand_items("foo! { Baz }", "fn bar () -> Baz < u8 > {unimplemented ! ()}"); } #[test] fn test_ty_with_complex_type() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:ty) => ( @@ -713,20 +661,14 @@ fn test_ty_with_complex_type() { ) } "#, - ); - + ) // Reference lifetime struct with generic type - assert_expansion( - MacroKind::Items, - &rules, + .assert_expand_items( "foo! { &'a Baz }", "fn bar () -> & 'a Baz < u8 > {unimplemented ! ()}", - ); - + ) // extern "Rust" func type - assert_expansion( - MacroKind::Items, - &rules, + .assert_expand_items( r#"foo! { extern "Rust" fn() -> Ret }"#, r#"fn bar () -> extern "Rust" fn () -> Ret {unimplemented ! ()}"#, ); @@ -734,19 +676,19 @@ fn test_ty_with_complex_type() { #[test] fn test_pat_() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:pat) => { fn foo() { let $ i; } } } "#, - ); - assert_expansion(MacroKind::Items, &rules, "foo! { (a, b) }", "fn foo () {let (a , b) ;}"); + ) + .assert_expand_items("foo! { (a, b) }", "fn foo () {let (a , b) ;}"); } #[test] fn test_stmt() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:stmt) => ( @@ -754,14 +696,14 @@ fn test_stmt() { ) } "#, - ); - assert_expansion(MacroKind::Items, &rules, "foo! { 2 }", "fn bar () {2 ;}"); - assert_expansion(MacroKind::Items, &rules, "foo! { let a = 0 }", "fn bar () {let a = 0 ;}"); + ) + .assert_expand_items("foo! { 2 }", "fn bar () {2 ;}") + .assert_expand_items("foo! { let a = 0 }", "fn bar () {let a = 0 ;}"); } #[test] fn test_single_item() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:item) => ( @@ -769,13 +711,13 @@ fn test_single_item() { ) } "#, - ); - assert_expansion(MacroKind::Items, &rules, "foo! {mod c {}}", "mod c {}"); + ) + .assert_expand_items("foo! {mod c {}}", "mod c {}"); } #[test] fn test_all_items() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ ($ i:item)*) => ($ ( @@ -783,10 +725,8 @@ fn test_all_items() { )*) } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, + ). + assert_expand_items( r#" foo! { extern crate a; @@ -810,19 +750,19 @@ fn test_all_items() { #[test] fn test_block() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:block) => { fn foo() $ i } } "#, - ); - assert_expansion(MacroKind::Stmts, &rules, "foo! { { 1; } }", "fn foo () {1 ;}"); + ) + .assert_expand_statements("foo! { { 1; } }", "fn foo () {1 ;}"); } #[test] fn test_meta() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:meta) => ( @@ -831,10 +771,8 @@ fn test_meta() { ) } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, + ) + .assert_expand_items( r#"foo! { cfg(target_os = "windows") }"#, r#"# [cfg (target_os = "windows")] fn bar () {}"#, ); @@ -842,7 +780,7 @@ fn test_meta() { #[test] fn test_meta_doc_comments() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($(#[$ i:meta])+) => ( @@ -851,10 +789,8 @@ fn test_meta_doc_comments() { ) } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, + ). + assert_expand_items( r#"foo! { /// Single Line Doc 1 /** @@ -867,69 +803,68 @@ fn test_meta_doc_comments() { #[test] fn test_tt_block() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ i:tt) => { fn foo() $ i } } "#, - ); - assert_expansion(MacroKind::Items, &rules, r#"foo! { { 1; } }"#, r#"fn foo () {1 ;}"#); + ) + .assert_expand_items(r#"foo! { { 1; } }"#, r#"fn foo () {1 ;}"#); } #[test] fn test_tt_group() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($($ i:tt)*) => { $($ i)* } } "#, - ); - assert_expansion(MacroKind::Items, &rules, r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#); + ) + .assert_expand_items(r#"foo! { fn foo() {} }"#, r#"fn foo () {}"#); } #[test] fn test_lifetime() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ lt:lifetime) => { struct Ref<$ lt>{ s: &$ lt str } } } "#, - ); - assert_expansion(MacroKind::Items, &rules, r#"foo!{'a}"#, r#"struct Ref <'a > {s : &'a str}"#); + ) + .assert_expand_items(r#"foo!{'a}"#, r#"struct Ref <'a > {s : &'a str}"#); } #[test] fn test_literal() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ type:ty $ lit:literal) => { const VALUE: $ type = $ lit;}; } "#, - ); - assert_expansion(MacroKind::Items, &rules, r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#); + ) + .assert_expand_items(r#"foo!(u8 0);"#, r#"const VALUE : u8 = 0 ;"#); } #[test] fn test_vis() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($ vis:vis $ name:ident) => { $ vis fn $ name() {}}; } "#, - ); - assert_expansion(MacroKind::Items, &rules, r#"foo!(pub foo);"#, r#"pub fn foo () {}"#); - - // test optional casse - assert_expansion(MacroKind::Items, &rules, r#"foo!(foo);"#, r#"fn foo () {}"#); + ) + .assert_expand_items(r#"foo!(pub foo);"#, r#"pub fn foo () {}"#) + // test optional cases + .assert_expand_items(r#"foo!(foo);"#, r#"fn foo () {}"#); } #[test] fn test_inner_macro_rules() { - let rules = create_rules( + parse_macro( r#" macro_rules! foo { ($a:ident, $b:ident, $c:tt) => { @@ -945,10 +880,8 @@ macro_rules! foo { } } "#, - ); - assert_expansion( - MacroKind::Items, - &rules, + ). + assert_expand_items( r#"foo!(x,y, 1);"#, r#"macro_rules ! bar {($ bi : ident) => {fn $ bi () -> u8 {1}}} bar ! (x) ; fn y () -> u8 {1}"#, ); @@ -957,7 +890,7 @@ macro_rules! foo { // The following tests are based on real world situations #[test] fn test_vec() { - let rules = create_rules( + let fixture = parse_macro( r#" macro_rules! vec { ($($item:expr),*) => { @@ -972,16 +905,14 @@ fn test_vec() { } "#, ); - assert_expansion(MacroKind::Items, &rules, r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#); - assert_expansion( - MacroKind::Items, - &rules, - r#"vec![1u32,2];"#, - r#"{let mut v = Vec :: new () ; v . push (1u32) ; v . push (2) ; v}"#, - ); + fixture + .assert_expand_items(r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#) + .assert_expand_items( + r#"vec![1u32,2];"#, + r#"{let mut v = Vec :: new () ; v . push (1u32) ; v . push (2) ; v}"#, + ); - let expansion = expand(&rules, r#"vec![1u32,2];"#); - let tree = token_tree_to_syntax_node(&expansion, FragmentKind::Expr).unwrap().0.syntax_node(); + let tree = fixture.expand_expr(r#"vec![1u32,2];"#); assert_eq!( format!("{:#?}", tree).trim(), @@ -1055,7 +986,7 @@ fn test_vec() { fn test_winapi_struct() { // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/macros.rs#L366 - let rules = create_rules( + parse_macro( r#" macro_rules! STRUCT { ($(#[$attrs:meta])* struct $name:ident { @@ -1077,17 +1008,19 @@ macro_rules! STRUCT { ); } "#, - ); + ). // from https://github.com/retep998/winapi-rs/blob/a7ef2bca086aae76cf6c4ce4c2552988ed9798ad/src/shared/d3d9caps.rs - assert_expansion(MacroKind::Items, &rules, r#"STRUCT!{struct D3DVSHADERCAPS2_0 {Caps: u8,}}"#, - "# [repr (C)] # [derive (Copy)] pub struct D3DVSHADERCAPS2_0 {pub Caps : u8 ,} impl Clone for D3DVSHADERCAPS2_0 {# [inline] fn clone (& self) -> D3DVSHADERCAPS2_0 {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DVSHADERCAPS2_0 {# [inline] fn default () -> D3DVSHADERCAPS2_0 {unsafe {$crate :: _core :: mem :: zeroed ()}}}"); - assert_expansion(MacroKind::Items, &rules, r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#, - "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}"); + assert_expand_items(r#"STRUCT!{struct D3DVSHADERCAPS2_0 {Caps: u8,}}"#, + "# [repr (C)] # [derive (Copy)] pub struct D3DVSHADERCAPS2_0 {pub Caps : u8 ,} impl Clone for D3DVSHADERCAPS2_0 {# [inline] fn clone (& self) -> D3DVSHADERCAPS2_0 {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DVSHADERCAPS2_0 {# [inline] fn default () -> D3DVSHADERCAPS2_0 {unsafe {$crate :: _core :: mem :: zeroed ()}}}" + ) + .assert_expand_items(r#"STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}"#, + "# [repr (C)] # [derive (Copy)] # [cfg_attr (target_arch = \"x86\" , repr (packed))] pub struct D3DCONTENTPROTECTIONCAPS {pub Caps : u8 ,} impl Clone for D3DCONTENTPROTECTIONCAPS {# [inline] fn clone (& self) -> D3DCONTENTPROTECTIONCAPS {* self}} # [cfg (feature = \"impl-default\")] impl Default for D3DCONTENTPROTECTIONCAPS {# [inline] fn default () -> D3DCONTENTPROTECTIONCAPS {unsafe {$crate :: _core :: mem :: zeroed ()}}}" + ); } #[test] fn test_int_base() { - let rules = create_rules( + parse_macro( r#" macro_rules! int_base { ($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => { @@ -1100,17 +1033,15 @@ macro_rules! int_base { } } "#, - ); - - assert_expansion(MacroKind::Items, &rules, r#" int_base!{Binary for isize as usize -> Binary}"#, + ).assert_expand_items(r#" int_base!{Binary for isize as usize -> Binary}"#, "# [stable (feature = \"rust1\" , since = \"1.0.0\")] impl fmt ::Binary for isize {fn fmt (& self , f : & mut fmt :: Formatter < \'_ >) -> fmt :: Result {Binary . fmt_int (* self as usize , f)}}" - ); + ); } #[test] fn test_generate_pattern_iterators() { // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/str/mod.rs - let rules = create_rules( + parse_macro( r#" macro_rules! generate_pattern_iterators { { double ended; with $(#[$common_stability_attribute:meta])*, @@ -1121,11 +1052,7 @@ macro_rules! generate_pattern_iterators { } } "#, - ); - - assert_expansion( - MacroKind::Items, - &rules, + ).assert_expand_items( r#"generate_pattern_iterators ! ( double ended ; with # [ stable ( feature = "rust1" , since = "1.0.0" ) ] , Split , RSplit , & 'a str );"#, "fn foo () {}", ); @@ -1134,7 +1061,7 @@ macro_rules! generate_pattern_iterators { #[test] fn test_impl_fn_for_zst() { // from https://github.com/rust-lang/rust/blob/5d20ff4d2718c820632b38c1e49d4de648a9810b/src/libcore/internal_macros.rs - let rules = create_rules( + parse_macro( r#" macro_rules! impl_fn_for_zst { { $( $( #[$attr: meta] )* @@ -1175,9 +1102,7 @@ $body: block; )+ } } "#, - ); - - assert_expansion(MacroKind::Items, &rules, r#" + ).assert_expand_items(r#" impl_fn_for_zst ! { # [ derive ( Clone ) ] struct CharEscapeDebugContinue impl Fn = | c : char | -> char :: EscapeDebug { @@ -1194,13 +1119,14 @@ impl_fn_for_zst ! { } ; } "#, - "# [derive (Clone)] struct CharEscapeDebugContinue ; impl Fn < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDebug {{c . escape_debug_ext (false)}}} impl FnMut < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDebugContinue {type Output = char :: EscapeDebug ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeUnicode ; impl Fn < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeUnicode {{c . escape_unicode ()}}} impl FnMut < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeUnicode {type Output = char :: EscapeUnicode ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeDefault ; impl Fn < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDefault {{c . escape_default ()}}} impl FnMut < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDefault {type Output = char :: EscapeDefault ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (& self , (c ,))}}"); + "# [derive (Clone)] struct CharEscapeDebugContinue ; impl Fn < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDebug {{c . escape_debug_ext (false)}}} impl FnMut < (char ,) > for CharEscapeDebugContinue {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDebugContinue {type Output = char :: EscapeDebug ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDebug {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeUnicode ; impl Fn < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeUnicode {{c . escape_unicode ()}}} impl FnMut < (char ,) > for CharEscapeUnicode {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeUnicode {type Output = char :: EscapeUnicode ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeUnicode {Fn :: call (& self , (c ,))}} # [derive (Clone)] struct CharEscapeDefault ; impl Fn < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call (& self , (c ,) : (char ,)) -> char :: EscapeDefault {{c . escape_default ()}}} impl FnMut < (char ,) > for CharEscapeDefault {# [inline] extern \"rust-call\" fn call_mut (& mut self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (&* self , (c ,))}} impl FnOnce < (char ,) > for CharEscapeDefault {type Output = char :: EscapeDefault ; # [inline] extern \"rust-call\" fn call_once (self , (c ,) : (char ,)) -> char :: EscapeDefault {Fn :: call (& self , (c ,))}}" + ); } #[test] fn test_impl_nonzero_fmt() { // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/num/mod.rs#L12 - let rules = create_rules( + parse_macro( r#" macro_rules! impl_nonzero_fmt { ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => { @@ -1208,11 +1134,7 @@ fn test_impl_nonzero_fmt() { } } "#, - ); - - assert_expansion( - MacroKind::Items, - &rules, + ).assert_expand_items( r#"impl_nonzero_fmt! { # [stable(feature= "nonzero",since="1.28.0")] (Debug,Display,Binary,Octal,LowerHex,UpperHex) for NonZeroU8}"#, "fn foo () {}", ); @@ -1221,7 +1143,7 @@ fn test_impl_nonzero_fmt() { #[test] fn test_cfg_if_items() { // from https://github.com/rust-lang/rust/blob/33fe1131cadba69d317156847be9a402b89f11bb/src/libstd/macros.rs#L986 - let rules = create_rules( + parse_macro( r#" macro_rules! __cfg_if_items { (($($not:meta,)*) ; ) => {}; @@ -1230,11 +1152,7 @@ fn test_cfg_if_items() { } } "#, - ); - - assert_expansion( - MacroKind::Items, - &rules, + ).assert_expand_items( r#"__cfg_if_items ! { ( rustdoc , ) ; ( ( ) ( # [ cfg ( any ( target_os = "redox" , unix ) ) ] # [ stable ( feature = "rust1" , since = "1.0.0" ) ] pub use sys :: ext as unix ; # [ cfg ( windows ) ] # [ stable ( feature = "rust1" , since = "1.0.0" ) ] pub use sys :: ext as windows ; # [ cfg ( any ( target_os = "linux" , target_os = "l4re" ) ) ] pub mod linux ; ) ) , }"#, "__cfg_if_items ! {(rustdoc ,) ;}", ); @@ -1243,7 +1161,7 @@ fn test_cfg_if_items() { #[test] fn test_cfg_if_main() { // from https://github.com/rust-lang/rust/blob/3d211248393686e0f73851fc7548f6605220fbe1/src/libpanic_unwind/macros.rs#L9 - let rules = create_rules( + parse_macro( r#" macro_rules! cfg_if { ($( @@ -1264,9 +1182,7 @@ fn test_cfg_if_main() { }; } "#, - ); - - assert_expansion(MacroKind::Items, &rules, r#" + ).assert_expand_items(r#" cfg_if ! { if # [ cfg ( target_env = "msvc" ) ] { // no extra unwinder support needed @@ -1278,11 +1194,8 @@ cfg_if ! { } } "#, - "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}"); - - assert_expansion( - MacroKind::Items, - &rules, + "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}" + ).assert_expand_items( r#" cfg_if ! { @ __apply cfg ( all ( not ( any ( not ( any ( target_os = "solaris" , target_os = "illumos" ) ) ) ) ) ) , } "#, @@ -1293,7 +1206,7 @@ cfg_if ! { @ __apply cfg ( all ( not ( any ( not ( any ( target_os = "solaris" , #[test] fn test_proptest_arbitrary() { // from https://github.com/AltSysrq/proptest/blob/d1c4b049337d2f75dd6f49a095115f7c532e5129/proptest/src/arbitrary/macros.rs#L16 - let rules = create_rules( + parse_macro( r#" macro_rules! arbitrary { ([$($bounds : tt)*] $typ: ty, $strat: ty, $params: ty; @@ -1308,22 +1221,21 @@ macro_rules! arbitrary { }; }"#, - ); - - assert_expansion(MacroKind::Items, &rules, r#"arbitrary ! ( [ A : Arbitrary ] + ).assert_expand_items(r#"arbitrary ! ( [ A : Arbitrary ] Vec < A > , VecStrategy < A :: Strategy > , RangedParams1 < A :: Parameters > ; args => { let product_unpack ! [ range , a ] = args ; vec ( any_with :: < A > ( a ) , range ) } ) ;"#, - "impl $crate :: arbitrary :: Arbitrary for Vec < A > {type Parameters = RangedParams1 < A :: Parameters > ; type Strategy = VecStrategy < A :: Strategy > ; fn arbitrary_with (args : Self :: Parameters) -> Self :: Strategy {{let product_unpack ! [range , a] = args ; vec (any_with :: < A > (a) , range)}}}"); + "impl $crate :: arbitrary :: Arbitrary for Vec < A > {type Parameters = RangedParams1 < A :: Parameters > ; type Strategy = VecStrategy < A :: Strategy > ; fn arbitrary_with (args : Self :: Parameters) -> Self :: Strategy {{let product_unpack ! [range , a] = args ; vec (any_with :: < A > (a) , range)}}}" + ); } #[test] fn test_old_ridl() { // This is from winapi 2.8, which do not have a link from github // - let rules = create_rules( + let expanded = parse_macro( r#" #[macro_export] macro_rules! RIDL { @@ -1339,21 +1251,17 @@ macro_rules! RIDL { } }; }"#, - ); + ).expand_tt(r#" + RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) { + fn GetDataSize(&mut self) -> UINT + }}"#); - let expanded = expand( - &rules, - r#" -RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) { - fn GetDataSize(&mut self) -> UINT -}}"#, - ); assert_eq!(expanded.to_string(), "impl ID3D11Asynchronous {pub unsafe fn GetDataSize (& mut self) -> UINT {((* self . lpVtbl) .GetDataSize) (self)}}"); } #[test] fn test_quick_error() { - let rules = create_rules( + let expanded = parse_macro( r#" macro_rules! quick_error { @@ -1376,10 +1284,8 @@ macro_rules! quick_error { } "#, - ); - - let expanded = expand( - &rules, + ) + .expand_tt( r#" quick_error ! (SORT [enum Wrapped # [derive (Debug)]] items [ => One : UNIT [] {} @@ -1393,7 +1299,7 @@ quick_error ! (SORT [enum Wrapped # [derive (Debug)]] items [ #[test] fn test_empty_repeat_vars_in_empty_repeat_vars() { - let rules = create_rules( + parse_macro( r#" macro_rules! delegate_impl { ([$self_type:ident, $self_wrap:ty, $self_map:ident] @@ -1440,11 +1346,7 @@ macro_rules! delegate_impl { } } "#, - ); - - assert_expansion( - MacroKind::Items, - &rules, + ).assert_expand_items( r#"delegate_impl ! {[G , & 'a mut G , deref] pub trait Data : GraphBase {@ section type type NodeWeight ;}}"#, "impl <> Data for & \'a mut G where G : Data {}", ); @@ -1452,7 +1354,7 @@ macro_rules! delegate_impl { #[test] fn expr_interpolation() { - let rules = create_rules( + let expanded = parse_macro( r#" macro_rules! id { ($expr:expr) => { @@ -1460,118 +1362,101 @@ fn expr_interpolation() { } } "#, - ); + ) + .expand_expr("id!(x + foo);"); - let expanded = expand(&rules, "id!(x + foo);"); - let expanded = - token_tree_to_syntax_node(&expanded, FragmentKind::Expr).unwrap().0.syntax_node(); assert_eq!(expanded.to_string(), "map(x+foo)"); } -pub(crate) fn create_rules(macro_definition: &str) -> MacroRules { - let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); - let macro_definition = - source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - - let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); - crate::MacroRules::parse(&definition_tt).unwrap() +pub(crate) struct MacroFixture { + rules: MacroRules, } -pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree { - let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); - let macro_invocation = - source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - - let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); +impl MacroFixture { + pub(crate) fn expand_tt(&self, invocation: &str) -> tt::Subtree { + let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); + let macro_invocation = + source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - rules.expand(&invocation_tt).unwrap() -} + let (invocation_tt, _) = + ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); -pub(crate) fn expand_and_map( - rules: &MacroRules, - invocation: &str, -) -> (tt::Subtree, (TokenMap, String)) { - let source_file = ast::SourceFile::parse(invocation).ok().unwrap(); - let macro_invocation = - source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + self.rules.expand(&invocation_tt).unwrap() + } - let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); - let expanded = rules.expand(&invocation_tt).unwrap(); + fn expand_items(&self, invocation: &str) -> SyntaxNode { + let expanded = self.expand_tt(invocation); + token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node() + } - let (node, expanded_token_tree) = - token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap(); + fn expand_statements(&self, invocation: &str) -> SyntaxNode { + let expanded = self.expand_tt(invocation); + token_tree_to_syntax_node(&expanded, FragmentKind::Statements).unwrap().0.syntax_node() + } - (expanded, (expanded_token_tree, node.syntax_node().to_string())) -} + fn expand_expr(&self, invocation: &str) -> SyntaxNode { + let expanded = self.expand_tt(invocation); + token_tree_to_syntax_node(&expanded, FragmentKind::Expr).unwrap().0.syntax_node() + } -pub(crate) enum MacroKind { - Items, - Stmts, -} + fn assert_expand_tt(&self, invocation: &str, expected: &str) { + let expansion = self.expand_tt(invocation); + assert_eq!(expansion.to_string(), expected); + } -pub(crate) fn assert_expansion( - kind: MacroKind, - rules: &MacroRules, - invocation: &str, - expected: &str, -) -> tt::Subtree { - let expanded = expand(rules, invocation); - assert_eq!(expanded.to_string(), expected); + fn assert_expand_items(&self, invocation: &str, expected: &str) -> &MacroFixture { + self.assert_expansion(FragmentKind::Items, invocation, expected); + self + } - let expected = expected.replace("$crate", "C_C__C"); + fn assert_expand_statements(&self, invocation: &str, expected: &str) -> &MacroFixture { + self.assert_expansion(FragmentKind::Statements, invocation, expected); + self + } - // wrap the given text to a macro call - let expected = { - let wrapped = format!("wrap_macro!( {} )", expected); - let wrapped = ast::SourceFile::parse(&wrapped); - let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; - wrapped.delimiter = None; - wrapped - }; - let (expanded_tree, expected_tree) = match kind { - MacroKind::Items => { - let expanded_tree = - token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node(); - let expected_tree = - token_tree_to_syntax_node(&expected, FragmentKind::Items).unwrap().0.syntax_node(); - - ( - debug_dump_ignore_spaces(&expanded_tree).trim().to_string(), - debug_dump_ignore_spaces(&expected_tree).trim().to_string(), - ) - } + fn assert_expansion(&self, kind: FragmentKind, invocation: &str, expected: &str) { + let expanded = self.expand_tt(invocation); + assert_eq!(expanded.to_string(), expected); + + let expected = expected.replace("$crate", "C_C__C"); + + // wrap the given text to a macro call + let expected = { + let wrapped = format!("wrap_macro!( {} )", expected); + let wrapped = ast::SourceFile::parse(&wrapped); + let wrapped = + wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); + let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; + wrapped.delimiter = None; + wrapped + }; - MacroKind::Stmts => { - let expanded_tree = token_tree_to_syntax_node(&expanded, FragmentKind::Statements) - .unwrap() - .0 - .syntax_node(); - let expected_tree = token_tree_to_syntax_node(&expected, FragmentKind::Statements) - .unwrap() - .0 - .syntax_node(); - - ( - debug_dump_ignore_spaces(&expanded_tree).trim().to_string(), - debug_dump_ignore_spaces(&expected_tree).trim().to_string(), - ) - } - }; + let expanded_tree = token_tree_to_syntax_node(&expanded, kind).unwrap().0.syntax_node(); + let expanded_tree = debug_dump_ignore_spaces(&expanded_tree).trim().to_string(); - let expected_tree = expected_tree.replace("C_C__C", "$crate"); - assert_eq!( - expanded_tree, expected_tree, - "\nleft:\n{}\nright:\n{}", - expanded_tree, expected_tree, - ); + let expected_tree = token_tree_to_syntax_node(&expected, kind).unwrap().0.syntax_node(); + let expected_tree = debug_dump_ignore_spaces(&expected_tree).trim().to_string(); - expanded + let expected_tree = expected_tree.replace("C_C__C", "$crate"); + assert_eq!( + expanded_tree, expected_tree, + "\nleft:\n{}\nright:\n{}", + expanded_tree, expected_tree, + ); + } } -pub fn debug_dump_ignore_spaces(node: &ra_syntax::SyntaxNode) -> String { - use std::fmt::Write; +pub(crate) fn parse_macro(macro_definition: &str) -> MacroFixture { + let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap(); + let macro_definition = + source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); + + let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); + let rules = MacroRules::parse(&definition_tt).unwrap(); + MacroFixture { rules } +} +fn debug_dump_ignore_spaces(node: &ra_syntax::SyntaxNode) -> String { let mut level = 0; let mut buf = String::new(); macro_rules! indent { -- cgit v1.2.3 From 0d5d63a80ea08f2af439bcc72fff9b24d144c70d Mon Sep 17 00:00:00 2001 From: kjeremy Date: Fri, 20 Dec 2019 15:14:30 -0500 Subject: Clippy lints --- crates/ra_mbe/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 45dad2d10..2c6ae5658 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -97,7 +97,9 @@ impl Shift { tt::Leaf::Literal(lit) => lit.id = self.shift(lit.id), }, tt::TokenTree::Subtree(tt) => { - tt.delimiter.as_mut().map(|it: &mut Delimiter| it.id = self.shift(it.id)); + if let Some(it) = tt.delimiter.as_mut() { + it.id = self.shift(it.id); + }; self.shift_all(tt) } } -- cgit v1.2.3