]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Parse AltPattern
authorOwen Avery <powerboat9.gamer@gmail.com>
Tue, 14 Feb 2023 23:42:39 +0000 (18:42 -0500)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 15:23:02 +0000 (16:23 +0100)
Renamed Parser::parse_pattern to Parser::parse_pattern_no_alt and created
new method Parser::parse_pattern to handle alternate patterns.

gcc/rust/ChangeLog:

* parse/rust-parse-impl.h
(Parser::parse_pattern): Add.
(Parser::parse_pattern_no_alt): Rename.
* parse/rust-parse.h:
(Parser::parse_pattern): Add.
(Parser::parse_pattern_no_alt): Rename.

gcc/testsuite/ChangeLog:

* rust/compile/pattern-or.rs: New test.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
gcc/rust/parse/rust-parse-impl.h
gcc/rust/parse/rust-parse.h
gcc/testsuite/rust/compile/pattern-or.rs [new file with mode: 0644]

index ef0eaef1b367e8f7912fd06590c7929e86b34506..d1f192e3183e187ac89d74068a0d6ca540024945 100644 (file)
@@ -10619,10 +10619,41 @@ Parser<ManagedTokenSource>::parse_range_pattern_bound ()
     }
 }
 
-// Parses a pattern (will further disambiguate any pattern).
 template <typename ManagedTokenSource>
 std::unique_ptr<AST::Pattern>
 Parser<ManagedTokenSource>::parse_pattern ()
+{
+  Location start_locus = lexer.peek_token ()->get_locus ();
+
+  /* skip optional starting pipe */
+  maybe_skip_token (PIPE);
+
+  auto first = parse_pattern_no_alt ();
+
+  if (lexer.peek_token ()->get_id () != PIPE)
+    /* no alternates */
+    return first;
+
+  std::vector<std::unique_ptr<AST::Pattern>> alts;
+  alts.push_back (std::move (first));
+
+  do
+    {
+      lexer.skip_token ();
+      alts.push_back (parse_pattern_no_alt ());
+    }
+  while (lexer.peek_token ()->get_id () == PIPE);
+
+  /* alternates */
+  return std::unique_ptr<AST::Pattern> (
+    new AST::AltPattern (std::move (alts), start_locus));
+}
+
+// Parses a pattern without alternates ('|')
+// (will further disambiguate any pattern).
+template <typename ManagedTokenSource>
+std::unique_ptr<AST::Pattern>
+Parser<ManagedTokenSource>::parse_pattern_no_alt ()
 {
   const_TokenPtr t = lexer.peek_token ();
   switch (t->get_id ())
index a4b65c502217795584d23ef8e2b95a1d045e2630..54a87c946f993fad7c5b47c4fba1db84ebf88dfb 100644 (file)
@@ -126,6 +126,7 @@ public:
 
   std::unique_ptr<AST::Item> parse_item (bool called_from_statement);
   std::unique_ptr<AST::Pattern> parse_pattern ();
+  std::unique_ptr<AST::Pattern> parse_pattern_no_alt ();
 
   /**
    * Parse a statement
diff --git a/gcc/testsuite/rust/compile/pattern-or.rs b/gcc/testsuite/rust/compile/pattern-or.rs
new file mode 100644 (file)
index 0000000..054b43f
--- /dev/null
@@ -0,0 +1,7 @@
+// { dg-additional-options "-fsyntax-only" }
+
+fn main() {
+    match ((12, 13)) {
+        (_, 5) | (12, _) => {}
+    }
+}