From 816147c4b508117fad4ca4d3cb6d77a3c4a683a9 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 25 May 2019 21:55:46 +0800 Subject: Simplify token_tree_to_xxx --- crates/ra_mbe/src/syntax_bridge.rs | 67 ++++++++++++-------------------------- 1 file changed, 20 insertions(+), 47 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 0aab5ea8b..6265fe462 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -45,18 +45,29 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke // // -/// Parses the token tree (result of macro expansion) to an expression -pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result, ExpandError> { +fn token_tree_to_syntax_node(tt: &tt::Subtree, f: F) -> Result, ExpandError> +where + F: Fn(&mut ra_parser::TokenSource, &mut ra_parser::TreeSink), +{ let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); let mut token_source = SubtreeTokenSource::new(&buffer); let querier = token_source.querier(); let mut tree_sink = TtTreeSink::new(querier.as_ref()); - ra_parser::parse_expr(&mut token_source, &mut tree_sink); + f(&mut token_source, &mut tree_sink); + if tree_sink.roots.len() != 1 { + return Err(ExpandError::ConversionError); + } + if tree_sink.roots.len() != 1 { return Err(ExpandError::ConversionError); } - let syntax = tree_sink.inner.finish(); + Ok(tree_sink.inner.finish()) +} + +/// 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) @@ -64,30 +75,13 @@ pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result, Expand /// Parses the token tree (result of macro expansion) to a Pattern pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result, ExpandError> { - let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); - let mut token_source = SubtreeTokenSource::new(&buffer); - let querier = token_source.querier(); - let mut tree_sink = TtTreeSink::new(querier.as_ref()); - ra_parser::parse_pat(&mut token_source, &mut tree_sink); - if tree_sink.roots.len() != 1 { - return Err(ExpandError::ConversionError); - } - - let syntax = tree_sink.inner.finish(); + 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) } /// Parses the token tree (result of macro expansion) to a Type pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result, ExpandError> { - let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); - let mut token_source = SubtreeTokenSource::new(&buffer); - let querier = token_source.querier(); - let mut tree_sink = TtTreeSink::new(querier.as_ref()); - ra_parser::parse_ty(&mut token_source, &mut tree_sink); - if tree_sink.roots.len() != 1 { - return Err(ExpandError::ConversionError); - } - let syntax = tree_sink.inner.finish(); + 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) } @@ -95,15 +89,7 @@ pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result, Expan pub fn token_tree_to_macro_stmts( tt: &tt::Subtree, ) -> Result, ExpandError> { - let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); - let mut token_source = SubtreeTokenSource::new(&buffer); - let querier = token_source.querier(); - let mut tree_sink = TtTreeSink::new(querier.as_ref()); - ra_parser::parse_macro_stmts(&mut token_source, &mut tree_sink); - if tree_sink.roots.len() != 1 { - return Err(ExpandError::ConversionError); - } - let syntax = tree_sink.inner.finish(); + 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) } @@ -111,26 +97,13 @@ pub fn token_tree_to_macro_stmts( pub fn token_tree_to_macro_items( tt: &tt::Subtree, ) -> Result, ExpandError> { - let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); - let mut token_source = SubtreeTokenSource::new(&buffer); - let querier = token_source.querier(); - let mut tree_sink = TtTreeSink::new(querier.as_ref()); - ra_parser::parse_macro_items(&mut token_source, &mut tree_sink); - if tree_sink.roots.len() != 1 { - return Err(ExpandError::ConversionError); - } - let syntax = tree_sink.inner.finish(); + 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) } /// 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 buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); - let mut token_source = SubtreeTokenSource::new(&buffer); - let querier = token_source.querier(); - let mut tree_sink = TtTreeSink::new(querier.as_ref()); - ra_parser::parse(&mut token_source, &mut tree_sink); - let syntax = tree_sink.inner.finish(); + let syntax = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap(); ast::SourceFile::cast(&syntax).unwrap().to_owned() } -- cgit v1.2.3