]> git.lizzy.rs Git - rust.git/blobdiff - crates/parser/src/grammar.rs
Allow macros to expand to or-patterns
[rust.git] / crates / parser / src / grammar.rs
index 991955fca6d79606d0eda2e88c94c01e48688735..4efbf9a606e1939bcdcd96398465e7cd1f201b5d 100644 (file)
     TokenSet, T,
 };
 
-pub(crate) mod entry_points {
+pub(crate) mod entry {
     use super::*;
 
-    pub(crate) fn source_file(p: &mut Parser) {
-        let m = p.start();
-        p.eat(SHEBANG);
-        items::mod_contents(p, false);
-        m.complete(p, SOURCE_FILE);
-    }
-
-    pub(crate) use expressions::block_expr;
+    pub(crate) mod prefix {
+        use super::*;
 
-    pub(crate) use paths::type_path as path;
+        pub(crate) fn vis(p: &mut Parser) {
+            let _ = opt_visibility(p, false);
+        }
 
-    pub(crate) use patterns::pattern_single as pattern;
+        pub(crate) fn block(p: &mut Parser) {
+            expressions::block_expr(p);
+        }
 
-    pub(crate) use types::type_;
+        pub(crate) fn stmt(p: &mut Parser) {
+            expressions::stmt(p, expressions::Semicolon::Forbidden);
+        }
 
-    pub(crate) fn expr(p: &mut Parser) {
-        let _ = expressions::expr(p);
-    }
+        pub(crate) fn pat(p: &mut Parser) {
+            patterns::pattern_single(p);
+        }
 
-    pub(crate) fn stmt(p: &mut Parser) {
-        expressions::stmt(p, expressions::StmtWithSemi::No, true);
+        pub(crate) fn ty(p: &mut Parser) {
+            types::type_(p);
+        }
+        pub(crate) fn expr(p: &mut Parser) {
+            let _ = expressions::expr(p);
+        }
+        pub(crate) fn path(p: &mut Parser) {
+            let _ = paths::type_path(p);
+        }
+        pub(crate) fn item(p: &mut Parser) {
+            items::item_or_macro(p, true);
+        }
+        // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
+        pub(crate) fn meta_item(p: &mut Parser) {
+            attributes::meta(p);
+        }
     }
 
-    pub(crate) fn stmt_optional_semi(p: &mut Parser) {
-        expressions::stmt(p, expressions::StmtWithSemi::Optional, false);
-    }
+    pub(crate) mod top {
+        use super::*;
 
-    pub(crate) fn visibility(p: &mut Parser) {
-        let _ = opt_visibility(p);
-    }
+        pub(crate) fn source_file(p: &mut Parser) {
+            let m = p.start();
+            p.eat(SHEBANG);
+            items::mod_contents(p, false);
+            m.complete(p, SOURCE_FILE);
+        }
 
-    // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
-    pub(crate) fn meta_item(p: &mut Parser) {
-        attributes::meta(p);
-    }
+        pub(crate) fn macro_stmts(p: &mut Parser) {
+            let m = p.start();
 
-    pub(crate) fn item(p: &mut Parser) {
-        items::item_or_macro(p, true);
-    }
+            while !p.at(EOF) {
+                expressions::stmt(p, expressions::Semicolon::Optional);
+            }
 
-    pub(crate) fn macro_items(p: &mut Parser) {
-        let m = p.start();
-        items::mod_contents(p, false);
-        m.complete(p, MACRO_ITEMS);
-    }
+            m.complete(p, MACRO_STMTS);
+        }
 
-    pub(crate) fn macro_stmts(p: &mut Parser) {
-        let m = p.start();
+        pub(crate) fn macro_items(p: &mut Parser) {
+            let m = p.start();
+            items::mod_contents(p, false);
+            m.complete(p, MACRO_ITEMS);
+        }
 
-        while !p.at(EOF) {
-            if p.at(T![;]) {
-                p.bump(T![;]);
-                continue;
+        pub(crate) fn pattern(p: &mut Parser) {
+            let m = p.start();
+            patterns::pattern_top(p);
+            if p.at(EOF) {
+                m.abandon(p);
+                return;
+            }
+            while !p.at(EOF) {
+                p.bump_any();
             }
+            m.complete(p, ERROR);
+        }
 
-            expressions::stmt(p, expressions::StmtWithSemi::Optional, true);
+        pub(crate) fn type_(p: &mut Parser) {
+            let m = p.start();
+            types::type_(p);
+            if p.at(EOF) {
+                m.abandon(p);
+                return;
+            }
+            while !p.at(EOF) {
+                p.bump_any();
+            }
+            m.complete(p, ERROR);
         }
 
-        m.complete(p, MACRO_STMTS);
-    }
+        pub(crate) fn expr(p: &mut Parser) {
+            let m = p.start();
+            expressions::expr(p);
+            if p.at(EOF) {
+                m.abandon(p);
+                return;
+            }
+            while !p.at(EOF) {
+                p.bump_any();
+            }
+            m.complete(p, ERROR);
+        }
 
-    pub(crate) fn attr(p: &mut Parser) {
-        attributes::outer_attrs(p);
+        pub(crate) fn meta_item(p: &mut Parser) {
+            let m = p.start();
+            attributes::meta(p);
+            if p.at(EOF) {
+                m.abandon(p);
+                return;
+            }
+            while !p.at(EOF) {
+                p.bump_any();
+            }
+            m.complete(p, ERROR);
+        }
     }
 }
 
@@ -149,7 +200,7 @@ fn is_block(self) -> bool {
     }
 }
 
-fn opt_visibility(p: &mut Parser) -> bool {
+fn opt_visibility(p: &mut Parser, in_tuple_field: bool) -> bool {
     match p.current() {
         T![pub] => {
             let m = p.start();
@@ -165,9 +216,17 @@ fn opt_visibility(p: &mut Parser) -> bool {
                     // struct B(pub (super::A));
                     // struct B(pub (crate::A,));
                     T![crate] | T![self] | T![super] | T![ident] if p.nth(2) != T![:] => {
-                        p.bump(T!['(']);
-                        paths::use_path(p);
-                        p.expect(T![')']);
+                        // If we are in a tuple struct, then the parens following `pub`
+                        // might be an tuple field, not part of the visibility. So in that
+                        // case we don't want to consume an identifier.
+
+                        // test pub_tuple_field
+                        // struct MyStruct(pub (u32, u32));
+                        if !(in_tuple_field && matches!(p.nth(1), T![ident])) {
+                            p.bump(T!['(']);
+                            paths::use_path(p);
+                            p.expect(T![')']);
+                        }
                     }
                     // test crate_visibility_in
                     // pub(in super::A) struct S;