From 19d952c603344d853567aeac42dcfa6fe40ba04b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 9 Apr 2020 23:48:08 +0800 Subject: Improve tt::Subtree debug print --- crates/ra_mbe/src/tests.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 254318e23..1ef6f6eed 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -141,6 +141,79 @@ macro_rules! impl_froms { ); } +#[test] +fn test_convert_tt2() { + parse_macro( + r#" +macro_rules! impl_froms { + ($e:ident: $($v:ident),*) => { + $( + impl From<$v> for $e { + fn from(it: $v) -> $e { + $e::$v(it) + } + } + )* + } +} +"#, + ) + .assert_expand( + "impl_froms!(TokenTree: Leaf, Subtree);", + r#" +SUBTREE $ + IDENT impl 20 + IDENT From 21 + PUNCH < [joint] 22 + IDENT Leaf 53 + PUNCH > [alone] 25 + IDENT for 26 + IDENT TokenTree 51 + SUBTREE {} 29 + IDENT fn 30 + IDENT from 31 + SUBTREE () 32 + IDENT it 33 + PUNCH : [alone] 34 + IDENT Leaf 53 + PUNCH - [joint] 37 + PUNCH > [alone] 38 + IDENT TokenTree 51 + SUBTREE {} 41 + IDENT TokenTree 51 + PUNCH : [joint] 44 + PUNCH : [joint] 45 + IDENT Leaf 53 + SUBTREE () 48 + IDENT it 49 + IDENT impl 20 + IDENT From 21 + PUNCH < [joint] 22 + IDENT Subtree 55 + PUNCH > [alone] 25 + IDENT for 26 + IDENT TokenTree 51 + SUBTREE {} 29 + IDENT fn 30 + IDENT from 31 + SUBTREE () 32 + IDENT it 33 + PUNCH : [alone] 34 + IDENT Subtree 55 + PUNCH - [joint] 37 + PUNCH > [alone] 38 + IDENT TokenTree 51 + SUBTREE {} 41 + IDENT TokenTree 51 + PUNCH : [joint] 44 + PUNCH : [joint] 45 + IDENT Subtree 55 + SUBTREE () 48 + IDENT it 49 +"#, + ); +} + #[test] fn test_expr_order() { let expanded = parse_macro( @@ -1479,6 +1552,12 @@ impl MacroFixture { assert_eq!(expansion.to_string(), expected); } + fn assert_expand(&self, invocation: &str, expected: &str) { + let expansion = self.expand_tt(invocation); + let actual = format!("{:?}", expansion); + test_utils::assert_eq_text!(&actual.trim(), &expected.trim()); + } + fn assert_expand_items(&self, invocation: &str, expected: &str) -> &MacroFixture { self.assert_expansion(FragmentKind::Items, invocation, expected); self -- cgit v1.2.3 From 5c5bde47fb759440d007c90fd83021de538120b8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Apr 2020 17:06:57 +0200 Subject: Rename some tokens --- crates/ra_mbe/src/tests.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/ra_mbe') diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 1ef6f6eed..5d1274d21 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -252,7 +252,7 @@ fn test_expr_order() { STAR@[11; 12) "*" LITERAL@[12; 13) INT_NUMBER@[12; 13) "2" - SEMI@[13; 14) ";" + SEMICOLON@[13; 14) ";" R_CURLY@[14; 15) "}""#, ); } @@ -605,7 +605,7 @@ fn test_tt_to_stmts() { EQ@[4; 5) "=" LITERAL@[5; 6) INT_NUMBER@[5; 6) "0" - SEMI@[6; 7) ";" + SEMICOLON@[6; 7) ";" EXPR_STMT@[7; 14) BIN_EXPR@[7; 13) PATH_EXPR@[7; 8) @@ -620,7 +620,7 @@ fn test_tt_to_stmts() { PLUS@[11; 12) "+" LITERAL@[12; 13) INT_NUMBER@[12; 13) "1" - SEMI@[13; 14) ";" + SEMICOLON@[13; 14) ";" EXPR_STMT@[14; 15) PATH_EXPR@[14; 15) PATH@[14; 15) @@ -953,7 +953,7 @@ fn test_tt_composite2() { PATH_SEGMENT@[0; 3) NAME_REF@[0; 3) IDENT@[0; 3) "abs" - EXCL@[3; 4) "!" + BANG@[3; 4) "!" TOKEN_TREE@[4; 10) L_PAREN@[4; 5) "(" EQ@[5; 6) "=" @@ -1073,14 +1073,14 @@ fn test_vec() { PATH_SEGMENT@[9; 12) NAME_REF@[9; 12) IDENT@[9; 12) "Vec" - COLONCOLON@[12; 14) "::" + COLON2@[12; 14) "::" PATH_SEGMENT@[14; 17) NAME_REF@[14; 17) IDENT@[14; 17) "new" ARG_LIST@[17; 19) L_PAREN@[17; 18) "(" R_PAREN@[18; 19) ")" - SEMI@[19; 20) ";" + SEMICOLON@[19; 20) ";" EXPR_STMT@[20; 33) METHOD_CALL_EXPR@[20; 32) PATH_EXPR@[20; 21) @@ -1096,7 +1096,7 @@ fn test_vec() { LITERAL@[27; 31) INT_NUMBER@[27; 31) "1u32" R_PAREN@[31; 32) ")" - SEMI@[32; 33) ";" + SEMICOLON@[32; 33) ";" EXPR_STMT@[33; 43) METHOD_CALL_EXPR@[33; 42) PATH_EXPR@[33; 34) @@ -1112,7 +1112,7 @@ fn test_vec() { LITERAL@[40; 41) INT_NUMBER@[40; 41) "2" R_PAREN@[41; 42) ")" - SEMI@[42; 43) ";" + SEMICOLON@[42; 43) ";" PATH_EXPR@[43; 44) PATH@[43; 44) PATH_SEGMENT@[43; 44) @@ -1760,7 +1760,7 @@ fn test_no_space_after_semi_colon() { MOD_KW@[21; 24) "mod" NAME@[24; 25) IDENT@[24; 25) "m" - SEMI@[25; 26) ";" + SEMICOLON@[25; 26) ";" MODULE@[26; 52) ATTR@[26; 47) POUND@[26; 27) "#" @@ -1779,7 +1779,7 @@ fn test_no_space_after_semi_colon() { MOD_KW@[47; 50) "mod" NAME@[50; 51) IDENT@[50; 51) "f" - SEMI@[51; 52) ";""###, + SEMICOLON@[51; 52) ";""###, ); } -- cgit v1.2.3 From 428d4dfe710f7fe7564e6db82c4b44caaf84655d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Apr 2020 19:41:01 +0200 Subject: Fix accidently quadratic behavior when processing includes closes #3927 --- crates/ra_mbe/src/syntax_bridge.rs | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 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 8e8ae2b29..9fb5cb058 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -137,21 +137,23 @@ impl TokenMap { token_id: tt::TokenId, open_relative_range: TextRange, close_relative_range: TextRange, - ) { + ) -> usize { + let res = self.entries.len(); self.entries .push((token_id, TokenTextRange::Delimiter(open_relative_range, close_relative_range))); + res } - fn update_close_delim(&mut self, token_id: tt::TokenId, close_relative_range: TextRange) { - if let Some(entry) = self.entries.iter_mut().find(|(tid, _)| *tid == token_id) { - if let TokenTextRange::Delimiter(dim, _) = entry.1 { - entry.1 = TokenTextRange::Delimiter(dim, close_relative_range); - } + fn update_close_delim(&mut self, idx: usize, close_relative_range: TextRange) { + let (_, token_text_range) = &mut self.entries[idx]; + if let TokenTextRange::Delimiter(dim, _) = token_text_range { + *token_text_range = TokenTextRange::Delimiter(*dim, close_relative_range); } } - fn remove_delim(&mut self, token_id: tt::TokenId) { - self.entries.retain(|(tid, _)| *tid != token_id); + fn remove_delim(&mut self, idx: usize) { + // FIXME: This could be accidently quadratic + self.entries.remove(idx); } } @@ -238,24 +240,24 @@ impl TokenIdAlloc { token_id } - fn open_delim(&mut self, open_abs_range: TextRange) -> tt::TokenId { + fn open_delim(&mut self, open_abs_range: TextRange) -> (tt::TokenId, usize) { let token_id = tt::TokenId(self.next_id); self.next_id += 1; - self.map.insert_delim( + let idx = self.map.insert_delim( token_id, open_abs_range - self.global_offset, open_abs_range - self.global_offset, ); - token_id + (token_id, idx) } - fn close_delim(&mut self, id: tt::TokenId, close_abs_range: Option) { + fn close_delim(&mut self, idx: usize, close_abs_range: Option) { match close_abs_range { None => { - self.map.remove_delim(id); + self.map.remove_delim(idx); } Some(close) => { - self.map.update_close_delim(id, close - self.global_offset); + self.map.update_close_delim(idx, close - self.global_offset); } } } @@ -322,7 +324,7 @@ trait TokenConvertor { if let Some((kind, closed)) = delim { let mut subtree = tt::Subtree::default(); - let id = self.id_alloc().open_delim(range); + let (id, idx) = self.id_alloc().open_delim(range); subtree.delimiter = Some(tt::Delimiter { kind, id }); while self.peek().map(|it| it.kind() != closed).unwrap_or(false) { @@ -331,7 +333,7 @@ trait TokenConvertor { let last_range = match self.bump() { None => { // For error resilience, we insert an char punct for the opening delim here - self.id_alloc().close_delim(id, None); + self.id_alloc().close_delim(idx, None); let leaf: tt::Leaf = tt::Punct { id: self.id_alloc().alloc(range), char: token.to_char().unwrap(), @@ -344,7 +346,7 @@ trait TokenConvertor { } Some(it) => it.1, }; - self.id_alloc().close_delim(id, Some(last_range)); + self.id_alloc().close_delim(idx, Some(last_range)); subtree.into() } else { let spacing = match self.peek() { -- cgit v1.2.3