about summary refs log tree commit diff
path: root/crates/parser
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-07-30 17:03:51 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-07-30 17:31:26 +0200
commit7c765d9f9ea1a79958ea9f4d29e1d627ee87837a (patch)
treee7e2e9efe6a2a3f1697e19c3891059ba35a4449a /crates/parser
parent712b53865f4130de5e7bb56aeedb9b03a3032745 (diff)
downloadrust-7c765d9f9ea1a79958ea9f4d29e1d627ee87837a.tar.gz
rust-7c765d9f9ea1a79958ea9f4d29e1d627ee87837a.zip
fix: Expand eager macros to delimited comma separated expression list
Diffstat (limited to 'crates/parser')
-rw-r--r--crates/parser/src/grammar.rs34
-rw-r--r--crates/parser/src/lib.rs3
-rw-r--r--crates/parser/src/syntax_kind/generated.rs1
3 files changed, 38 insertions, 0 deletions
diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs
index a868419821d..333318f53b7 100644
--- a/crates/parser/src/grammar.rs
+++ b/crates/parser/src/grammar.rs
@@ -165,6 +165,40 @@ pub(crate) mod entry {
             }
             m.complete(p, ERROR);
         }
+
+        pub(crate) fn eager_macro_input(p: &mut Parser<'_>) {
+            let m = p.start();
+
+            let closing_paren_kind = match p.current() {
+                T!['{'] => T!['}'],
+                T!['('] => T![')'],
+                T!['['] => T![']'],
+                _ => {
+                    p.error("expected `{`, `[`, `(`");
+                    while !p.at(EOF) {
+                        p.bump_any();
+                    }
+                    m.complete(p, ERROR);
+                    return;
+                }
+            };
+            p.bump_any();
+            while !p.at(EOF) && !p.at(closing_paren_kind) {
+                expressions::expr(p);
+                if !p.at(EOF) && !p.at(closing_paren_kind) {
+                    p.expect(T![,]);
+                }
+            }
+            p.expect(closing_paren_kind);
+            if p.at(EOF) {
+                m.complete(p, MACRO_EAGER_INPUT);
+                return;
+            }
+            while !p.at(EOF) {
+                p.bump_any();
+            }
+            m.complete(p, ERROR);
+        }
     }
 }
 
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index 1aba1f7674f..c155e8aaf67 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -75,6 +75,8 @@ pub enum TopEntryPoint {
     /// Edge case -- macros generally don't expand to attributes, with the
     /// exception of `cfg_attr` which does!
     MetaItem,
+    /// Edge case 2 -- eager macros expand their input to a delimited list of comma separated expressions
+    MacroEagerInput,
 }
 
 impl TopEntryPoint {
@@ -87,6 +89,7 @@ impl TopEntryPoint {
             TopEntryPoint::Type => grammar::entry::top::type_,
             TopEntryPoint::Expr => grammar::entry::top::expr,
             TopEntryPoint::MetaItem => grammar::entry::top::meta_item,
+            TopEntryPoint::MacroEagerInput => grammar::entry::top::eager_macro_input,
         };
         let mut p = parser::Parser::new(input);
         entry_point(&mut p);
diff --git a/crates/parser/src/syntax_kind/generated.rs b/crates/parser/src/syntax_kind/generated.rs
index a8fbcfacf7e..48f407623d8 100644
--- a/crates/parser/src/syntax_kind/generated.rs
+++ b/crates/parser/src/syntax_kind/generated.rs
@@ -262,6 +262,7 @@ pub enum SyntaxKind {
     TYPE_BOUND_LIST,
     MACRO_ITEMS,
     MACRO_STMTS,
+    MACRO_EAGER_INPUT,
     #[doc(hidden)]
     __LAST,
 }