diff options
Diffstat (limited to 'crates/mbe')
-rw-r--r-- | crates/mbe/src/benchmark.rs | 2 | ||||
-rw-r--r-- | crates/mbe/src/expander.rs | 6 | ||||
-rw-r--r-- | crates/mbe/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/mbe/src/syntax_bridge.rs | 23 | ||||
-rw-r--r-- | crates/mbe/src/tests.rs | 11 | ||||
-rw-r--r-- | crates/mbe/src/tests/expand.rs | 15 | ||||
-rw-r--r-- | crates/mbe/src/tests/rule.rs | 2 | ||||
-rw-r--r-- | crates/mbe/src/tt_iter.rs | 11 |
8 files changed, 49 insertions, 27 deletions
diff --git a/crates/mbe/src/benchmark.rs b/crates/mbe/src/benchmark.rs index ba814a2e1..38707ffa5 100644 --- a/crates/mbe/src/benchmark.rs +++ b/crates/mbe/src/benchmark.rs | |||
@@ -65,7 +65,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> { | |||
65 | .filter_map(ast::MacroRules::cast) | 65 | .filter_map(ast::MacroRules::cast) |
66 | .map(|rule| { | 66 | .map(|rule| { |
67 | let id = rule.name().unwrap().to_string(); | 67 | let id = rule.name().unwrap().to_string(); |
68 | let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()).unwrap(); | 68 | let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()); |
69 | (id, def_tt) | 69 | (id, def_tt) |
70 | }) | 70 | }) |
71 | .collect() | 71 | .collect() |
diff --git a/crates/mbe/src/expander.rs b/crates/mbe/src/expander.rs index 3197c834c..bfef7f73d 100644 --- a/crates/mbe/src/expander.rs +++ b/crates/mbe/src/expander.rs | |||
@@ -159,8 +159,7 @@ mod tests { | |||
159 | let macro_definition = | 159 | let macro_definition = |
160 | source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); | 160 | source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); |
161 | 161 | ||
162 | let (definition_tt, _) = | 162 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()); |
163 | ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); | ||
164 | crate::MacroRules::parse(&definition_tt).unwrap() | 163 | crate::MacroRules::parse(&definition_tt).unwrap() |
165 | } | 164 | } |
166 | 165 | ||
@@ -169,8 +168,7 @@ mod tests { | |||
169 | let macro_invocation = | 168 | let macro_invocation = |
170 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 169 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
171 | 170 | ||
172 | let (invocation_tt, _) = | 171 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()); |
173 | ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); | ||
174 | 172 | ||
175 | expand_rules(&rules.rules, &invocation_tt) | 173 | expand_rules(&rules.rules, &invocation_tt) |
176 | } | 174 | } |
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index e74f8cf3f..3af5bc18b 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs | |||
@@ -220,9 +220,11 @@ impl MacroDef { | |||
220 | while src.len() > 0 { | 220 | while src.len() > 0 { |
221 | let rule = Rule::parse(&mut src, true)?; | 221 | let rule = Rule::parse(&mut src, true)?; |
222 | rules.push(rule); | 222 | rules.push(rule); |
223 | if let Err(()) = src.expect_char(';') { | 223 | if let Err(()) = src.expect_any_char(&[';', ',']) { |
224 | if src.len() > 0 { | 224 | if src.len() > 0 { |
225 | return Err(ParseError::Expected("expected `;`".to_string())); | 225 | return Err(ParseError::Expected( |
226 | "expected `;` or `,` to delimit rules".to_string(), | ||
227 | )); | ||
226 | } | 228 | } |
227 | break; | 229 | break; |
228 | } | 230 | } |
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 9d433b3b0..9ba98f7fb 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs | |||
@@ -43,18 +43,18 @@ pub struct TokenMap { | |||
43 | 43 | ||
44 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro | 44 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro |
45 | /// will consume). | 45 | /// will consume). |
46 | pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> Option<(tt::Subtree, TokenMap)> { | 46 | pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> (tt::Subtree, TokenMap) { |
47 | syntax_node_to_token_tree(ast.syntax()) | 47 | syntax_node_to_token_tree(ast.syntax()) |
48 | } | 48 | } |
49 | 49 | ||
50 | /// Convert the syntax node to a `TokenTree` (what macro | 50 | /// Convert the syntax node to a `TokenTree` (what macro |
51 | /// will consume). | 51 | /// will consume). |
52 | pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, TokenMap)> { | 52 | pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) { |
53 | let global_offset = node.text_range().start(); | 53 | let global_offset = node.text_range().start(); |
54 | let mut c = Convertor::new(node, global_offset); | 54 | let mut c = Convertor::new(node, global_offset); |
55 | let subtree = c.go()?; | 55 | let subtree = c.go(); |
56 | c.id_alloc.map.entries.shrink_to_fit(); | 56 | c.id_alloc.map.entries.shrink_to_fit(); |
57 | Some((subtree, c.id_alloc.map)) | 57 | (subtree, c.id_alloc.map) |
58 | } | 58 | } |
59 | 59 | ||
60 | // The following items are what `rustc` macro can be parsed into : | 60 | // The following items are what `rustc` macro can be parsed into : |
@@ -108,7 +108,7 @@ pub fn parse_to_token_tree(text: &str) -> Option<(tt::Subtree, TokenMap)> { | |||
108 | }, | 108 | }, |
109 | }; | 109 | }; |
110 | 110 | ||
111 | let subtree = conv.go()?; | 111 | let subtree = conv.go(); |
112 | Some((subtree, conv.id_alloc.map)) | 112 | Some((subtree, conv.id_alloc.map)) |
113 | } | 113 | } |
114 | 114 | ||
@@ -319,21 +319,18 @@ trait SrcToken: std::fmt::Debug { | |||
319 | trait TokenConvertor { | 319 | trait TokenConvertor { |
320 | type Token: SrcToken; | 320 | type Token: SrcToken; |
321 | 321 | ||
322 | fn go(&mut self) -> Option<tt::Subtree> { | 322 | fn go(&mut self) -> tt::Subtree { |
323 | let mut subtree = tt::Subtree::default(); | 323 | let mut subtree = tt::Subtree::default(); |
324 | subtree.delimiter = None; | 324 | subtree.delimiter = None; |
325 | while self.peek().is_some() { | 325 | while self.peek().is_some() { |
326 | self.collect_leaf(&mut subtree.token_trees); | 326 | self.collect_leaf(&mut subtree.token_trees); |
327 | } | 327 | } |
328 | if subtree.token_trees.is_empty() { | ||
329 | return None; | ||
330 | } | ||
331 | if subtree.token_trees.len() == 1 { | 328 | if subtree.token_trees.len() == 1 { |
332 | if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] { | 329 | if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] { |
333 | return Some(first.clone()); | 330 | return first.clone(); |
334 | } | 331 | } |
335 | } | 332 | } |
336 | Some(subtree) | 333 | subtree |
337 | } | 334 | } |
338 | 335 | ||
339 | fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) { | 336 | fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) { |
@@ -861,7 +858,7 @@ mod tests { | |||
861 | // - T!['}'] | 858 | // - T!['}'] |
862 | // - WHITE_SPACE | 859 | // - WHITE_SPACE |
863 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); | 860 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); |
864 | let tt = ast_to_token_tree(&token_tree).unwrap().0; | 861 | let tt = ast_to_token_tree(&token_tree).0; |
865 | 862 | ||
866 | assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); | 863 | assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); |
867 | } | 864 | } |
@@ -870,7 +867,7 @@ mod tests { | |||
870 | fn test_token_tree_multi_char_punct() { | 867 | fn test_token_tree_multi_char_punct() { |
871 | let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap(); | 868 | let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap(); |
872 | let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap(); | 869 | let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap(); |
873 | let tt = ast_to_token_tree(&struct_def).unwrap().0; | 870 | let tt = ast_to_token_tree(&struct_def).0; |
874 | token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); | 871 | token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); |
875 | } | 872 | } |
876 | } | 873 | } |
diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs index 6da18ecf4..3698ff3f0 100644 --- a/crates/mbe/src/tests.rs +++ b/crates/mbe/src/tests.rs | |||
@@ -29,8 +29,7 @@ macro_rules! impl_fixture { | |||
29 | let macro_invocation = | 29 | let macro_invocation = |
30 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 30 | source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
31 | 31 | ||
32 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()) | 32 | let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()); |
33 | .ok_or_else(|| ExpandError::ConversionError)?; | ||
34 | 33 | ||
35 | self.rules.expand(&invocation_tt).result() | 34 | self.rules.expand(&invocation_tt).result() |
36 | } | 35 | } |
@@ -101,7 +100,7 @@ macro_rules! impl_fixture { | |||
101 | .descendants() | 100 | .descendants() |
102 | .find_map(ast::TokenTree::cast) | 101 | .find_map(ast::TokenTree::cast) |
103 | .unwrap(); | 102 | .unwrap(); |
104 | let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; | 103 | let mut wrapped = ast_to_token_tree(&wrapped).0; |
105 | wrapped.delimiter = None; | 104 | wrapped.delimiter = None; |
106 | wrapped | 105 | wrapped |
107 | }; | 106 | }; |
@@ -151,7 +150,7 @@ pub(crate) fn parse_macro_error(ra_fixture: &str) -> ParseError { | |||
151 | 150 | ||
152 | pub(crate) fn parse_to_token_tree_by_syntax(ra_fixture: &str) -> tt::Subtree { | 151 | pub(crate) fn parse_to_token_tree_by_syntax(ra_fixture: &str) -> tt::Subtree { |
153 | let source_file = ast::SourceFile::parse(ra_fixture).ok().unwrap(); | 152 | let source_file = ast::SourceFile::parse(ra_fixture).ok().unwrap(); |
154 | let tt = syntax_node_to_token_tree(source_file.syntax()).unwrap().0; | 153 | let tt = syntax_node_to_token_tree(source_file.syntax()).0; |
155 | 154 | ||
156 | let parsed = parse_to_token_tree(ra_fixture).unwrap().0; | 155 | let parsed = parse_to_token_tree(ra_fixture).unwrap().0; |
157 | assert_eq!(tt, parsed); | 156 | assert_eq!(tt, parsed); |
@@ -164,7 +163,7 @@ fn parse_macro_rules_to_tt(ra_fixture: &str) -> tt::Subtree { | |||
164 | let macro_definition = | 163 | let macro_definition = |
165 | source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); | 164 | source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); |
166 | 165 | ||
167 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); | 166 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()); |
168 | 167 | ||
169 | let parsed = parse_to_token_tree( | 168 | let parsed = parse_to_token_tree( |
170 | &ra_fixture[macro_definition.token_tree().unwrap().syntax().text_range()], | 169 | &ra_fixture[macro_definition.token_tree().unwrap().syntax().text_range()], |
@@ -181,7 +180,7 @@ fn parse_macro_def_to_tt(ra_fixture: &str) -> tt::Subtree { | |||
181 | let macro_definition = | 180 | let macro_definition = |
182 | source_file.syntax().descendants().find_map(ast::MacroDef::cast).unwrap(); | 181 | source_file.syntax().descendants().find_map(ast::MacroDef::cast).unwrap(); |
183 | 182 | ||
184 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.body().unwrap()).unwrap(); | 183 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.body().unwrap()); |
185 | 184 | ||
186 | let parsed = | 185 | let parsed = |
187 | parse_to_token_tree(&ra_fixture[macro_definition.body().unwrap().syntax().text_range()]) | 186 | parse_to_token_tree(&ra_fixture[macro_definition.body().unwrap().syntax().text_range()]) |
diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 8951f3813..e02d038b6 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs | |||
@@ -663,6 +663,21 @@ macro foo { | |||
663 | } | 663 | } |
664 | 664 | ||
665 | #[test] | 665 | #[test] |
666 | fn test_macro_2_0_panic_2015() { | ||
667 | parse_macro2( | ||
668 | r#" | ||
669 | macro panic_2015 { | ||
670 | () => ( | ||
671 | ), | ||
672 | (bar) => ( | ||
673 | ), | ||
674 | } | ||
675 | "#, | ||
676 | ) | ||
677 | .assert_expand_items("panic_2015!(bar);", ""); | ||
678 | } | ||
679 | |||
680 | #[test] | ||
666 | fn test_path() { | 681 | fn test_path() { |
667 | parse_macro( | 682 | parse_macro( |
668 | r#" | 683 | r#" |
diff --git a/crates/mbe/src/tests/rule.rs b/crates/mbe/src/tests/rule.rs index bf48112b3..5c61a98fd 100644 --- a/crates/mbe/src/tests/rule.rs +++ b/crates/mbe/src/tests/rule.rs | |||
@@ -44,6 +44,6 @@ fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError | |||
44 | let macro_definition = | 44 | let macro_definition = |
45 | source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); | 45 | source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); |
46 | 46 | ||
47 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); | 47 | let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()); |
48 | crate::MacroRules::parse(&definition_tt) | 48 | crate::MacroRules::parse(&definition_tt) |
49 | } | 49 | } |
diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs index 319a40f2a..195b8cf30 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs | |||
@@ -34,6 +34,17 @@ impl<'a> TtIter<'a> { | |||
34 | } | 34 | } |
35 | } | 35 | } |
36 | 36 | ||
37 | pub(crate) fn expect_any_char(&mut self, chars: &[char]) -> Result<(), ()> { | ||
38 | match self.next() { | ||
39 | Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: c, .. }))) | ||
40 | if chars.contains(c) => | ||
41 | { | ||
42 | Ok(()) | ||
43 | } | ||
44 | _ => Err(()), | ||
45 | } | ||
46 | } | ||
47 | |||
37 | pub(crate) fn expect_subtree(&mut self) -> Result<&'a tt::Subtree, ()> { | 48 | pub(crate) fn expect_subtree(&mut self) -> Result<&'a tt::Subtree, ()> { |
38 | match self.next() { | 49 | match self.next() { |
39 | Some(tt::TokenTree::Subtree(it)) => Ok(it), | 50 | Some(tt::TokenTree::Subtree(it)) => Ok(it), |