From 4bd09ce06f50d266c992c984cc993384d5e6655e Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Tue, 14 Feb 2023 18:42:39 -0500 Subject: [PATCH] gccrs: 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 --- 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(-) create mode 100644 gcc/testsuite/rust/compile/pattern-or.rs diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index ef0eaef1b367..d1f192e3183e 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 a4b65c502217..54a87c946f99 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 000000000000..054b43f05046 --- /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, _) => {} + } +} -- 2.47.2