]> 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 53a20cd124915014199d5dd909ba648bf4cf0675..4efbf9a606e1939bcdcd96398465e7cd1f201b5d 100644 (file)
@@ -6,9 +6,9 @@
 //! each submodule starts with `use super::*` import and exports
 //! "public" productions via `pub(super)`.
 //!
-//! See docs for `Parser` to learn about API, available to the grammar,
-//! and see docs for `Event` to learn how this actually manages to
-//! produce parse trees.
+//! See docs for [`Parser`](super::parser::Parser) to learn about API,
+//! available to the grammar, and see docs for [`Event`](super::event::Event)
+//! to learn how this actually manages to produce parse trees.
 //!
 //! Code in this module also contains inline tests, which start with
 //! `// test name-of-the-test` comment and look like this:
 mod params;
 mod paths;
 mod patterns;
-mod type_args;
-mod type_params;
+mod generic_args;
+mod generic_params;
 mod types;
 
 use crate::{
     parser::{CompletedMarker, Marker, Parser},
     SyntaxKind::{self, *},
-    TokenSet,
+    TokenSet, T,
 };
 
-pub(crate) fn root(p: &mut Parser) {
-    let m = p.start();
-    p.eat(SHEBANG);
-    items::mod_contents(p, false);
-    m.complete(p, SOURCE_FILE);
-}
-
-/// Various pieces of syntax that can be parsed by macros by example
-pub(crate) mod fragments {
+pub(crate) mod entry {
     use super::*;
 
-    pub(crate) use super::{
-        expressions::block_expr, paths::type_path as path, patterns::pattern_single, types::type_,
-    };
+    pub(crate) mod prefix {
+        use super::*;
 
-    pub(crate) fn expr(p: &mut Parser) {
-        let _ = expressions::expr_with_attrs(p);
-    }
+        pub(crate) fn vis(p: &mut Parser) {
+            let _ = opt_visibility(p, false);
+        }
 
-    pub(crate) fn stmt(p: &mut Parser) {
-        expressions::stmt(p, expressions::StmtWithSemi::No, true)
-    }
+        pub(crate) fn block(p: &mut Parser) {
+            expressions::block_expr(p);
+        }
 
-    pub(crate) fn stmt_optional_semi(p: &mut Parser) {
-        expressions::stmt(p, expressions::StmtWithSemi::Optional, false)
-    }
+        pub(crate) fn stmt(p: &mut Parser) {
+            expressions::stmt(p, expressions::Semicolon::Forbidden);
+        }
 
-    pub(crate) fn opt_visibility(p: &mut Parser) {
-        let _ = super::opt_visibility(p);
-    }
+        pub(crate) fn pat(p: &mut Parser) {
+            patterns::pattern_single(p);
+        }
 
-    // Parse a meta item , which excluded [], e.g : #[ MetaItem ]
-    pub(crate) fn meta_item(p: &mut Parser) {
-        attributes::meta(p);
+        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 item(p: &mut Parser) {
-        items::item_or_macro(p, true)
-    }
+    pub(crate) mod top {
+        use super::*;
 
-    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 source_file(p: &mut Parser) {
+            let m = p.start();
+            p.eat(SHEBANG);
+            items::mod_contents(p, false);
+            m.complete(p, SOURCE_FILE);
+        }
 
-    pub(crate) fn macro_stmts(p: &mut Parser) {
-        let m = p.start();
+        pub(crate) fn macro_stmts(p: &mut Parser) {
+            let m = p.start();
 
-        while !p.at(EOF) {
-            if p.at(T![;]) {
-                p.bump(T![;]);
-                continue;
+            while !p.at(EOF) {
+                expressions::stmt(p, expressions::Semicolon::Optional);
             }
 
-            expressions::stmt(p, expressions::StmtWithSemi::Optional, true);
+            m.complete(p, MACRO_STMTS);
         }
 
-        m.complete(p, MACRO_STMTS);
-    }
+        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 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);
+        }
+
+        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);
+        }
 
-    pub(crate) fn attr(p: &mut Parser) {
-        attributes::outer_attrs(p)
+        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 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);
+        }
     }
 }
 
@@ -125,8 +179,7 @@ pub(crate) fn reparser(
         EXTERN_ITEM_LIST => items::extern_item_list,
         TOKEN_TREE if first_child? == T!['{'] => items::token_tree,
         ASSOC_ITEM_LIST => match parent? {
-            IMPL => items::assoc_item_list,
-            TRAIT => items::assoc_item_list,
+            IMPL | TRAIT => items::assoc_item_list,
             _ => return None,
         },
         ITEM_LIST => items::item_list,
@@ -147,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();
@@ -163,22 +216,24 @@ 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_any();
-                        let path_m = p.start();
-                        let path_segment_m = p.start();
-                        let name_ref_m = p.start();
-                        p.bump_any();
-                        name_ref_m.complete(p, NAME_REF);
-                        path_segment_m.complete(p, PATH_SEGMENT);
-                        path_m.complete(p, PATH);
-                        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;
                     // pub(in crate) struct S;
                     T![in] => {
-                        p.bump_any();
-                        p.bump_any();
+                        p.bump(T!['(']);
+                        p.bump(T![in]);
                         paths::use_path(p);
                         p.expect(T![')']);
                     }
@@ -186,22 +241,25 @@ fn opt_visibility(p: &mut Parser) -> bool {
                 }
             }
             m.complete(p, VISIBILITY);
+            true
         }
         // test crate_keyword_vis
         // crate fn main() { }
         // struct S { crate field: u32 }
         // struct T(crate u32);
-        //
-        // test crate_keyword_path
-        // fn foo() { crate::foo(); }
-        T![crate] if !p.nth_at(1, T![::]) => {
+        T![crate] => {
+            if p.nth_at(1, T![::]) {
+                // test crate_keyword_path
+                // fn foo() { crate::foo(); }
+                return false;
+            }
             let m = p.start();
             p.bump(T![crate]);
             m.complete(p, VISIBILITY);
+            true
         }
-        _ => return false,
+        _ => false,
     }
-    true
 }
 
 fn opt_rename(p: &mut Parser) {
@@ -246,7 +304,7 @@ fn name_r(p: &mut Parser, recovery: TokenSet) {
 }
 
 fn name(p: &mut Parser) {
-    name_r(p, TokenSet::EMPTY)
+    name_r(p, TokenSet::EMPTY);
 }
 
 fn name_ref(p: &mut Parser) {