From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 189AB3858C42; Tue, 16 Jan 2024 18:14:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 189AB3858C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705428862; bh=v6MjcKNxOx5ZylbpmlVOxwnBPRmofn5gpVLigJkGAY0=; h=From:To:Subject:Date:From; b=k6RSXH4nqZMtgoe9nKhxDhxvWpYb8sAHC5o8SMlEunUdMWizR7ukb/oQ/kqXaEWrY +eV7J99MBsRIOkAV3+0nlRD4v1kSY7pdHoWQ6E91QQiwlgUl7B/XC6pMbv0gAl9dKz jXjVqphGjbJwwWJhWPclIAPym5/3dsIWohBjhH/Q= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8051] gccrs: Allow macro named macro_rules X-Act-Checkin: gcc X-Git-Author: Pierre-Emmanuel Patry X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 3d43c98bf857c42ec533cc605f157b8431aa1b4c X-Git-Newrev: 68a8a5503729c6b45b58e0147ebba675db6fbfd5 Message-Id: <20240116181422.189AB3858C42@sourceware.org> Date: Tue, 16 Jan 2024 18:14:21 +0000 (GMT) List-Id: https://gcc.gnu.org/g:68a8a5503729c6b45b58e0147ebba675db6fbfd5 commit r14-8051-g68a8a5503729c6b45b58e0147ebba675db6fbfd5 Author: Pierre-Emmanuel Patry Date: Tue Oct 10 15:46:48 2023 +0200 gccrs: Allow macro named macro_rules 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 Diff: --- gcc/rust/parse/rust-parse-impl.h | 17 ++++++++++++++--- gcc/rust/parse/rust-parse.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 63dea9836df..71f76f8e2eb 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1071,6 +1071,18 @@ Parser::parse_token_tree () } } +template +bool +Parser::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 std::unique_ptr @@ -1142,7 +1154,7 @@ Parser::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::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)); diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 904b5028a75..d5c1219b080 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -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 parse_item (bool called_from_statement); std::unique_ptr parse_pattern (); std::unique_ptr parse_pattern_no_alt ();