Skip to content

Commit

Permalink
migrate mbe to the new rowan
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jul 18, 2019
1 parent 4627a6b commit 229726e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
6 changes: 4 additions & 2 deletions crates/ra_mbe/src/mbe_expander.rs
Expand Up @@ -599,7 +599,8 @@ mod tests {
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let (definition_tt, _) =
ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
crate::MacroRules::parse(&definition_tt).unwrap()
}

Expand All @@ -611,7 +612,8 @@ mod tests {
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let (invocation_tt, _) =
ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();

expand_rule(&rules.rules[0], &invocation_tt)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/ra_mbe/src/mbe_parser.rs
Expand Up @@ -179,7 +179,8 @@ mod tests {
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let (definition_tt, _) =
ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
parse(&definition_tt)
}

Expand Down
11 changes: 4 additions & 7 deletions crates/ra_mbe/src/syntax_bridge.rs
Expand Up @@ -2,7 +2,7 @@ use crate::subtree_source::SubtreeTokenSource;
use crate::ExpandError;
use ra_parser::{ParseError, TreeSink};
use ra_syntax::{
ast, AstNode, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode,
ast, AstNode, AstToken, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode,
SyntaxTreeBuilder, TextRange, TextUnit, T,
};
use tt::buffer::{Cursor, TokenBuffer};
Expand Down Expand Up @@ -116,8 +116,6 @@ impl TokenMap {
/// and strips the ending `*/`
/// And then quote the string, which is needed to convert to `tt::Literal`
fn doc_comment_text(comment: &ast::Comment) -> SmolStr {
use ast::AstToken;

let prefix_len = comment.prefix().len();
let mut text = &comment.text()[prefix_len..];

Expand All @@ -132,9 +130,8 @@ fn doc_comment_text(comment: &ast::Comment) -> SmolStr {
text.into()
}

fn convert_doc_comment<'a>(token: &ra_syntax::SyntaxToken<'a>) -> Option<Vec<tt::TokenTree>> {
use ast::AstToken;
let comment = ast::Comment::cast(*token)?;
fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTree>> {
let comment = ast::Comment::cast(token.clone())?;
let doc = comment.kind().doc?;

// Make `doc="\" Comments\""
Expand Down Expand Up @@ -245,7 +242,7 @@ fn convert_tt(
}
}
SyntaxElement::Node(node) => {
let child = convert_tt(token_map, global_offset, node)?.into();
let child = convert_tt(token_map, global_offset, &node)?.into();
token_trees.push(child);
}
};
Expand Down
43 changes: 17 additions & 26 deletions crates/ra_mbe/src/tests.rs
Expand Up @@ -37,8 +37,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();
let rules = crate::MacroRules::parse(&definition_tt).unwrap();
let expansion = rules.expand(&invocation_tt).unwrap();
assert_eq!(
Expand All @@ -53,7 +53,7 @@ pub(crate) fn create_rules(macro_definition: &str) -> MacroRules {
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
crate::MacroRules::parse(&definition_tt).unwrap()
}

Expand All @@ -62,42 +62,33 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree {
let macro_invocation =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();

let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap()).unwrap();

rules.expand(&invocation_tt).unwrap()
}

pub(crate) fn expand_to_items(
rules: &MacroRules,
invocation: &str,
) -> ra_syntax::TreeArc<ast::MacroItems> {
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().to_owned()
token_tree_to_macro_items(&expanded).unwrap().tree()
}

#[allow(unused)]
pub(crate) fn expand_to_stmts(
rules: &MacroRules,
invocation: &str,
) -> ra_syntax::TreeArc<ast::MacroStmts> {
pub(crate) fn expand_to_stmts(rules: &MacroRules, invocation: &str) -> ast::MacroStmts {
let expanded = expand(rules, invocation);
token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned()
token_tree_to_macro_stmts(&expanded).unwrap().tree()
}

pub(crate) fn expand_to_expr(
rules: &MacroRules,
invocation: &str,
) -> ra_syntax::TreeArc<ast::Expr> {
pub(crate) fn expand_to_expr(rules: &MacroRules, invocation: &str) -> ast::Expr {
let expanded = expand(rules, invocation);
token_tree_to_expr(&expanded).unwrap().tree().to_owned()
token_tree_to_expr(&expanded).unwrap().tree()
}

pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree {
// wrap the given text to a macro call
let wrapped = format!("wrap_macro!( {} )", text);
let wrapped = ast::SourceFile::parse(&wrapped);
let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
let mut wrapped = ast_to_token_tree(wrapped).unwrap().0;
let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0;
wrapped.delimiter = tt::Delimiter::None;

wrapped
Expand Down Expand Up @@ -164,8 +155,8 @@ pub(crate) fn assert_expansion(

let (expanded_tree, expected_tree) = match kind {
MacroKind::Items => {
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();
let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree();
let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree();

(
debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(),
Expand All @@ -174,8 +165,8 @@ pub(crate) fn assert_expansion(
}

MacroKind::Stmts => {
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();
let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree();
let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree();

(
debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(),
Expand Down Expand Up @@ -419,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().to_owned();
let tree = token_tree_to_macro_items(&expansion).unwrap().tree();
assert_eq!(
tree.syntax().debug_dump().trim(),
r#"
Expand Down Expand Up @@ -537,7 +528,7 @@ fn test_tt_to_stmts() {
);

let expanded = expand(&rules, "foo!{}");
let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned();
let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree();

assert_eq!(
stmts.syntax().debug_dump().trim(),
Expand Down

0 comments on commit 229726e

Please sign in to comment.