Return Result for token_tree_to_xx functions

This commit is contained in:
Edwin Cheng 2019-04-19 04:03:22 +08:00
parent c0f19d7005
commit a6d51e0961
2 changed files with 24 additions and 14 deletions

View file

@ -204,7 +204,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
invocation: &str,
) -> ra_syntax::TreeArc<ast::MacroItems> {
let expanded = expand(rules, invocation);
token_tree_to_macro_items(&expanded)
token_tree_to_macro_items(&expanded).unwrap()
}
pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
@ -218,7 +218,10 @@ impl_froms!(TokenTree: Leaf, Subtree);
let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0;
let file = token_tree_to_macro_items(&expansion);
assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim());
assert_eq!(
tree.unwrap().syntax().debug_dump().trim(),
file.unwrap().syntax().debug_dump().trim()
);
}
#[test]
@ -358,7 +361,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
let expansion = expand(&rules, "structs!(Foo, Bar)");
let tree = token_tree_to_macro_items(&expansion);
assert_eq!(
tree.syntax().debug_dump().trim(),
tree.unwrap().syntax().debug_dump().trim(),
r#"
MACRO_ITEMS@[0; 40)
STRUCT_DEF@[0; 20)
@ -472,7 +475,7 @@ MACRO_ITEMS@[0; 40)
let stmts = token_tree_to_macro_stmts(&expanded);
assert_eq!(
stmts.syntax().debug_dump().trim(),
stmts.unwrap().syntax().debug_dump().trim(),
r#"MACRO_STMTS@[0; 15)
LET_STMT@[0; 7)
LET_KW@[0; 3) "let"

View file

@ -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<ast::Expr> {
pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, 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<ast::Pat> {
pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, 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<ast::TypeRef> {
pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, 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<ast::MacroStmts> {
pub fn token_tree_to_macro_stmts(
tt: &tt::Subtree,
) -> Result<TreeArc<ast::MacroStmts>, 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<ast::MacroItems> {
pub fn token_tree_to_macro_items(
tt: &tt::Subtree,
) -> Result<TreeArc<ast::MacroItems>, 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