From 3ff5440a503f090032136c37c3d44375d6107db1 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 19 Apr 2019 02:47:29 +0800 Subject: Add MacroItems and MacroStmts in grammer.ron --- crates/ra_mbe/src/syntax_bridge.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'crates/ra_mbe/src/syntax_bridge.rs') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 28ded7870..19e09be80 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -30,6 +30,29 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke Some((tt, token_map)) } +// The following items are what `rustc` macro can be parsed into : +// link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141 +// * Expr(P) +// * Pat(P) +// * Ty(P) +// * Stmts(SmallVec<[ast::Stmt; 1]>) +// * Items(SmallVec<[P; 1]>) +// +// * TraitItems(SmallVec<[ast::TraitItem; 1]>) +// * ImplItems(SmallVec<[ast::ImplItem; 1]>) +// * ForeignItems(SmallVec<[ast::ForeignItem; 1]> +// +// + +/// Parses the token tree (result of macro expansion) as a sequence of items +pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> TreeArc { + let token_source = SubtreeTokenSource::new(tt); + let mut tree_sink = TtTreeSink::new(token_source.querier()); + ra_parser::parse_macro_items(&token_source, &mut tree_sink); + let syntax = tree_sink.inner.finish(); + ast::MacroItems::cast(&syntax).unwrap().to_owned() +} + /// 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 token_source = SubtreeTokenSource::new(tt); -- cgit v1.2.3 From c0f19d70056fada5f381019694d893e0ffe8360a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 19 Apr 2019 03:49:56 +0800 Subject: Add expr, pat, ty and macro_stmts --- crates/ra_mbe/src/syntax_bridge.rs | 46 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'crates/ra_mbe/src/syntax_bridge.rs') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 19e09be80..6af3b1995 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -32,11 +32,11 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke // The following items are what `rustc` macro can be parsed into : // link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141 -// * Expr(P) -// * Pat(P) -// * Ty(P) -// * Stmts(SmallVec<[ast::Stmt; 1]>) -// * Items(SmallVec<[P; 1]>) +// * Expr(P) -> token_tree_to_expr +// * Pat(P) -> token_tree_to_pat +// * Ty(P) -> token_tree_to_ty +// * Stmts(SmallVec<[ast::Stmt; 1]>) -> token_tree_to_stmts +// * Items(SmallVec<[P; 1]>) -> token_tree_to_items // // * TraitItems(SmallVec<[ast::TraitItem; 1]>) // * ImplItems(SmallVec<[ast::ImplItem; 1]>) @@ -44,6 +44,42 @@ 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) -> TreeArc { + let token_source = SubtreeTokenSource::new(tt); + let mut tree_sink = TtTreeSink::new(token_source.querier()); + ra_parser::parse_expr(&token_source, &mut tree_sink); + let syntax = tree_sink.inner.finish(); + ast::Expr::cast(&syntax).unwrap().to_owned() +} + +/// Parses the token tree (result of macro expansion) to a Pattern +pub fn token_tree_to_pat(tt: &tt::Subtree) -> TreeArc { + let token_source = SubtreeTokenSource::new(tt); + let mut tree_sink = TtTreeSink::new(token_source.querier()); + ra_parser::parse_pat(&token_source, &mut tree_sink); + let syntax = tree_sink.inner.finish(); + ast::Pat::cast(&syntax).unwrap().to_owned() +} + +/// Parses the token tree (result of macro expansion) to a Type +pub fn token_tree_to_ty(tt: &tt::Subtree) -> TreeArc { + let token_source = SubtreeTokenSource::new(tt); + let mut tree_sink = TtTreeSink::new(token_source.querier()); + ra_parser::parse_ty(&token_source, &mut tree_sink); + let syntax = tree_sink.inner.finish(); + ast::TypeRef::cast(&syntax).unwrap().to_owned() +} + +/// Parses the token tree (result of macro expansion) as a sequence of stmts +pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> TreeArc { + let token_source = SubtreeTokenSource::new(tt); + let mut tree_sink = TtTreeSink::new(token_source.querier()); + ra_parser::parse_macro_stmts(&token_source, &mut tree_sink); + let syntax = tree_sink.inner.finish(); + ast::MacroStmts::cast(&syntax).unwrap().to_owned() +} + /// Parses the token tree (result of macro expansion) as a sequence of items pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> TreeArc { let token_source = SubtreeTokenSource::new(tt); -- cgit v1.2.3 From a6d51e09610989821aaf79871bcab0661c9b0f74 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 19 Apr 2019 04:03:22 +0800 Subject: Return Result for token_tree_to_xx functions --- crates/ra_mbe/src/syntax_bridge.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'crates/ra_mbe/src/syntax_bridge.rs') diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 6af3b1995..5844d3f12 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -5,6 +5,7 @@ use ra_syntax::{ }; use crate::subtree_source::{SubtreeTokenSource, Querier}; +use crate::ExpandError; /// Maps `tt::TokenId` to the relative range of the original token. #[derive(Default)] @@ -45,48 +46,54 @@ 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) -> TreeArc { +pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result, ExpandError> { let token_source = SubtreeTokenSource::new(tt); let mut tree_sink = TtTreeSink::new(token_source.querier()); ra_parser::parse_expr(&token_source, &mut tree_sink); let syntax = tree_sink.inner.finish(); - ast::Expr::cast(&syntax).unwrap().to_owned() + ast::Expr::cast(&syntax) + .map(|m| m.to_owned()) + .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) -> TreeArc { +pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result, ExpandError> { let token_source = SubtreeTokenSource::new(tt); let mut tree_sink = TtTreeSink::new(token_source.querier()); ra_parser::parse_pat(&token_source, &mut tree_sink); let syntax = tree_sink.inner.finish(); - ast::Pat::cast(&syntax).unwrap().to_owned() + 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) -> TreeArc { +pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result, ExpandError> { let token_source = SubtreeTokenSource::new(tt); let mut tree_sink = TtTreeSink::new(token_source.querier()); ra_parser::parse_ty(&token_source, &mut tree_sink); let syntax = tree_sink.inner.finish(); - ast::TypeRef::cast(&syntax).unwrap().to_owned() + ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| 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) -> TreeArc { +pub fn token_tree_to_macro_stmts( + tt: &tt::Subtree, +) -> Result, ExpandError> { let token_source = SubtreeTokenSource::new(tt); let mut tree_sink = TtTreeSink::new(token_source.querier()); ra_parser::parse_macro_stmts(&token_source, &mut tree_sink); let syntax = tree_sink.inner.finish(); - ast::MacroStmts::cast(&syntax).unwrap().to_owned() + ast::MacroStmts::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_macro_items(tt: &tt::Subtree) -> TreeArc { +pub fn token_tree_to_macro_items( + tt: &tt::Subtree, +) -> Result, ExpandError> { let token_source = SubtreeTokenSource::new(tt); let mut tree_sink = TtTreeSink::new(token_source.querier()); ra_parser::parse_macro_items(&token_source, &mut tree_sink); let syntax = tree_sink.inner.finish(); - ast::MacroItems::cast(&syntax).unwrap().to_owned() + 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 -- cgit v1.2.3