From df33e7685bdb0f63bf6aa809b9046708d563a1a7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 18 Jul 2019 23:19:04 +0300 Subject: use Parse in mbe --- crates/ra_mbe/src/syntax_bridge.rs | 52 +++++++++++++++++--------------------- crates/ra_mbe/src/tests.rs | 30 +++++++++++----------- 2 files changed, 38 insertions(+), 44 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 7560d215a..bfc351f81 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -2,8 +2,8 @@ use crate::subtree_source::SubtreeTokenSource; use crate::ExpandError; use ra_parser::{ParseError, TreeSink}; use ra_syntax::{ - ast, AstNode, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxTreeBuilder, - TextRange, TextUnit, TreeArc, T, + ast, AstNode, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode, + SyntaxTreeBuilder, TextRange, TextUnit, T, }; use tt::buffer::{Cursor, TokenBuffer}; @@ -45,7 +45,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke // // -fn token_tree_to_syntax_node(tt: &tt::Subtree, f: F) -> Result, ExpandError> +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), { @@ -58,50 +58,44 @@ where return Err(ExpandError::ConversionError); } //FIXME: would be cool to report errors - let (tree, _errors) = tree_sink.inner.finish(); - Ok(tree) + let parse = tree_sink.inner.finish(); + Ok(parse) } /// Parses the token tree (result of macro expansion) to an expression -pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result, ExpandError> { - let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?; - ast::Expr::cast(&syntax) - .map(|m| m.to_owned()) - .ok_or_else(|| crate::ExpandError::ConversionError) +pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result, ExpandError> { + let parse = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?; + parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) } /// Parses the token tree (result of macro expansion) to a Pattern -pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result, ExpandError> { - let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?; - ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) +pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result, ExpandError> { + let parse = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?; + parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) } /// Parses the token tree (result of macro expansion) to a Type -pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result, ExpandError> { - let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?; - ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) +pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result, ExpandError> { + let parse = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?; + parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) } /// Parses the token tree (result of macro expansion) as a sequence of stmts -pub fn token_tree_to_macro_stmts( - tt: &tt::Subtree, -) -> Result, ExpandError> { - let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?; - ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) +pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> Result, ExpandError> { + let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?; + 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_macro_items( - tt: &tt::Subtree, -) -> Result, ExpandError> { - let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?; - ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) +pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> Result, ExpandError> { + let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_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) -> TreeArc { - let syntax = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap(); - ast::SourceFile::cast(&syntax).unwrap().to_owned() +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 { diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 5a1494fee..419b2c099 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs @@ -72,7 +72,7 @@ pub(crate) fn expand_to_items( invocation: &str, ) -> ra_syntax::TreeArc { let expanded = expand(rules, invocation); - token_tree_to_macro_items(&expanded).unwrap() + token_tree_to_macro_items(&expanded).unwrap().tree().to_owned() } #[allow(unused)] @@ -81,7 +81,7 @@ pub(crate) fn expand_to_stmts( invocation: &str, ) -> ra_syntax::TreeArc { let expanded = expand(rules, invocation); - token_tree_to_macro_stmts(&expanded).unwrap() + token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned() } pub(crate) fn expand_to_expr( @@ -89,7 +89,7 @@ pub(crate) fn expand_to_expr( invocation: &str, ) -> ra_syntax::TreeArc { let expanded = expand(rules, invocation); - token_tree_to_expr(&expanded).unwrap() + token_tree_to_expr(&expanded).unwrap().tree().to_owned() } pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { @@ -164,22 +164,22 @@ pub(crate) fn assert_expansion( let (expanded_tree, expected_tree) = match kind { MacroKind::Items => { - let expanded_tree = token_tree_to_macro_items(&expanded); - let expected_tree = token_tree_to_macro_items(&expected); + let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree().to_owned(); + let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree().to_owned(); ( - debug_dump_ignore_spaces(expanded_tree.unwrap().syntax()).trim().to_string(), - debug_dump_ignore_spaces(expected_tree.unwrap().syntax()).trim().to_string(), + debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), + debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(), ) } MacroKind::Stmts => { - let expanded_tree = token_tree_to_macro_stmts(&expanded); - let expected_tree = token_tree_to_macro_stmts(&expected); + let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned(); + let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree().to_owned(); ( - debug_dump_ignore_spaces(expanded_tree.unwrap().syntax()).trim().to_string(), - debug_dump_ignore_spaces(expected_tree.unwrap().syntax()).trim().to_string(), + debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(), + debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(), ) } }; @@ -419,9 +419,9 @@ fn test_expand_to_item_list() { ", ); let expansion = expand(&rules, "structs!(Foo, Bar);"); - let tree = token_tree_to_macro_items(&expansion); + let tree = token_tree_to_macro_items(&expansion).unwrap().tree().to_owned(); assert_eq!( - tree.unwrap().syntax().debug_dump().trim(), + tree.syntax().debug_dump().trim(), r#" MACRO_ITEMS@[0; 40) STRUCT_DEF@[0; 20) @@ -537,10 +537,10 @@ fn test_tt_to_stmts() { ); let expanded = expand(&rules, "foo!{}"); - let stmts = token_tree_to_macro_stmts(&expanded); + let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned(); assert_eq!( - stmts.unwrap().syntax().debug_dump().trim(), + stmts.syntax().debug_dump().trim(), r#"MACRO_STMTS@[0; 15) LET_STMT@[0; 7) LET_KW@[0; 3) "let" -- cgit v1.2.3