Add MacroItems and MacroStmts in grammer.ron

This commit is contained in:
Edwin Cheng 2019-04-19 02:47:29 +08:00
parent 403cd78bae
commit 3ff5440a50
7 changed files with 132 additions and 8 deletions

View file

@ -37,9 +37,15 @@ pub enum ExpandError {
NoMatchingRule,
UnexpectedToken,
BindingError(String),
ConversionError,
}
pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list, syntax_node_to_token_tree};
pub use crate::syntax_bridge::{
ast_to_token_tree,
token_tree_to_ast_item_list,
syntax_node_to_token_tree,
token_tree_to_macro_items,
};
/// This struct contains AST for a single `macro_rules` definition. What might
/// be very confusing is that AST has almost exactly the same shape as
@ -192,21 +198,21 @@ impl_froms!(TokenTree: Leaf, Subtree);
pub(crate) fn expand_to_syntax(
rules: &MacroRules,
invocation: &str,
) -> ra_syntax::TreeArc<ast::SourceFile> {
) -> ra_syntax::TreeArc<ast::MacroItems> {
let expanded = expand(rules, invocation);
token_tree_to_ast_item_list(&expanded)
token_tree_to_macro_items(&expanded)
}
pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
let expanded = expand(rules, invocation);
assert_eq!(expanded.to_string(), expansion);
let tree = token_tree_to_ast_item_list(&expanded);
let tree = token_tree_to_macro_items(&expanded);
// Eat all white space by parse it back and forth
let expansion = ast::SourceFile::parse(expansion);
let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0;
let file = token_tree_to_ast_item_list(&expansion);
let file = token_tree_to_macro_items(&expansion);
assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim());
}
@ -346,11 +352,11 @@ impl_froms!(TokenTree: Leaf, Subtree);
",
);
let expansion = expand(&rules, "structs!(Foo, Bar)");
let tree = token_tree_to_ast_item_list(&expansion);
let tree = token_tree_to_macro_items(&expansion);
assert_eq!(
tree.syntax().debug_dump().trim(),
r#"
SOURCE_FILE@[0; 40)
MACRO_ITEMS@[0; 40)
STRUCT_DEF@[0; 20)
STRUCT_KW@[0; 6) "struct"
NAME@[6; 9)
@ -527,7 +533,7 @@ SOURCE_FILE@[0; 40)
assert_eq!(
expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(),
r#"SOURCE_FILE@[0; 15)
r#"MACRO_ITEMS@[0; 15)
FN_DEF@[0; 15)
FN_KW@[0; 2) "fn"
NAME@[2; 5)

View file

@ -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<ast::Expr>)
// * Pat(P<ast::Pat>)
// * Ty(P<ast::Ty>)
// * Stmts(SmallVec<[ast::Stmt; 1]>)
// * Items(SmallVec<[P<ast::Item>; 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<ast::MacroItems> {
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<ast::SourceFile> {
let token_source = SubtreeTokenSource::new(tt);