diff options
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 216 |
1 files changed, 27 insertions, 189 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index d7b18dd0f..7ebba807c 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs | |||
@@ -24,6 +24,7 @@ mod subtree_source; | |||
24 | mod subtree_parser; | 24 | mod subtree_parser; |
25 | 25 | ||
26 | use ra_syntax::SmolStr; | 26 | use ra_syntax::SmolStr; |
27 | use smallvec::SmallVec; | ||
27 | 28 | ||
28 | pub use tt::{Delimiter, Punct}; | 29 | pub use tt::{Delimiter, Punct}; |
29 | 30 | ||
@@ -99,10 +100,17 @@ pub(crate) struct Subtree { | |||
99 | } | 100 | } |
100 | 101 | ||
101 | #[derive(Clone, Debug, PartialEq, Eq)] | 102 | #[derive(Clone, Debug, PartialEq, Eq)] |
103 | pub(crate) enum Separator { | ||
104 | Literal(tt::Literal), | ||
105 | Ident(tt::Ident), | ||
106 | Puncts(SmallVec<[tt::Punct; 3]>), | ||
107 | } | ||
108 | |||
109 | #[derive(Clone, Debug, PartialEq, Eq)] | ||
102 | pub(crate) struct Repeat { | 110 | pub(crate) struct Repeat { |
103 | pub(crate) subtree: Subtree, | 111 | pub(crate) subtree: Subtree, |
104 | pub(crate) kind: RepeatKind, | 112 | pub(crate) kind: RepeatKind, |
105 | pub(crate) separator: Option<char>, | 113 | pub(crate) separator: Option<Separator>, |
106 | } | 114 | } |
107 | 115 | ||
108 | #[derive(Clone, Debug, PartialEq, Eq)] | 116 | #[derive(Clone, Debug, PartialEq, Eq)] |
@@ -175,8 +183,8 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
175 | let expansion = rules.expand(&invocation_tt).unwrap(); | 183 | let expansion = rules.expand(&invocation_tt).unwrap(); |
176 | assert_eq!( | 184 | assert_eq!( |
177 | expansion.to_string(), | 185 | expansion.to_string(), |
178 | "impl From < Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree :: Leaf (it)}} \ | 186 | "impl From <Leaf > for TokenTree {fn from (it : Leaf) -> TokenTree {TokenTree ::Leaf (it)}} \ |
179 | impl From < Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree :: Subtree (it)}}" | 187 | impl From <Subtree > for TokenTree {fn from (it : Subtree) -> TokenTree {TokenTree ::Subtree (it)}}" |
180 | ) | 188 | ) |
181 | } | 189 | } |
182 | 190 | ||
@@ -384,7 +392,7 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
384 | "#, | 392 | "#, |
385 | ); | 393 | ); |
386 | 394 | ||
387 | assert_expansion(&rules, "foo! { foo, bar }", "fn baz {foo () ; bar ()}"); | 395 | assert_expansion(&rules, "foo! { foo, bar }", "fn baz {foo () ;bar ()}"); |
388 | } | 396 | } |
389 | 397 | ||
390 | #[test] | 398 | #[test] |
@@ -417,6 +425,18 @@ impl_froms!(TokenTree: Leaf, Subtree); | |||
417 | } | 425 | } |
418 | 426 | ||
419 | #[test] | 427 | #[test] |
428 | fn test_match_group_with_multichar_sep() { | ||
429 | let rules = create_rules( | ||
430 | r#" | ||
431 | macro_rules! foo { | ||
432 | (fn $name:ident {$($i:literal)*} ) => ( fn $name() -> bool { $($i)&&*} ); | ||
433 | }"#, | ||
434 | ); | ||
435 | |||
436 | assert_expansion(&rules, "foo! (fn baz {true true} )", "fn baz () -> bool {true &&true}"); | ||
437 | } | ||
438 | |||
439 | #[test] | ||
420 | fn test_expand_to_item_list() { | 440 | fn test_expand_to_item_list() { |
421 | let rules = create_rules( | 441 | let rules = create_rules( |
422 | " | 442 | " |
@@ -597,7 +617,7 @@ MACRO_ITEMS@[0; 40) | |||
597 | assert_expansion( | 617 | assert_expansion( |
598 | &rules, | 618 | &rules, |
599 | "foo! { bar::<u8>::baz::<u8> }", | 619 | "foo! { bar::<u8>::baz::<u8> }", |
600 | "fn foo () {let a = bar :: < u8 > :: baz :: < u8 > ;}", | 620 | "fn foo () {let a = bar ::< u8 >:: baz ::< u8 > ;}", |
601 | ); | 621 | ); |
602 | } | 622 | } |
603 | 623 | ||
@@ -891,7 +911,7 @@ MACRO_ITEMS@[0; 40) | |||
891 | } | 911 | } |
892 | "#, | 912 | "#, |
893 | ); | 913 | ); |
894 | assert_expansion(&rules, r#"foo!{'a}"#, r#"struct Ref < 'a > {s : & 'a str}"#); | 914 | assert_expansion(&rules, r#"foo!{'a}"#, r#"struct Ref <'a > {s : &'a str}"#); |
895 | } | 915 | } |
896 | 916 | ||
897 | #[test] | 917 | #[test] |
@@ -1063,7 +1083,7 @@ macro_rules! int_base { | |||
1063 | ); | 1083 | ); |
1064 | 1084 | ||
1065 | assert_expansion(&rules, r#" int_base!{Binary for isize as usize -> Binary}"#, | 1085 | assert_expansion(&rules, r#" int_base!{Binary for isize as usize -> Binary}"#, |
1066 | "# [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)}}" | 1086 | "# [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)}}" |
1067 | ); | 1087 | ); |
1068 | } | 1088 | } |
1069 | 1089 | ||
@@ -1140,186 +1160,4 @@ impl_fn_for_zst ! { | |||
1140 | assert_expansion(&rules, r#"impl_nonzero_fmt ! { # [ stable ( feature = "nonzero" , since = "1.28.0" ) ] ( Debug , Display , Binary , Octal , LowerHex , UpperHex ) for NonZeroU8 }"#, | 1160 | assert_expansion(&rules, r#"impl_nonzero_fmt ! { # [ stable ( feature = "nonzero" , since = "1.28.0" ) ] ( Debug , Display , Binary , Octal , LowerHex , UpperHex ) for NonZeroU8 }"#, |
1141 | "fn foo () {}"); | 1161 | "fn foo () {}"); |
1142 | } | 1162 | } |
1143 | |||
1144 | #[test] | ||
1145 | fn test_tuple_impls() { | ||
1146 | // from https://github.com/rust-lang/rust/blob/316a391dcb7d66dc25f1f9a4ec9d368ef7615005/src/libcore/num/mod.rs#L12 | ||
1147 | let rules = create_rules( | ||
1148 | r#" | ||
1149 | macro_rules! tuple_impls { | ||
1150 | ($( | ||
1151 | $Tuple:ident { | ||
1152 | $(($idx:tt) -> $T:ident)+ | ||
1153 | } | ||
1154 | )+) => { | ||
1155 | $( | ||
1156 | #[stable(feature = "rust1", since = "1.0.0")] | ||
1157 | impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized { | ||
1158 | #[inline] | ||
1159 | fn eq(&self, other: &($($T,)+)) -> bool { | ||
1160 | $(self.$idx == other.$idx)&&+ | ||
1161 | } | ||
1162 | #[inline] | ||
1163 | fn ne(&self, other: &($($T,)+)) -> bool { | ||
1164 | $(self.$idx != other.$idx)||+ | ||
1165 | } | ||
1166 | } | ||
1167 | |||
1168 | #[stable(feature = "rust1", since = "1.0.0")] | ||
1169 | impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {} | ||
1170 | |||
1171 | #[stable(feature = "rust1", since = "1.0.0")] | ||
1172 | impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) | ||
1173 | where last_type!($($T,)+): ?Sized { | ||
1174 | #[inline] | ||
1175 | fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> { | ||
1176 | lexical_partial_cmp!($(self.$idx, other.$idx),+) | ||
1177 | } | ||
1178 | #[inline] | ||
1179 | fn lt(&self, other: &($($T,)+)) -> bool { | ||
1180 | lexical_ord!(lt, $(self.$idx, other.$idx),+) | ||
1181 | } | ||
1182 | #[inline] | ||
1183 | fn le(&self, other: &($($T,)+)) -> bool { | ||
1184 | lexical_ord!(le, $(self.$idx, other.$idx),+) | ||
1185 | } | ||
1186 | #[inline] | ||
1187 | fn ge(&self, other: &($($T,)+)) -> bool { | ||
1188 | lexical_ord!(ge, $(self.$idx, other.$idx),+) | ||
1189 | } | ||
1190 | #[inline] | ||
1191 | fn gt(&self, other: &($($T,)+)) -> bool { | ||
1192 | lexical_ord!(gt, $(self.$idx, other.$idx),+) | ||
1193 | } | ||
1194 | } | ||
1195 | |||
1196 | #[stable(feature = "rust1", since = "1.0.0")] | ||
1197 | impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized { | ||
1198 | #[inline] | ||
1199 | fn cmp(&self, other: &($($T,)+)) -> Ordering { | ||
1200 | lexical_cmp!($(self.$idx, other.$idx),+) | ||
1201 | } | ||
1202 | } | ||
1203 | |||
1204 | #[stable(feature = "rust1", since = "1.0.0")] | ||
1205 | impl<$($T:Default),+> Default for ($($T,)+) { | ||
1206 | #[inline] | ||
1207 | fn default() -> ($($T,)+) { | ||
1208 | ($({ let x: $T = Default::default(); x},)+) | ||
1209 | } | ||
1210 | } | ||
1211 | )+ | ||
1212 | } | ||
1213 | }"#, | ||
1214 | ); | ||
1215 | |||
1216 | assert_expansion( | ||
1217 | &rules, | ||
1218 | r#"tuple_impls ! { | ||
1219 | Tuple1 { | ||
1220 | ( 0 ) -> A | ||
1221 | } | ||
1222 | Tuple2 { | ||
1223 | ( 0 ) -> A | ||
1224 | ( 1 ) -> B | ||
1225 | } | ||
1226 | Tuple3 { | ||
1227 | ( 0 ) -> A | ||
1228 | ( 1 ) -> B | ||
1229 | ( 2 ) -> C | ||
1230 | } | ||
1231 | Tuple4 { | ||
1232 | ( 0 ) -> A | ||
1233 | ( 1 ) -> B | ||
1234 | ( 2 ) -> C | ||
1235 | ( 3 ) -> D | ||
1236 | } | ||
1237 | Tuple5 { | ||
1238 | ( 0 ) -> A | ||
1239 | ( 1 ) -> B | ||
1240 | ( 2 ) -> C | ||
1241 | ( 3 ) -> D | ||
1242 | ( 4 ) -> E | ||
1243 | } | ||
1244 | Tuple6 { | ||
1245 | ( 0 ) -> A | ||
1246 | ( 1 ) -> B | ||
1247 | ( 2 ) -> C | ||
1248 | ( 3 ) -> D | ||
1249 | ( 4 ) -> E | ||
1250 | ( 5 ) -> F | ||
1251 | } | ||
1252 | Tuple7 { | ||
1253 | ( 0 ) -> A | ||
1254 | ( 1 ) -> B | ||
1255 | ( 2 ) -> C | ||
1256 | ( 3 ) -> D | ||
1257 | ( 4 ) -> E | ||
1258 | ( 5 ) -> F | ||
1259 | ( 6 ) -> G | ||
1260 | } | ||
1261 | Tuple8 { | ||
1262 | ( 0 ) -> A | ||
1263 | ( 1 ) -> B | ||
1264 | ( 2 ) -> C | ||
1265 | ( 3 ) -> D | ||
1266 | ( 4 ) -> E | ||
1267 | ( 5 ) -> F | ||
1268 | ( 6 ) -> G | ||
1269 | ( 7 ) -> H | ||
1270 | } | ||
1271 | Tuple9 { | ||
1272 | ( 0 ) -> A | ||
1273 | ( 1 ) -> B | ||
1274 | ( 2 ) -> C | ||
1275 | ( 3 ) -> D | ||
1276 | ( 4 ) -> E | ||
1277 | ( 5 ) -> F | ||
1278 | ( 6 ) -> G | ||
1279 | ( 7 ) -> H | ||
1280 | ( 8 ) -> I | ||
1281 | } | ||
1282 | Tuple10 { | ||
1283 | ( 0 ) -> A | ||
1284 | ( 1 ) -> B | ||
1285 | ( 2 ) -> C | ||
1286 | ( 3 ) -> D | ||
1287 | ( 4 ) -> E | ||
1288 | ( 5 ) -> F | ||
1289 | ( 6 ) -> G | ||
1290 | ( 7 ) -> H | ||
1291 | ( 8 ) -> I | ||
1292 | ( 9 ) -> J | ||
1293 | } | ||
1294 | Tuple11 { | ||
1295 | ( 0 ) -> A | ||
1296 | ( 1 ) -> B | ||
1297 | ( 2 ) -> C | ||
1298 | ( 3 ) -> D | ||
1299 | ( 4 ) -> E | ||
1300 | ( 5 ) -> F | ||
1301 | ( 6 ) -> G | ||
1302 | ( 7 ) -> H | ||
1303 | ( 8 ) -> I | ||
1304 | ( 9 ) -> J | ||
1305 | ( 10 ) -> K | ||
1306 | } | ||
1307 | Tuple12 { | ||
1308 | ( 0 ) -> A | ||
1309 | ( 1 ) -> B | ||
1310 | ( 2 ) -> C | ||
1311 | ( 3 ) -> D | ||
1312 | ( 4 ) -> E | ||
1313 | ( 5 ) -> F | ||
1314 | ( 6 ) -> G | ||
1315 | ( 7 ) -> H | ||
1316 | ( 8 ) -> I | ||
1317 | ( 9 ) -> J | ||
1318 | ( 10 ) -> K | ||
1319 | ( 11 ) -> L | ||
1320 | } | ||
1321 | }"#, | ||
1322 | "fn foo () {}", | ||
1323 | ); | ||
1324 | } | ||
1325 | } | 1163 | } |