public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7632] gccrs: Properly match delimiters
@ 2024-01-16 17:46 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 17:46 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:007248a2c48ef3c349204899b325bab574019734

commit r14-7632-g007248a2c48ef3c349204899b325bab574019734
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date:   Tue May 30 16:24:08 2023 -0400

    gccrs: Properly match delimiters
    
    gcc/rust/ChangeLog:
    
            * expand/rust-macro-expand.cc
            (MacroExpander::try_match_rule): Don't match delimiters for root matcher.
            (MacroExpander::match_matcher): Add option to match delimiters.
            * expand/rust-macro-expand.h
            (MacroExpander::match_matcher): Likewise.
            * parse/rust-parse-impl.h
            (Parser::skip_token): Add zero argument method.
            * parse/rust-parse.h:
            (Parser::skip_token): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/macro-delim.rs: New test.
    
    Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>

Diff:
---
 gcc/rust/expand/rust-macro-expand.cc      | 18 ++++++++++++------
 gcc/rust/expand/rust-macro-expand.h       |  3 ++-
 gcc/rust/parse/rust-parse-impl.h          |  8 ++++++++
 gcc/rust/parse/rust-parse.h               |  5 +++++
 gcc/testsuite/rust/compile/macro-delim.rs |  8 ++++++++
 5 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index f347335f2ef..7229a09f2fe 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -352,7 +352,7 @@ MacroExpander::try_match_rule (AST::MacroRule &match_rule,
   AST::MacroMatcher &matcher = match_rule.get_matcher ();
 
   expansion_depth++;
-  if (!match_matcher (parser, matcher))
+  if (!match_matcher (parser, matcher, false, false))
     {
       expansion_depth--;
       return false;
@@ -437,7 +437,8 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser,
 
 bool
 MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
-			      AST::MacroMatcher &matcher, bool in_repetition)
+			      AST::MacroMatcher &matcher, bool in_repetition,
+			      bool match_delim)
 {
   if (depth_exceeds_recursion_limit ())
     {
@@ -447,29 +448,34 @@ MacroExpander::match_matcher (Parser<MacroInvocLexer> &parser,
 
   auto delimiter = parser.peek_current_token ();
 
+  auto check_delim = [&matcher, match_delim] (AST::DelimType delim) {
+    return !match_delim || matcher.get_delim_type () == delim;
+  };
+
   // this is used so we can check that we delimit the stream correctly.
   switch (delimiter->get_id ())
     {
       case LEFT_PAREN: {
-	if (!parser.skip_token (LEFT_PAREN))
+	if (!check_delim (AST::DelimType::PARENS))
 	  return false;
       }
       break;
 
       case LEFT_SQUARE: {
-	if (!parser.skip_token (LEFT_SQUARE))
+	if (!check_delim (AST::DelimType::SQUARE))
 	  return false;
       }
       break;
 
       case LEFT_CURLY: {
-	if (!parser.skip_token (LEFT_CURLY))
+	if (!check_delim (AST::DelimType::CURLY))
 	  return false;
       }
       break;
     default:
-      gcc_unreachable ();
+      return false;
     }
+  parser.skip_token ();
 
   const MacroInvocLexer &source = parser.get_token_source ();
 
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index ceac8e60405..a24ec4f682c 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -275,7 +275,8 @@ struct MacroExpander
 			 AST::MacroMatchRepetition &rep);
 
   bool match_matcher (Parser<MacroInvocLexer> &parser,
-		      AST::MacroMatcher &matcher, bool in_repetition = false);
+		      AST::MacroMatcher &matcher, bool in_repetition = false,
+		      bool match_delim = true);
 
   /**
    * Match any amount of matches
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 3f2500660df..9faa374915c 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -12159,6 +12159,14 @@ Parser<ManagedTokenSource>::skip_after_semicolon ()
     lexer.skip_token ();
 }
 
+/* Skips the current token */
+template <typename ManagedTokenSource>
+void
+Parser<ManagedTokenSource>::skip_token ()
+{
+  lexer.skip_token ();
+}
+
 /* Checks if current token has inputted id - skips it and returns true if so,
  * diagnoses an error and returns false otherwise. */
 template <typename ManagedTokenSource>
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 1e7e5262eec..315d3fcdec6 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -94,6 +94,11 @@ struct ParseRestrictions
 template <typename ManagedTokenSource> class Parser
 {
 public:
+  /**
+   * Consume a token
+   */
+  void skip_token ();
+
   /**
    * Consume a token, reporting an error if it isn't the next token
    *
diff --git a/gcc/testsuite/rust/compile/macro-delim.rs b/gcc/testsuite/rust/compile/macro-delim.rs
new file mode 100644
index 00000000000..de4cd5607d9
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro-delim.rs
@@ -0,0 +1,8 @@
+macro_rules! foo {
+    ([]) => {struct Foo;};
+    (()) => {struct _A;};
+    (bool) => {struct _B;};
+}
+
+foo! (());
+foo! (bool);

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

only message in thread, other threads:[~2024-01-16 17:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 17:46 [gcc r14-7632] gccrs: Properly match delimiters Arthur Cohen

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).