From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id BF557380FDF9; Wed, 8 Jun 2022 12:15:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF557380FDF9 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] macros: Substitute separator if necessary when expanding repetitions X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: ab4533dab7a5be7c900012f40dbeb3fc0cce0456 X-Git-Newrev: 4fde21b37a2de2d2915d13d108395e86980aef96 Message-Id: <20220608121511.BF557380FDF9@sourceware.org> Date: Wed, 8 Jun 2022 12:15:11 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:15:11 -0000 https://gcc.gnu.org/g:4fde21b37a2de2d2915d13d108395e86980aef96 commit 4fde21b37a2de2d2915d13d108395e86980aef96 Author: Arthur Cohen Date: Thu Mar 3 14:54:50 2022 +0100 macros: Substitute separator if necessary when expanding repetitions Diff: --- gcc/rust/expand/rust-macro-substitute-ctx.cc | 44 +++++++++++++++++++++++----- gcc/rust/expand/rust-macro-substitute-ctx.h | 6 ++-- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc b/gcc/rust/expand/rust-macro-substitute-ctx.cc index 3d07b484134..f9f2005b72b 100644 --- a/gcc/rust/expand/rust-macro-substitute-ctx.cc +++ b/gcc/rust/expand/rust-macro-substitute-ctx.cc @@ -33,7 +33,9 @@ SubstituteCtx::substitute_metavar (std::unique_ptr &metavar) } std::vector> -SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end) +SubstituteCtx::substitute_repetition ( + size_t pattern_start, size_t pattern_end, + std::unique_ptr separator_token) { rust_assert (pattern_end < macro.size ()); @@ -117,6 +119,11 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end) auto substitute_context = SubstituteCtx (input, new_macro, sub_map); auto new_tokens = substitute_context.substitute_tokens (); + // Skip the first repetition, but add the separator to the expanded + // tokens if it is present + if (i != 0 && separator_token) + expanded.emplace_back (separator_token->clone_token ()); + for (auto &new_token : new_tokens) expanded.emplace_back (new_token->clone_token ()); } @@ -127,6 +134,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end) return expanded; } +static bool +is_rep_op (std::unique_ptr &tok) +{ + auto id = tok->get_id (); + return id == QUESTION_MARK || id == ASTERISK || id == PLUS; +} + std::pair>, size_t> SubstituteCtx::substitute_token (size_t token_idx) { @@ -148,20 +162,34 @@ SubstituteCtx::substitute_token (size_t token_idx) pattern_end++) ; + std::unique_ptr separator_token = nullptr; + // FIXME: Can this go out of bounds? + auto &post_pattern_token = macro.at (pattern_end + 1); + if (!is_rep_op (post_pattern_token)) + separator_token = post_pattern_token->clone_token (); + + // Amount of tokens to skip + auto to_skip = 0; + // Parentheses + to_skip += 2; + // Repetition operator + to_skip += 1; + // Separator + if (separator_token) + to_skip += 1; + // FIXME: This skips whitespaces... Is that okay?? - // FIXME: Is there any existing parsing function that allows us to parse - // a macro pattern? + // FIXME: Is there any existing parsing function that allows us to + // parse a macro pattern? // FIXME: Add error handling in the case we haven't found a matching // closing delimiter // FIXME: We need to parse the repetition token now - return { - substitute_repetition (pattern_start, pattern_end), - // + 2 for the opening and closing parentheses which are mandatory - // + 1 for the repetitor (+, *, ?) - pattern_end - pattern_start + 3}; + return {substitute_repetition (pattern_start, pattern_end, + std::move (separator_token)), + pattern_end - pattern_start + to_skip}; } // TODO: We need to check if the $ was alone. In that case, do // not error out: Simply act as if there was an empty identifier diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.h b/gcc/rust/expand/rust-macro-substitute-ctx.h index d51fb81c2c7..ed83926c32c 100644 --- a/gcc/rust/expand/rust-macro-substitute-ctx.h +++ b/gcc/rust/expand/rust-macro-substitute-ctx.h @@ -49,12 +49,14 @@ public: * Substitute a macro repetition by its given fragments * * @param pattern_start Start index of the pattern tokens - * @param pattern_end Index Amount of tokens in the pattern + * @param pattern_end End index of the patterns tokens + * @param separator Optional separator to include when expanding tokens * * @return A vector containing the repeated pattern */ std::vector> - substitute_repetition (size_t pattern_start, size_t pattern_end); + substitute_repetition (size_t pattern_start, size_t pattern_end, + std::unique_ptr separator); /** * Substitute a given token by its appropriate representation