aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_hir_expand/Cargo.toml1
-rw-r--r--crates/ra_hir_expand/src/db.rs15
-rw-r--r--crates/ra_mbe/src/lib.rs3
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs34
-rw-r--r--crates/ra_mbe/src/tests.rs102
6 files changed, 84 insertions, 72 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b67637fe8..37ff1ce76 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1060,6 +1060,7 @@ dependencies = [
1060 "ra_arena 0.1.0", 1060 "ra_arena 0.1.0",
1061 "ra_db 0.1.0", 1061 "ra_db 0.1.0",
1062 "ra_mbe 0.1.0", 1062 "ra_mbe 0.1.0",
1063 "ra_parser 0.1.0",
1063 "ra_prof 0.1.0", 1064 "ra_prof 0.1.0",
1064 "ra_syntax 0.1.0", 1065 "ra_syntax 0.1.0",
1065 "ra_tt 0.1.0", 1066 "ra_tt 0.1.0",
diff --git a/crates/ra_hir_expand/Cargo.toml b/crates/ra_hir_expand/Cargo.toml
index 9bf5b7918..8f29bf7d9 100644
--- a/crates/ra_hir_expand/Cargo.toml
+++ b/crates/ra_hir_expand/Cargo.toml
@@ -10,6 +10,7 @@ log = "0.4.5"
10ra_arena = { path = "../ra_arena" } 10ra_arena = { path = "../ra_arena" }
11ra_db = { path = "../ra_db" } 11ra_db = { path = "../ra_db" }
12ra_syntax = { path = "../ra_syntax" } 12ra_syntax = { path = "../ra_syntax" }
13ra_parser = { path = "../ra_parser" }
13ra_prof = { path = "../ra_prof" } 14ra_prof = { path = "../ra_prof" }
14tt = { path = "../ra_tt", package = "ra_tt" } 15tt = { path = "../ra_tt", package = "ra_tt" }
15mbe = { path = "../ra_mbe", package = "ra_mbe" } 16mbe = { path = "../ra_mbe", package = "ra_mbe" }
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index b789c6e7b..b4dafe1d8 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -4,6 +4,7 @@ use std::sync::Arc;
4 4
5use mbe::MacroRules; 5use mbe::MacroRules;
6use ra_db::{salsa, SourceDatabase}; 6use ra_db::{salsa, SourceDatabase};
7use ra_parser::FragmentKind;
7use ra_prof::profile; 8use ra_prof::profile;
8use ra_syntax::{AstNode, Parse, SyntaxNode}; 9use ra_syntax::{AstNode, Parse, SyntaxNode};
9 10
@@ -108,12 +109,10 @@ pub(crate) fn parse_macro(
108 }) 109 })
109 .ok()?; 110 .ok()?;
110 111
111 match macro_file.macro_file_kind { 112 let fragment_kind = match macro_file.macro_file_kind {
112 MacroFileKind::Items => { 113 MacroFileKind::Items => FragmentKind::Items,
113 mbe::token_tree_to_items(&tt).ok().map(|(p, map)| (p.to_syntax(), Arc::new(map))) 114 MacroFileKind::Expr => FragmentKind::Expr,
114 } 115 };
115 MacroFileKind::Expr => { 116 let (parse, rev_token_map) = mbe::token_tree_to_syntax_node(&tt, fragment_kind).ok()?;
116 mbe::token_tree_to_expr(&tt).ok().map(|(p, map)| (p.to_syntax(), Arc::new(map))) 117 Some((parse, Arc::new(rev_token_map)))
117 }
118 }
119} 118}
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 70a289f09..8a31d1c36 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -31,8 +31,7 @@ pub enum ExpandError {
31} 31}
32 32
33pub use crate::syntax_bridge::{ 33pub use crate::syntax_bridge::{
34 ast_to_token_tree, syntax_node_to_token_tree, token_tree_to_expr, token_tree_to_items, 34 ast_to_token_tree, syntax_node_to_token_tree, token_tree_to_syntax_node, RevTokenMap, TokenMap,
35 token_tree_to_macro_stmts, token_tree_to_pat, token_tree_to_ty, RevTokenMap, TokenMap,
36}; 35};
37 36
38/// This struct contains AST for a single `macro_rules` definition. What might 37/// This struct contains AST for a single `macro_rules` definition. What might
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 9653f7fef..3f57ce3b5 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -1,9 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_parser::{ 3use ra_parser::{FragmentKind, ParseError, TreeSink};
4 FragmentKind::{self, *},
5 ParseError, TreeSink,
6};
7use ra_syntax::{ 4use ra_syntax::{
8 ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, 5 ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode,
9 SyntaxTreeBuilder, TextRange, TextUnit, T, 6 SyntaxTreeBuilder, TextRange, TextUnit, T,
@@ -55,7 +52,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
55// * ImplItems(SmallVec<[ast::ImplItem; 1]>) 52// * ImplItems(SmallVec<[ast::ImplItem; 1]>)
56// * ForeignItems(SmallVec<[ast::ForeignItem; 1]> 53// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
57 54
58fn fragment_to_syntax_node( 55pub fn token_tree_to_syntax_node(
59 tt: &tt::Subtree, 56 tt: &tt::Subtree,
60 fragment_kind: FragmentKind, 57 fragment_kind: FragmentKind,
61) -> Result<(Parse<SyntaxNode>, RevTokenMap), ExpandError> { 58) -> Result<(Parse<SyntaxNode>, RevTokenMap), ExpandError> {
@@ -79,31 +76,6 @@ fn fragment_to_syntax_node(
79 Ok((parse, range_map)) 76 Ok((parse, range_map))
80} 77}
81 78
82macro_rules! impl_token_tree_conversions {
83 ($($(#[$attr:meta])* $name:ident => ($kind:ident, $t:ty) ),*) => {
84 $(
85 $(#[$attr])*
86 pub fn $name(tt: &tt::Subtree) -> Result<(Parse<$t>, RevTokenMap), ExpandError> {
87 let (parse, map) = fragment_to_syntax_node(tt, $kind)?;
88 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError).map(|p| (p, map))
89 }
90 )*
91 }
92}
93
94impl_token_tree_conversions! {
95 /// Parses the token tree (result of macro expansion) to an expression
96 token_tree_to_expr => (Expr, ast::Expr),
97 /// Parses the token tree (result of macro expansion) to a Pattern
98 token_tree_to_pat => (Pattern, ast::Pat),
99 /// Parses the token tree (result of macro expansion) to a Type
100 token_tree_to_ty => (Type, ast::TypeRef),
101 /// Parses the token tree (result of macro expansion) as a sequence of stmts
102 token_tree_to_macro_stmts => (Statements, ast::MacroStmts),
103 /// Parses the token tree (result of macro expansion) as a sequence of items
104 token_tree_to_items => (Items, ast::MacroItems)
105}
106
107impl TokenMap { 79impl TokenMap {
108 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { 80 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> {
109 let idx = tt.0 as usize; 81 let idx = tt.0 as usize;
@@ -446,6 +418,6 @@ mod tests {
446 "#, 418 "#,
447 ); 419 );
448 let expansion = expand(&rules, "stmts!();"); 420 let expansion = expand(&rules, "stmts!();");
449 assert!(token_tree_to_expr(&expansion).is_err()); 421 assert!(token_tree_to_syntax_node(&expansion, FragmentKind::Expr).is_err());
450 } 422 }
451} 423}
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs
index a848ea334..0109a4d98 100644
--- a/crates/ra_mbe/src/tests.rs
+++ b/crates/ra_mbe/src/tests.rs
@@ -1,3 +1,4 @@
1use ra_parser::FragmentKind;
1use ra_syntax::{ast, AstNode, NodeOrToken, WalkEvent}; 2use ra_syntax::{ast, AstNode, NodeOrToken, WalkEvent};
2use test_utils::assert_eq_text; 3use test_utils::assert_eq_text;
3 4
@@ -126,9 +127,9 @@ fn test_expr_order() {
126"#, 127"#,
127 ); 128 );
128 let expanded = expand(&rules, "foo! { 1 + 1}"); 129 let expanded = expand(&rules, "foo! { 1 + 1}");
129 let tree = token_tree_to_items(&expanded).unwrap().0.tree(); 130 let tree = token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node();
130 131
131 let dump = format!("{:#?}", tree.syntax()); 132 let dump = format!("{:#?}", tree);
132 assert_eq_text!( 133 assert_eq_text!(
133 dump.trim(), 134 dump.trim(),
134 r#"MACRO_ITEMS@[0; 15) 135 r#"MACRO_ITEMS@[0; 15)
@@ -383,9 +384,9 @@ fn test_expand_to_item_list() {
383 ", 384 ",
384 ); 385 );
385 let expansion = expand(&rules, "structs!(Foo, Bar);"); 386 let expansion = expand(&rules, "structs!(Foo, Bar);");
386 let tree = token_tree_to_items(&expansion).unwrap().0.tree(); 387 let tree = token_tree_to_syntax_node(&expansion, FragmentKind::Items).unwrap().0.syntax_node();
387 assert_eq!( 388 assert_eq!(
388 format!("{:#?}", tree.syntax()).trim(), 389 format!("{:#?}", tree).trim(),
389 r#" 390 r#"
390MACRO_ITEMS@[0; 40) 391MACRO_ITEMS@[0; 40)
391 STRUCT_DEF@[0; 20) 392 STRUCT_DEF@[0; 20)
@@ -501,10 +502,11 @@ fn test_tt_to_stmts() {
501 ); 502 );
502 503
503 let expanded = expand(&rules, "foo!{}"); 504 let expanded = expand(&rules, "foo!{}");
504 let stmts = token_tree_to_macro_stmts(&expanded).unwrap().0.tree(); 505 let stmts =
506 token_tree_to_syntax_node(&expanded, FragmentKind::Statements).unwrap().0.syntax_node();
505 507
506 assert_eq!( 508 assert_eq!(
507 format!("{:#?}", stmts.syntax()).trim(), 509 format!("{:#?}", stmts).trim(),
508 r#"MACRO_STMTS@[0; 15) 510 r#"MACRO_STMTS@[0; 15)
509 LET_STMT@[0; 7) 511 LET_STMT@[0; 7)
510 LET_KW@[0; 3) "let" 512 LET_KW@[0; 3) "let"
@@ -754,7 +756,10 @@ fn test_all_items() {
754 } 756 }
755"#, 757"#,
756 ); 758 );
757 assert_expansion(MacroKind::Items, &rules, r#" 759 assert_expansion(
760 MacroKind::Items,
761 &rules,
762 r#"
758 foo! { 763 foo! {
759 extern crate a; 764 extern crate a;
760 mod b; 765 mod b;
@@ -770,7 +775,9 @@ fn test_all_items() {
770 extern {} 775 extern {}
771 type T = u8; 776 type T = u8;
772 } 777 }
773"#, r#"extern crate a ; mod b ; mod c {} use d ; const E : i32 = 0 ; static F : i32 = 0 ; impl G {} struct H ; enum I {Foo} trait J {} fn h () {} extern {} type T = u8 ;"#); 778"#,
779 r#"extern crate a ; mod b ; mod c {} use d ; const E : i32 = 0 ; static F : i32 = 0 ; impl G {} struct H ; enum I {Foo} trait J {} fn h () {} extern {} type T = u8 ;"#,
780 );
774} 781}
775 782
776#[test] 783#[test]
@@ -946,10 +953,10 @@ fn test_vec() {
946 ); 953 );
947 954
948 let expansion = expand(&rules, r#"vec![1u32,2];"#); 955 let expansion = expand(&rules, r#"vec![1u32,2];"#);
949 let tree = token_tree_to_expr(&expansion).unwrap().0.tree(); 956 let tree = token_tree_to_syntax_node(&expansion, FragmentKind::Expr).unwrap().0.syntax_node();
950 957
951 assert_eq!( 958 assert_eq!(
952 format!("{:#?}", tree.syntax()).trim(), 959 format!("{:#?}", tree).trim(),
953 r#"BLOCK_EXPR@[0; 45) 960 r#"BLOCK_EXPR@[0; 45)
954 BLOCK@[0; 45) 961 BLOCK@[0; 45)
955 L_CURLY@[0; 1) "{" 962 L_CURLY@[0; 1) "{"
@@ -1088,8 +1095,12 @@ macro_rules! generate_pattern_iterators {
1088"#, 1095"#,
1089 ); 1096 );
1090 1097
1091 assert_expansion(MacroKind::Items, &rules, r#"generate_pattern_iterators ! ( double ended ; with # [ stable ( feature = "rust1" , since = "1.0.0" ) ] , Split , RSplit , & 'a str );"#, 1098 assert_expansion(
1092 "fn foo () {}"); 1099 MacroKind::Items,
1100 &rules,
1101 r#"generate_pattern_iterators ! ( double ended ; with # [ stable ( feature = "rust1" , since = "1.0.0" ) ] , Split , RSplit , & 'a str );"#,
1102 "fn foo () {}",
1103 );
1093} 1104}
1094 1105
1095#[test] 1106#[test]
@@ -1171,8 +1182,12 @@ fn test_impl_nonzero_fmt() {
1171"#, 1182"#,
1172 ); 1183 );
1173 1184
1174 assert_expansion(MacroKind::Items, &rules, r#"impl_nonzero_fmt! { # [stable(feature= "nonzero",since="1.28.0")] (Debug,Display,Binary,Octal,LowerHex,UpperHex) for NonZeroU8}"#, 1185 assert_expansion(
1175 "fn foo () {}"); 1186 MacroKind::Items,
1187 &rules,
1188 r#"impl_nonzero_fmt! { # [stable(feature= "nonzero",since="1.28.0")] (Debug,Display,Binary,Octal,LowerHex,UpperHex) for NonZeroU8}"#,
1189 "fn foo () {}",
1190 );
1176} 1191}
1177 1192
1178#[test] 1193#[test]
@@ -1189,8 +1204,12 @@ fn test_cfg_if_items() {
1189"#, 1204"#,
1190 ); 1205 );
1191 1206
1192 assert_expansion(MacroKind::Items, &rules, 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 ; ) ) , }"#, 1207 assert_expansion(
1193 "__cfg_if_items ! {(rustdoc ,) ;}"); 1208 MacroKind::Items,
1209 &rules,
1210 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 ; ) ) , }"#,
1211 "__cfg_if_items ! {(rustdoc ,) ;}",
1212 );
1194} 1213}
1195 1214
1196#[test] 1215#[test]
@@ -1233,10 +1252,13 @@ cfg_if ! {
1233"#, 1252"#,
1234 "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}"); 1253 "__cfg_if_items ! {() ; ((target_env = \"msvc\") ()) , ((all (target_arch = \"wasm32\" , not (target_os = \"emscripten\"))) ()) , (() (mod libunwind ; pub use libunwind :: * ;)) ,}");
1235 1254
1236 assert_expansion(MacroKind::Items, &rules, r#" 1255 assert_expansion(
1256 MacroKind::Items,
1257 &rules,
1258 r#"
1237cfg_if ! { @ __apply cfg ( all ( not ( any ( not ( any ( target_os = "solaris" , target_os = "illumos" ) ) ) ) ) ) , } 1259cfg_if ! { @ __apply cfg ( all ( not ( any ( not ( any ( target_os = "solaris" , target_os = "illumos" ) ) ) ) ) ) , }
1238"#, 1260"#,
1239 "" 1261 "",
1240 ); 1262 );
1241} 1263}
1242 1264
@@ -1291,10 +1313,13 @@ macro_rules! RIDL {
1291}"#, 1313}"#,
1292 ); 1314 );
1293 1315
1294 let expanded = expand(&rules, r#" 1316 let expanded = expand(
1317 &rules,
1318 r#"
1295RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) { 1319RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID3D11DeviceChildVtbl) {
1296 fn GetDataSize(&mut self) -> UINT 1320 fn GetDataSize(&mut self) -> UINT
1297}}"#); 1321}}"#,
1322 );
1298 assert_eq!(expanded.to_string(), "impl ID3D11Asynchronous {pub unsafe fn GetDataSize (& mut self) -> UINT {((* self . lpVtbl) .GetDataSize) (self)}}"); 1323 assert_eq!(expanded.to_string(), "impl ID3D11Asynchronous {pub unsafe fn GetDataSize (& mut self) -> UINT {((* self . lpVtbl) .GetDataSize) (self)}}");
1299} 1324}
1300 1325
@@ -1340,7 +1365,8 @@ quick_error ! (SORT [enum Wrapped # [derive (Debug)]] items [
1340 1365
1341#[test] 1366#[test]
1342fn test_empty_repeat_vars_in_empty_repeat_vars() { 1367fn test_empty_repeat_vars_in_empty_repeat_vars() {
1343 let rules = create_rules(r#" 1368 let rules = create_rules(
1369 r#"
1344macro_rules! delegate_impl { 1370macro_rules! delegate_impl {
1345 ([$self_type:ident, $self_wrap:ty, $self_map:ident] 1371 ([$self_type:ident, $self_wrap:ty, $self_map:ident]
1346 pub trait $name:ident $(: $sup:ident)* $(+ $more_sup:ident)* { 1372 pub trait $name:ident $(: $sup:ident)* $(+ $more_sup:ident)* {
@@ -1385,9 +1411,15 @@ macro_rules! delegate_impl {
1385 } 1411 }
1386 } 1412 }
1387} 1413}
1388"#); 1414"#,
1415 );
1389 1416
1390 assert_expansion(MacroKind::Items, &rules, 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 {}"); 1417 assert_expansion(
1418 MacroKind::Items,
1419 &rules,
1420 r#"delegate_impl ! {[G , & 'a mut G , deref] pub trait Data : GraphBase {@ section type type NodeWeight ;}}"#,
1421 "impl <> Data for & \'a mut G where G : Data {}",
1422 );
1391} 1423}
1392 1424
1393pub(crate) fn create_rules(macro_definition: &str) -> MacroRules { 1425pub(crate) fn create_rules(macro_definition: &str) -> MacroRules {
@@ -1436,22 +1468,30 @@ pub(crate) fn assert_expansion(
1436 }; 1468 };
1437 let (expanded_tree, expected_tree) = match kind { 1469 let (expanded_tree, expected_tree) = match kind {
1438 MacroKind::Items => { 1470 MacroKind::Items => {
1439 let expanded_tree = token_tree_to_items(&expanded).unwrap().0.tree(); 1471 let expanded_tree =
1440 let expected_tree = token_tree_to_items(&expected).unwrap().0.tree(); 1472 token_tree_to_syntax_node(&expanded, FragmentKind::Items).unwrap().0.syntax_node();
1473 let expected_tree =
1474 token_tree_to_syntax_node(&expected, FragmentKind::Items).unwrap().0.syntax_node();
1441 1475
1442 ( 1476 (
1443 debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), 1477 debug_dump_ignore_spaces(&expanded_tree).trim().to_string(),
1444 debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(), 1478 debug_dump_ignore_spaces(&expected_tree).trim().to_string(),
1445 ) 1479 )
1446 } 1480 }
1447 1481
1448 MacroKind::Stmts => { 1482 MacroKind::Stmts => {
1449 let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().0.tree(); 1483 let expanded_tree = token_tree_to_syntax_node(&expanded, FragmentKind::Statements)
1450 let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().0.tree(); 1484 .unwrap()
1485 .0
1486 .syntax_node();
1487 let expected_tree = token_tree_to_syntax_node(&expected, FragmentKind::Statements)
1488 .unwrap()
1489 .0
1490 .syntax_node();
1451 1491
1452 ( 1492 (
1453 debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), 1493 debug_dump_ignore_spaces(&expanded_tree).trim().to_string(),
1454 debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(), 1494 debug_dump_ignore_spaces(&expected_tree).trim().to_string(),
1455 ) 1495 )
1456 } 1496 }
1457 }; 1497 };