]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Allow macro named macro_rules
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 10 Oct 2023 13:46:48 +0000 (15:46 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:09:17 +0000 (19:09 +0100)
Change the constraints around macro rules declaration in order to allow
macro_rules named macro as well as tighter constraint around macro rules
definitions.

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h (Parser::is_macro_rules_def): Add a function
that checks tokens given by the lexer represents an accurate macro
definition. This will reduce code duplication.
(Parser::parse_item): Replace condition with call to new checking
function.
(Parser::parse_stmt): Likewise.
* parse/rust-parse.h: Add function prototype for is_macro_rules_def.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/parse/rust-parse-impl.h
gcc/rust/parse/rust-parse.h

index 63dea9836df8eab97a03bd049e0c199f11997960..71f76f8e2eb6d72a035fb86d336bc3d8706399de 100644 (file)
@@ -1071,6 +1071,18 @@ Parser<ManagedTokenSource>::parse_token_tree ()
     }
 }
 
+template <typename ManagedTokenSource>
+bool
+Parser<ManagedTokenSource>::is_macro_rules_def (const_TokenPtr t)
+{
+  auto macro_name = lexer.peek_token (2)->get_id ();
+
+  bool allowed_macro_name = (macro_name == IDENTIFIER || macro_name == TRY);
+
+  return t->get_str () == "macro_rules"
+        && lexer.peek_token (1)->get_id () == EXCLAM && allowed_macro_name;
+}
+
 // Parses a single item
 template <typename ManagedTokenSource>
 std::unique_ptr<AST::Item>
@@ -1142,7 +1154,7 @@ Parser<ManagedTokenSource>::parse_item (bool called_from_statement)
                            "default", "impl"));
          return nullptr;
        }
-      else if (t->get_str () == "macro_rules")
+      else if (is_macro_rules_def (t))
        {
          // macro_rules! macro item
          return parse_macro_rules_def (std::move (outer_attrs));
@@ -6233,8 +6245,7 @@ Parser<ManagedTokenSource>::parse_stmt (ParseRestrictions restrictions)
          return parse_vis_item (std::move (outer_attrs));
          // or should this go straight to parsing union?
        }
-      else if (t->get_str () == "macro_rules"
-              && lexer.peek_token (1)->get_id () == EXCLAM)
+      else if (is_macro_rules_def (t))
        {
          // macro_rules! macro item
          return parse_macro_rules_def (std::move (outer_attrs));
index 904b5028a75560ada007387a542c41a938231d10..d5c1219b08007b205c2aefc3b5b9513dff70f9a5 100644 (file)
@@ -141,6 +141,7 @@ public:
   parse_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
                    location_t pratt_parsed_loc = UNKNOWN_LOCATION);
 
+  bool is_macro_rules_def (const_TokenPtr t);
   std::unique_ptr<AST::Item> parse_item (bool called_from_statement);
   std::unique_ptr<AST::Pattern> parse_pattern ();
   std::unique_ptr<AST::Pattern> parse_pattern_no_alt ();