public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] substitute_repetition: Add parsing of repetition pattern
@ 2022-06-08 12:10 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:10 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:143aad62e16ea96e8c562b96857c2497f74ba7c7

commit 143aad62e16ea96e8c562b96857c2497f74ba7c7
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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<std::unique_ptr<AST::Token>>
+MacroExpander::substitute_repetition (
+  std::vector<std::unique_ptr<AST::Token>> &input,
+  std::map<std::string, MatchedFragment> &fragments,
+  std::vector<std::unique_ptr<AST::Token>> &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::unique_ptr<AST::Token>> ();
+}
+
 std::pair<std::vector<std::unique_ptr<AST::Token>>, size_t>
 MacroExpander::substitute_token (
+  std::vector<std::unique_ptr<AST::Token>> &macro,
   std::vector<std::unique_ptr<AST::Token>> &input,
-  std::map<std::string, MatchedFragment> &fragments,
-  std::unique_ptr<AST::Token> &token)
+  std::map<std::string, MatchedFragment> &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<std::unique_ptr<AST::Token>> 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<std::string, MatchedFragment> &fragments,
 		      std::unique_ptr<AST::Token> &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<std::unique_ptr<AST::Token>>
+  substitute_repetition (std::vector<std::unique_ptr<AST::Token>> &input,
+			 std::map<std::string, MatchedFragment> &fragments,
+			 std::vector<std::unique_ptr<AST::Token>> &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<std::vector<std::unique_ptr<AST::Token>>, size_t>
-  substitute_token (std::vector<std::unique_ptr<AST::Token>> &input,
+  substitute_token (std::vector<std::unique_ptr<AST::Token>> &macro,
+		    std::vector<std::unique_ptr<AST::Token>> &input,
 		    std::map<std::string, MatchedFragment> &fragments,
-		    std::unique_ptr<AST::Token> &token);
+		    size_t token_idx);
 
   static std::vector<std::unique_ptr<AST::Token>>
   substitute_tokens (std::vector<std::unique_ptr<AST::Token>> &input,


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:10 [gcc/devel/rust/master] substitute_repetition: Add parsing of repetition pattern Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).