diff options
-rw-r--r-- | crates/ra_mbe/src/mbe_expander.rs | 6 | ||||
-rw-r--r-- | crates/ra_mbe/src/mbe_parser.rs | 3 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 11 | ||||
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 43 |
4 files changed, 27 insertions, 36 deletions
diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index c7c06c7fd..f185aecb7 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs | |||
@@ -599,7 +599,8 @@ mod tests { | |||
599 | let macro_definition = | 599 | let macro_definition = |
600 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 600 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
601 | 601 | ||
602 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | 602 | let (definition_tt, _) = |
603 | ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); | ||
603 | crate::MacroRules::parse(&definition_tt).unwrap() | 604 | crate::MacroRules::parse(&definition_tt).unwrap() |
604 | } | 605 | } |
605 | 606 | ||
@@ -611,7 +612,8 @@ mod tests { | |||
611 | let macro_invocation = | 612 | let macro_invocation = |
612 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 613 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
613 | 614 | ||
614 | let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | 615 | let (invocation_tt, _) = |
616 | ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); | ||
615 | 617 | ||
616 | expand_rule(&rules.rules[0], &invocation_tt) | 618 | expand_rule(&rules.rules[0], &invocation_tt) |
617 | } | 619 | } |
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index cddb4a7b4..954b84d9d 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs | |||
@@ -179,7 +179,8 @@ mod tests { | |||
179 | let macro_definition = | 179 | let macro_definition = |
180 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 180 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
181 | 181 | ||
182 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | 182 | let (definition_tt, _) = |
183 | ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); | ||
183 | parse(&definition_tt) | 184 | parse(&definition_tt) |
184 | } | 185 | } |
185 | 186 | ||
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index bfc351f81..64ed6a517 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -2,7 +2,7 @@ use crate::subtree_source::SubtreeTokenSource; | |||
2 | use crate::ExpandError; | 2 | use crate::ExpandError; |
3 | use ra_parser::{ParseError, TreeSink}; | 3 | use ra_parser::{ParseError, TreeSink}; |
4 | use ra_syntax::{ | 4 | use ra_syntax::{ |
5 | ast, AstNode, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode, | 5 | ast, AstNode, AstToken, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode, |
6 | SyntaxTreeBuilder, TextRange, TextUnit, T, | 6 | SyntaxTreeBuilder, TextRange, TextUnit, T, |
7 | }; | 7 | }; |
8 | use tt::buffer::{Cursor, TokenBuffer}; | 8 | use tt::buffer::{Cursor, TokenBuffer}; |
@@ -116,8 +116,6 @@ impl TokenMap { | |||
116 | /// and strips the ending `*/` | 116 | /// and strips the ending `*/` |
117 | /// And then quote the string, which is needed to convert to `tt::Literal` | 117 | /// And then quote the string, which is needed to convert to `tt::Literal` |
118 | fn doc_comment_text(comment: &ast::Comment) -> SmolStr { | 118 | fn doc_comment_text(comment: &ast::Comment) -> SmolStr { |
119 | use ast::AstToken; | ||
120 | |||
121 | let prefix_len = comment.prefix().len(); | 119 | let prefix_len = comment.prefix().len(); |
122 | let mut text = &comment.text()[prefix_len..]; | 120 | let mut text = &comment.text()[prefix_len..]; |
123 | 121 | ||
@@ -132,9 +130,8 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr { | |||
132 | text.into() | 130 | text.into() |
133 | } | 131 | } |
134 | 132 | ||
135 | fn convert_doc_comment<'a>(token: &ra_syntax::SyntaxToken<'a>) -> Option<Vec<tt::TokenTree>> { | 133 | fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTree>> { |
136 | use ast::AstToken; | 134 | let comment = ast::Comment::cast(token.clone())?; |
137 | let comment = ast::Comment::cast(*token)?; | ||
138 | let doc = comment.kind().doc?; | 135 | let doc = comment.kind().doc?; |
139 | 136 | ||
140 | // Make `doc="\" Comments\"" | 137 | // Make `doc="\" Comments\"" |
@@ -245,7 +242,7 @@ fn convert_tt( | |||
245 | } | 242 | } |
246 | } | 243 | } |
247 | SyntaxElement::Node(node) => { | 244 | SyntaxElement::Node(node) => { |
248 | let child = convert_tt(token_map, global_offset, node)?.into(); | 245 | let child = convert_tt(token_map, global_offset, &node)?.into(); |
249 | token_trees.push(child); | 246 | token_trees.push(child); |
250 | } | 247 | } |
251 | }; | 248 | }; |
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 419b2c099..38a31109d 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -37,8 +37,8 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
37 | let macro_invocation = | 37 | let macro_invocation = |
38 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 38 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
39 | 39 | ||
40 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | 40 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); |
41 | let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | 41 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); |
42 | let rules = crate::MacroRules::parse(&definition_tt).unwrap(); | 42 | let rules = crate::MacroRules::parse(&definition_tt).unwrap(); |
43 | let expansion = rules.expand(&invocation_tt).unwrap(); | 43 | let expansion = rules.expand(&invocation_tt).unwrap(); |
44 | assert_eq!( | 44 | assert_eq!( |
@@ -53,7 +53,7 @@ pub(crate) fn create_rules(macro_definition: &str) -> MacroRules { | |||
53 | let macro_definition = | 53 | let macro_definition = |
54 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 54 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
55 | 55 | ||
56 | let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap(); | 56 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); |
57 | crate::MacroRules::parse(&definition_tt).unwrap() | 57 | crate::MacroRules::parse(&definition_tt).unwrap() |
58 | } | 58 | } |
59 | 59 | ||
@@ -62,34 +62,25 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree { | |||
62 | let macro_invocation = | 62 | let macro_invocation = |
63 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 63 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
64 | 64 | ||
65 | let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); | 65 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); |
66 | 66 | ||
67 | rules.expand(&invocation_tt).unwrap() | 67 | rules.expand(&invocation_tt).unwrap() |
68 | } | 68 | } |
69 | 69 | ||
70 | pub(crate) fn expand_to_items( | 70 | pub(crate) fn expand_to_items(rules: &MacroRules, invocation: &str) -> ast::MacroItems { |
71 | rules: &MacroRules, | ||
72 | invocation: &str, | ||
73 | ) -> ra_syntax::TreeArc<ast::MacroItems> { | ||
74 | let expanded = expand(rules, invocation); | 71 | let expanded = expand(rules, invocation); |
75 | token_tree_to_macro_items(&expanded).unwrap().tree().to_owned() | 72 | token_tree_to_macro_items(&expanded).unwrap().tree() |
76 | } | 73 | } |
77 | 74 | ||
78 | #[allow(unused)] | 75 | #[allow(unused)] |
79 | pub(crate) fn expand_to_stmts( | 76 | pub(crate) fn expand_to_stmts(rules: &MacroRules, invocation: &str) -> ast::MacroStmts { |
80 | rules: &MacroRules, | ||
81 | invocation: &str, | ||
82 | ) -> ra_syntax::TreeArc<ast::MacroStmts> { | ||
83 | let expanded = expand(rules, invocation); | 77 | let expanded = expand(rules, invocation); |
84 | token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned() | 78 | token_tree_to_macro_stmts(&expanded).unwrap().tree() |
85 | } | 79 | } |
86 | 80 | ||
87 | pub(crate) fn expand_to_expr( | 81 | pub(crate) fn expand_to_expr(rules: &MacroRules, invocation: &str) -> ast::Expr { |
88 | rules: &MacroRules, | ||
89 | invocation: &str, | ||
90 | ) -> ra_syntax::TreeArc<ast::Expr> { | ||
91 | let expanded = expand(rules, invocation); | 82 | let expanded = expand(rules, invocation); |
92 | token_tree_to_expr(&expanded).unwrap().tree().to_owned() | 83 | token_tree_to_expr(&expanded).unwrap().tree() |
93 | } | 84 | } |
94 | 85 | ||
95 | pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { | 86 | pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { |
@@ -97,7 +88,7 @@ pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { | |||
97 | let wrapped = format!("wrap_macro!( {} )", text); | 88 | let wrapped = format!("wrap_macro!( {} )", text); |
98 | let wrapped = ast::SourceFile::parse(&wrapped); | 89 | let wrapped = ast::SourceFile::parse(&wrapped); |
99 | let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | 90 | let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); |
100 | let mut wrapped = ast_to_token_tree(wrapped).unwrap().0; | 91 | let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; |
101 | wrapped.delimiter = tt::Delimiter::None; | 92 | wrapped.delimiter = tt::Delimiter::None; |
102 | 93 | ||
103 | wrapped | 94 | wrapped |
@@ -164,8 +155,8 @@ pub(crate) fn assert_expansion( | |||
164 | 155 | ||
165 | let (expanded_tree, expected_tree) = match kind { | 156 | let (expanded_tree, expected_tree) = match kind { |
166 | MacroKind::Items => { | 157 | MacroKind::Items => { |
167 | let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree().to_owned(); | 158 | let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree(); |
168 | let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree().to_owned(); | 159 | let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree(); |
169 | 160 | ||
170 | ( | 161 | ( |
171 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), | 162 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), |
@@ -174,8 +165,8 @@ pub(crate) fn assert_expansion( | |||
174 | } | 165 | } |
175 | 166 | ||
176 | MacroKind::Stmts => { | 167 | MacroKind::Stmts => { |
177 | let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned(); | 168 | let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree(); |
178 | let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree().to_owned(); | 169 | let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree(); |
179 | 170 | ||
180 | ( | 171 | ( |
181 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), | 172 | debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), |
@@ -419,7 +410,7 @@ fn test_expand_to_item_list() { | |||
419 | ", | 410 | ", |
420 | ); | 411 | ); |
421 | let expansion = expand(&rules, "structs!(Foo, Bar);"); | 412 | let expansion = expand(&rules, "structs!(Foo, Bar);"); |
422 | let tree = token_tree_to_macro_items(&expansion).unwrap().tree().to_owned(); | 413 | let tree = token_tree_to_macro_items(&expansion).unwrap().tree(); |
423 | assert_eq!( | 414 | assert_eq!( |
424 | tree.syntax().debug_dump().trim(), | 415 | tree.syntax().debug_dump().trim(), |
425 | r#" | 416 | r#" |
@@ -537,7 +528,7 @@ fn test_tt_to_stmts() { | |||
537 | ); | 528 | ); |
538 | 529 | ||
539 | let expanded = expand(&rules, "foo!{}"); | 530 | let expanded = expand(&rules, "foo!{}"); |
540 | let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned(); | 531 | let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree(); |
541 | 532 | ||
542 | assert_eq!( | 533 | assert_eq!( |
543 | stmts.syntax().debug_dump().trim(), | 534 | stmts.syntax().debug_dump().trim(), |