]> git.lizzy.rs Git - rust.git/commitdiff
Add MacroItems and MacroStmts in grammer.ron
authorEdwin Cheng <edwin0cheng@gmail.com>
Thu, 18 Apr 2019 18:47:29 +0000 (02:47 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Thu, 18 Apr 2019 18:47:29 +0000 (02:47 +0800)
crates/ra_mbe/src/lib.rs
crates/ra_mbe/src/syntax_bridge.rs
crates/ra_parser/src/grammar.rs
crates/ra_parser/src/lib.rs
crates/ra_parser/src/syntax_kind/generated.rs
crates/ra_syntax/src/ast/generated.rs
crates/ra_syntax/src/grammar.ron

index 2f47e32d3b72352bb2169ce60cf2cfee08913f3c..4cfa1f95537ea17b0a819dda09dd2498222c971e 100644 (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 @@ pub(crate) fn expand(rules: &MacroRules, invocation: &str) -> tt::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 @@ macro_rules! structs {
             ",
         );
         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 @@ macro_rules! foo {
 
         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)
index 28ded7870e1309c64696ac19e5c9eaa9ad829d3f..19e09be80ed68b04986b486a02b20364244c620d 100644 (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);
index f8ed1299a90f209c4035d5eef8a80ff8f57b9dab..1adc27b80f09e251bb11fc219ecfb4d8b32edb20 100644 (file)
@@ -49,6 +49,12 @@ pub(crate) fn root(p: &mut Parser) {
     m.complete(p, SOURCE_FILE);
 }
 
+pub(crate) fn macro_items(p: &mut Parser) {
+    let m = p.start();
+    items::mod_contents(p, false);
+    m.complete(p, MACRO_ITEMS);
+}
+
 pub(crate) fn path(p: &mut Parser) {
     paths::type_path(p);
 }
index 11b5b9a75c6a92c86d8a38f3f27124719db1d239..d6bcc4d8cd657af2e5098b3465ffc8ba30c345ec 100644 (file)
@@ -98,6 +98,10 @@ pub fn parse_item(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink)
     parse_from_tokens(token_source, tree_sink, grammar::item);
 }
 
+pub fn parse_macro_items(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
+    parse_from_tokens(token_source, tree_sink, grammar::macro_items);
+}
+
 /// A parsing function for a specific braced-block.
 pub struct Reparser(fn(&mut parser::Parser));
 
index 498b0e164a660da6c405db0be27558b5d78478ac..6f984aea16f4b03cbd2d3cd858e97c6e049669f8 100644 (file)
@@ -233,6 +233,8 @@ pub enum SyntaxKind {
     ARG_LIST,
     TYPE_BOUND,
     TYPE_BOUND_LIST,
+    MACRO_ITEMS,
+    MACRO_STMTS,
     // Technical kind so that we can cast from u16 safely
     #[doc(hidden)]
     __LAST,
@@ -592,6 +594,8 @@ pub(crate) fn info(self) -> &'static SyntaxInfo {
             ARG_LIST => &SyntaxInfo { name: "ARG_LIST" },
             TYPE_BOUND => &SyntaxInfo { name: "TYPE_BOUND" },
             TYPE_BOUND_LIST => &SyntaxInfo { name: "TYPE_BOUND_LIST" },
+            MACRO_ITEMS => &SyntaxInfo { name: "MACRO_ITEMS" },
+            MACRO_STMTS => &SyntaxInfo { name: "MACRO_STMTS" },
             TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" },
             EOF => &SyntaxInfo { name: "EOF" },
             __LAST => &SyntaxInfo { name: "__LAST" },
index 774d9bcc84887bfba3dd8fb37657adf0594cfcfa..17de4f058081d13a74478a158ace4d49bc800399 100644 (file)
@@ -1770,6 +1770,72 @@ pub fn path(&self) -> Option<&Path> {
     }
 }
 
+// MacroItems
+#[derive(Debug, PartialEq, Eq, Hash)]
+#[repr(transparent)]
+pub struct MacroItems {
+    pub(crate) syntax: SyntaxNode,
+}
+unsafe impl TransparentNewType for MacroItems {
+    type Repr = rowan::SyntaxNode;
+}
+
+impl AstNode for MacroItems {
+    fn cast(syntax: &SyntaxNode) -> Option<&Self> {
+        match syntax.kind() {
+            MACRO_ITEMS => Some(MacroItems::from_repr(syntax.into_repr())),
+            _ => None,
+        }
+    }
+    fn syntax(&self) -> &SyntaxNode { &self.syntax }
+}
+
+impl ToOwned for MacroItems {
+    type Owned = TreeArc<MacroItems>;
+    fn to_owned(&self) -> TreeArc<MacroItems> { TreeArc::cast(self.syntax.to_owned()) }
+}
+
+
+impl ast::ModuleItemOwner for MacroItems {}
+impl ast::FnDefOwner for MacroItems {}
+impl MacroItems {}
+
+// MacroStmts
+#[derive(Debug, PartialEq, Eq, Hash)]
+#[repr(transparent)]
+pub struct MacroStmts {
+    pub(crate) syntax: SyntaxNode,
+}
+unsafe impl TransparentNewType for MacroStmts {
+    type Repr = rowan::SyntaxNode;
+}
+
+impl AstNode for MacroStmts {
+    fn cast(syntax: &SyntaxNode) -> Option<&Self> {
+        match syntax.kind() {
+            MACRO_STMTS => Some(MacroStmts::from_repr(syntax.into_repr())),
+            _ => None,
+        }
+    }
+    fn syntax(&self) -> &SyntaxNode { &self.syntax }
+}
+
+impl ToOwned for MacroStmts {
+    type Owned = TreeArc<MacroStmts>;
+    fn to_owned(&self) -> TreeArc<MacroStmts> { TreeArc::cast(self.syntax.to_owned()) }
+}
+
+
+impl MacroStmts {
+    pub fn statements(&self) -> impl Iterator<Item = &Stmt> {
+        super::children(self)
+    }
+
+    pub fn expr(&self) -> Option<&Expr> {
+        super::child_opt(self)
+    }
+}
+
 // MatchArm
 #[derive(Debug, PartialEq, Eq, Hash)]
 #[repr(transparent)]
index b412412879b27adda7ac3d73cf72ca7c9e23be19..663e3a2f9ec02602571d09b3c6951356540ce1b0 100644 (file)
@@ -247,6 +247,10 @@ Grammar(
         "ARG_LIST",
         "TYPE_BOUND",
         "TYPE_BOUND_LIST",
+
+        // macro related
+        "MACRO_ITEMS",
+        "MACRO_STMTS",
     ],
     ast: {
         "SourceFile": (
@@ -668,5 +672,16 @@ Grammar(
         "TypeArg": (options: ["TypeRef"]),
         "AssocTypeArg": (options: ["NameRef", "TypeRef"]),
         "LifetimeArg": (),
+
+        "MacroItems": (
+            traits: [ "ModuleItemOwner", "FnDefOwner" ],            
+        ),
+
+        "MacroStmts" : (
+            options: [ "Expr" ],
+            collections: [
+                ["statements", "Stmt"],
+            ],
+        )
     },
 )