From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 966A83817D27; Wed, 8 Jun 2022 12:10:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 966A83817D27 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] substitute_repetition: Add parsing of repetition pattern X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: ae1f91a698022a5600a2d54e48fc90895ea834fd X-Git-Newrev: 143aad62e16ea96e8c562b96857c2497f74ba7c7 Message-Id: <20220608121042.966A83817D27@sourceware.org> Date: Wed, 8 Jun 2022 12:10:42 +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:10:42 -0000 https://gcc.gnu.org/g:143aad62e16ea96e8c562b96857c2497f74ba7c7 commit 143aad62e16ea96e8c562b96857c2497f74ba7c7 Author: Arthur Cohen Date: Mon Feb 21 10:48:00 2022 +0100 substitute_repetition: Add parsing of repetition pattern Diff: --- gcc/rust/expand/rust-macro-expand.cc | 51 +++++++++++++++++++++++++++++++----- gcc/rust/expand/rust-macro-expand.h | 20 ++++++++++++-- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 7dc8f88a7ab..7aa4289ec6b 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -3905,20 +3905,58 @@ MacroExpander::substitute_metavar ( return expanded; } +std::vector> +MacroExpander::substitute_repetition ( + std::vector> &input, + std::map &fragments, + std::vector> &pattern) +{ + // If the repetition is not anything we know (ie no declared metavars, or + // metavars which aren't present in the fragment), we can just error out. No + // need to paste the tokens as if nothing had happened. + for (auto &token : pattern) + rust_debug ("[repetition pattern]: %s", token->as_string ().c_str ()); + + return std::vector> (); +} + std::pair>, size_t> MacroExpander::substitute_token ( + std::vector> ¯o, std::vector> &input, - std::map &fragments, - std::unique_ptr &token) + std::map &fragments, size_t token_idx) { + auto &token = macro.at (token_idx); switch (token->get_id ()) { case IDENTIFIER: rust_debug ("expanding metavar"); return {substitute_metavar (input, fragments, token), 1}; - case LEFT_PAREN: - rust_debug ("expanding repetition"); - break; + case LEFT_PAREN: { + // We need to parse up until the closing delimiter and expand this + // fragment->n times. + rust_debug ("expanding repetition"); + std::vector> repetition_pattern; + for (size_t rep_idx = token_idx + 1; + rep_idx < macro.size () + && macro.at (rep_idx)->get_id () != RIGHT_PAREN; + rep_idx++) + repetition_pattern.emplace_back (macro.at (rep_idx)->clone_token ()); + + // FIXME: This skips whitespaces... Is that okay?? + // 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 (input, fragments, repetition_pattern), + // + 2 for the opening and closing parenthesis which are mandatory + repetition_pattern.size () + 2}; + } // 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 // with no associated fragment and paste the dollar sign in the @@ -3956,10 +3994,9 @@ MacroExpander::substitute_tokens ( auto &tok = macro.at (i); if (tok->get_id () == DOLLAR_SIGN) { - auto &next_tok = macro.at (i + 1); // Aaaaah, if only we had C++17 :) // auto [expanded, tok_to_skip] = ... - auto p = substitute_token (input, fragments, next_tok); + auto p = substitute_token (macro, input, fragments, i + 1); auto expanded = std::move (p.first); auto tok_to_skip = p.second; diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index f77acc70769..943115dcdcd 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -199,9 +199,24 @@ struct MacroExpander std::map &fragments, std::unique_ptr &metavar); + /** + * Substitute a macro repetition by its given fragments + * + * @param input Tokens given to the transcribing context + * @param fragments Fragments given to the macro substitution + * @param repetition Set of tokens to substitute and replace + * + * @return A vector containing the repeated pattern + */ + static std::vector> + substitute_repetition (std::vector> &input, + std::map &fragments, + std::vector> &pattern); + /** * Substitute a given token by its appropriate representation * + * @param macro Tokens used in the macro declaration * @param input Tokens given to the transcribing context * @param fragments Fragments given to the macro substitution * @param token Current token to try and substitute @@ -213,9 +228,10 @@ struct MacroExpander * ahead of the input to avoid mis-substitutions */ static std::pair>, size_t> - substitute_token (std::vector> &input, + substitute_token (std::vector> ¯o, + std::vector> &input, std::map &fragments, - std::unique_ptr &token); + size_t token_idx); static std::vector> substitute_tokens (std::vector> &input,