From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 32CBB3858C1F; Wed, 22 Feb 2023 07:22:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32CBB3858C1F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677050551; bh=+SSDet6Y/paVE36ofDiUGjL+JbDyHq1NfgIbro/hul8=; h=From:To:Subject:Date:From; b=rKwhV/ase0WfQAF4JZZXWzafxufX/bvagmbysvzaKSDukSZOmGthX2j+Q91DCjDwn rJAnUoydMSF3stQaylUVDC8If3s2vSZggTI4QiHF+3HsDolFZ0SHlzd9X1y9oqeAZV 7jLWYwALUX2spNSAP5eK2q7c/oXyw/esbQDuJCYI= 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] Parse AltPattern X-Act-Checkin: gcc X-Git-Author: Owen Avery X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: e23e7f14a51d93d87516c92efedc67f742a1213f X-Git-Newrev: 15ca76ea868ccb9dbb0bd1a77f9b13746220499f Message-Id: <20230222072231.32CBB3858C1F@sourceware.org> Date: Wed, 22 Feb 2023 07:22:31 +0000 (GMT) List-Id: https://gcc.gnu.org/g:15ca76ea868ccb9dbb0bd1a77f9b13746220499f commit 15ca76ea868ccb9dbb0bd1a77f9b13746220499f Author: Owen Avery Date: Tue Feb 14 18:42:39 2023 -0500 Parse AltPattern 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 Diff: --- gcc/rust/parse/rust-parse-impl.h | 33 +++++++++++++++++++++++++++++++- gcc/rust/parse/rust-parse.h | 1 + gcc/testsuite/rust/compile/pattern-or.rs | 7 +++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index db32803ddbe..4c39284749d 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -10619,10 +10619,41 @@ Parser::parse_range_pattern_bound () } } -// Parses a pattern (will further disambiguate any pattern). template std::unique_ptr Parser::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> 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 ( + new AST::AltPattern (std::move (alts), start_locus)); +} + +// Parses a pattern without alternates ('|') +// (will further disambiguate any pattern). +template +std::unique_ptr +Parser::parse_pattern_no_alt () { const_TokenPtr t = lexer.peek_token (); switch (t->get_id ()) diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index a4b65c50221..54a87c946f9 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -126,6 +126,7 @@ public: std::unique_ptr parse_item (bool called_from_statement); std::unique_ptr parse_pattern (); + std::unique_ptr 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 index 00000000000..054b43f0504 --- /dev/null +++ b/gcc/testsuite/rust/compile/pattern-or.rs @@ -0,0 +1,7 @@ +// { dg-additional-options "-fsyntax-only" } + +fn main() { + match ((12, 13)) { + (_, 5) | (12, _) => {} + } +}