From 9eb14e11706ebf9a60b0afbe9bc82a99a816bc02 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 10 Sep 2019 22:12:37 +0300 Subject: cleanup expansion to item list --- crates/ra_hir/src/ids.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 4 +++- crates/ra_mbe/src/lib.rs | 4 ++-- crates/ra_mbe/src/syntax_bridge.rs | 27 +-------------------------- crates/ra_mbe/src/tests.rs | 8 ++++---- 5 files changed, 11 insertions(+), 34 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index e0d0d4209..9ea4e695d 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -90,7 +90,7 @@ impl HirFileId { }) .ok()?; match macro_file.macro_file_kind { - MacroFileKind::Items => Some(Parse::to_syntax(mbe::token_tree_to_ast_item_list(&tt))), + MacroFileKind::Items => mbe::token_tree_to_items(&tt).ok().map(Parse::to_syntax), MacroFileKind::Expr => mbe::token_tree_to_expr(&tt).ok().map(Parse::to_syntax), } } diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index c646d3d00..04b97cb90 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -76,8 +76,10 @@ impl RawItems { source_map: ImportSourceMap::default(), }; if let Some(node) = db.parse_or_expand(file_id) { - if let Some(source_file) = ast::SourceFile::cast(node) { + if let Some(source_file) = ast::SourceFile::cast(node.clone()) { collector.process_module(None, source_file); + } else if let Some(item_list) = ast::MacroItems::cast(node) { + collector.process_module(None, item_list); } } (Arc::new(collector.raw_items), Arc::new(collector.source_map)) diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 52c3d03b5..f07f000ff 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -41,8 +41,8 @@ pub enum ExpandError { } pub use crate::syntax_bridge::{ - ast_to_token_tree, syntax_node_to_token_tree, token_tree_to_ast_item_list, token_tree_to_expr, - token_tree_to_macro_items, token_tree_to_macro_stmts, token_tree_to_pat, token_tree_to_ty, + ast_to_token_tree, syntax_node_to_token_tree, token_tree_to_expr, token_tree_to_items, + token_tree_to_macro_stmts, token_tree_to_pat, token_tree_to_ty, }; /// 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 26524adf9..2d035307b 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -46,25 +46,6 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke // * TraitItems(SmallVec<[ast::TraitItem; 1]>) // * ImplItems(SmallVec<[ast::ImplItem; 1]>) // * ForeignItems(SmallVec<[ast::ForeignItem; 1]> -// -// - -fn token_tree_to_syntax_node(tt: &tt::Subtree, f: F) -> Result, ExpandError> -where - F: Fn(&mut dyn ra_parser::TokenSource, &mut dyn ra_parser::TreeSink), -{ - let tokens = [tt.clone().into()]; - let buffer = TokenBuffer::new(&tokens); - let mut token_source = SubtreeTokenSource::new(&buffer); - let mut tree_sink = TtTreeSink::new(buffer.begin()); - f(&mut token_source, &mut tree_sink); - if tree_sink.roots.len() != 1 { - return Err(ExpandError::ConversionError); - } - //FIXME: would be cool to report errors - let parse = tree_sink.inner.finish(); - Ok(parse) -} fn fragment_to_syntax_node( tt: &tt::Subtree, @@ -115,17 +96,11 @@ pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> Result Result, ExpandError> { +pub fn token_tree_to_items(tt: &tt::Subtree) -> Result, ExpandError> { let parse = fragment_to_syntax_node(tt, Items)?; parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) } -/// Parses the token tree (result of macro expansion) as a sequence of items -pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> Parse { - let parse = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap(); - parse.cast().unwrap() -} - impl TokenMap { pub fn relative_range_of(&self, tt: tt::TokenId) -> Option { let idx = tt.0 as usize; diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 0f07e935d..312fa4626 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -70,7 +70,7 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree { pub(crate) fn expand_to_items(rules: &MacroRules, invocation: &str) -> ast::MacroItems { let expanded = expand(rules, invocation); - token_tree_to_macro_items(&expanded).unwrap().tree() + token_tree_to_items(&expanded).unwrap().tree() } #[allow(unused)] @@ -155,8 +155,8 @@ pub(crate) fn assert_expansion( let expected = text_to_tokentree(&expected); let (expanded_tree, expected_tree) = match kind { MacroKind::Items => { - let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree(); - let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree(); + let expanded_tree = token_tree_to_items(&expanded).unwrap().tree(); + let expected_tree = token_tree_to_items(&expected).unwrap().tree(); ( debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), @@ -410,7 +410,7 @@ fn test_expand_to_item_list() { ", ); let expansion = expand(&rules, "structs!(Foo, Bar);"); - let tree = token_tree_to_macro_items(&expansion).unwrap().tree(); + let tree = token_tree_to_items(&expansion).unwrap().tree(); assert_eq!( format!("{:#?}", tree.syntax()).trim(), r#" -- cgit v1.2.3