public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macro-repetitions: Match repetitions properly
@ 2022-06-08 12:09 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:09 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ad7e4bb6f8e19efbae93332f25f3a4897fedbb74

commit ad7e4bb6f8e19efbae93332f25f3a4897fedbb74
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Fri Feb 18 12:15:14 2022 +0100

    macro-repetitions: Match repetitions properly

Diff:
---
 gcc/rust/ast/rust-macro.h            |   3 +
 gcc/rust/expand/rust-macro-expand.cc | 117 ++++++++++++++++++++++++++++++++++-
 gcc/rust/expand/rust-macro-expand.h  |  25 ++++++++
 3 files changed, 142 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h
index fed45895eb8..995b255294d 100644
--- a/gcc/rust/ast/rust-macro.h
+++ b/gcc/rust/ast/rust-macro.h
@@ -206,6 +206,9 @@ public:
     return MacroMatchType::Repetition;
   }
 
+  MacroRepOp get_op () const { return op; }
+  std::vector<std::unique_ptr<MacroMatch> > &get_matches () { return matches; }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
    * than base */
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index dcfec7ceeb7..68859291873 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -3520,6 +3520,7 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
   for (auto &match : matcher.get_matches ())
     {
       size_t offs_begin = source.get_offs ();
+
       switch (match->get_macro_match_type ())
 	{
 	  case AST::MacroMatch::MacroMatchType::Fragment: {
@@ -3597,13 +3598,123 @@ MacroExpander::match_token (Parser<MacroInvocLexer> &parser, AST::Token &token)
   return parser.skip_token (token.get_id ());
 }
 
+bool
+MacroExpander::match_n_matches (
+  Parser<MacroInvocLexer> &parser,
+  std::vector<std::unique_ptr<AST::MacroMatch>> &matches, size_t &match_amount,
+  size_t lo_bound, size_t hi_bound)
+{
+  match_amount = 0;
+
+  const MacroInvocLexer &source = parser.get_token_source ();
+  while (true)
+    {
+      // If the current token is a closing macro delimiter, break away.
+      // TODO: Is this correct?
+      auto t_id = parser.peek_current_token ()->get_id ();
+      if (t_id == RIGHT_PAREN || t_id == RIGHT_SQUARE || t_id == RIGHT_CURLY)
+	break;
+
+      bool valid_current_match = false;
+      for (auto &match : matches)
+	{
+	  size_t offs_begin = source.get_offs ();
+	  switch (match->get_macro_match_type ())
+	    {
+	      case AST::MacroMatch::MacroMatchType::Fragment: {
+		AST::MacroMatchFragment *fragment
+		  = static_cast<AST::MacroMatchFragment *> (match.get ());
+		valid_current_match = match_fragment (parser, *fragment);
+
+		// matched fragment get the offset in the token stream
+		size_t offs_end = source.get_offs ();
+		sub_stack.peek ().insert (
+		  {fragment->get_ident (),
+		   {fragment->get_ident (), offs_begin, offs_end}});
+	      }
+	      break;
+
+	      case AST::MacroMatch::MacroMatchType::Tok: {
+		AST::Token *tok = static_cast<AST::Token *> (match.get ());
+		valid_current_match = match_token (parser, *tok);
+	      }
+	      break;
+
+	      case AST::MacroMatch::MacroMatchType::Repetition: {
+		AST::MacroMatchRepetition *rep
+		  = static_cast<AST::MacroMatchRepetition *> (match.get ());
+		valid_current_match = match_repetition (parser, *rep);
+	      }
+	      break;
+
+	      case AST::MacroMatch::MacroMatchType::Matcher: {
+		AST::MacroMatcher *m
+		  = static_cast<AST::MacroMatcher *> (match.get ());
+		valid_current_match = match_matcher (parser, *m);
+	      }
+	      break;
+	    }
+	}
+      // If we've encountered an error once, stop trying to match more
+      // repetitions
+      if (!valid_current_match)
+	break;
+
+      match_amount++;
+
+      // Break early if we notice there's too many expressions already
+      if (hi_bound && match_amount > hi_bound)
+	break;
+    }
+
+  // Check if the amount of matches we got is valid: Is it more than the lower
+  // bound and less than the higher bound?
+  if (!hi_bound) // infinite amount, no upper bound
+    return match_amount >= lo_bound;
+  else
+    return match_amount >= lo_bound && match_amount <= hi_bound;
+}
+
 bool
 MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
 				 AST::MacroMatchRepetition &rep)
 {
-  // TODO
-  gcc_unreachable ();
-  return false;
+  size_t match_amount = 0;
+  bool res = false;
+
+  std::string lo_str;
+  std::string hi_str;
+  switch (rep.get_op ())
+    {
+    case AST::MacroMatchRepetition::MacroRepOp::ANY:
+      lo_str = "0";
+      hi_str = "+inf";
+      res = match_n_matches (parser, rep.get_matches (), match_amount);
+      break;
+    case AST::MacroMatchRepetition::MacroRepOp::ONE_OR_MORE:
+      lo_str = "1";
+      hi_str = "+inf";
+      res = match_n_matches (parser, rep.get_matches (), match_amount, 1);
+      break;
+    case AST::MacroMatchRepetition::MacroRepOp::ZERO_OR_ONE:
+      lo_str = "0";
+      hi_str = "1";
+      res = match_n_matches (parser, rep.get_matches (), match_amount, 0, 1);
+      break;
+    default:
+      gcc_unreachable ();
+    }
+
+  if (!res)
+    rust_error_at (rep.get_match_locus (),
+		   "invalid amount of matches for macro invocation. Expected "
+		   "between %s and %s, got %lu",
+		   lo_str.c_str (), hi_str.c_str (), match_amount);
+
+  rust_debug_loc (rep.get_match_locus (), "%s matched %lu times",
+		  res ? "successfully" : "unsuccessfully", match_amount);
+
+  return res;
 }
 
 AST::ASTFragment
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index d8a2d5003e6..d49c77571b2 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -193,6 +193,31 @@ struct MacroExpander
   bool match_matcher (Parser<MacroInvocLexer> &parser,
 		      AST::MacroMatcher &matcher);
 
+  /**
+   * Match any amount of matches
+   *
+   * @param parser Parser to use for matching
+   * @param matches All consecutive matches to identify
+   * @param match_amount Reference in which to store the ammount of succesful
+   * and valid matches
+   *
+   * @param lo_bound Lower bound of the matcher. When specified, the matcher
+   * will only succeed if it parses at *least* `lo_bound` fragments. If
+   * unspecified, the matcher could succeed when parsing 0 fragments.
+   *
+   * @param hi_bound Higher bound of the matcher. When specified, the matcher
+   * will only succeed if it parses *less than* `hi_bound` fragments. If
+   * unspecified, the matcher could succeed when parsing an infinity of
+   * fragments.
+   *
+   * @return true if matching was successful and within the given limits, false
+   * otherwise
+   */
+  bool match_n_matches (Parser<MacroInvocLexer> &parser,
+			std::vector<std::unique_ptr<AST::MacroMatch>> &matches,
+			size_t &match_amount, size_t lo_bound = 0,
+			size_t hi_bound = 0);
+
   static std::vector<std::unique_ptr<AST::Token>>
   substitute_tokens (std::vector<std::unique_ptr<AST::Token>> &input,
 		     std::vector<std::unique_ptr<AST::Token>> &macro,


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

only message in thread, other threads:[~2022-06-08 12:09 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:09 [gcc/devel/rust/master] macro-repetitions: Match repetitions properly 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).