From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 3662F3858001 for ; Sun, 25 Jul 2021 13:53:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3662F3858001 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x02.wildebeest.org [172.31.17.132]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 8C72A300047B; Sun, 25 Jul 2021 15:53:45 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 33DC22E80D97; Sun, 25 Jul 2021 15:53:45 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH] Support RangeFrom ([x..]) and RangeFromTo ([x..y]) in the parser Date: Sun, 25 Jul 2021 15:53:43 +0200 Message-Id: <20210725135343.72944-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Jul 2021 13:53:48 -0000 Parsing the .. (DOT_DOT) operator to get a range had two issues. Trying to compile: let block = [1,2,3,4,5]; let _rf = &block[1..]; let _rt = &block[..3]; let _rft = &block[2..4]; range.rs:4:23: error: found unexpected token ‘]’ in null denotation 4 | let _rf = &block[1..]; | ^ range.rs:4:24: error: expecting ‘]’ but ‘;’ found 4 | let _rf = &block[1..]; | ^ Since .. can represent either a range from or a range from-to it can be followed by an expression or not. We do have a hack in our pratt-parser so that it is allowed to return a nullptr. But even in that case it will have swallowed the next token. Add another hack to the pratt-parser so that if the next token is one that cannot start an expression and the caller allows a nullptr return then don't skip the token and return immediately. After this patch we can parse the above range expressions, but we still don't handle them fully: range.rs:4:20: fatal error: Failed to lower expr: [1..] 4 | let _rf = &block[1..]; | ^ Ranges are actually syntactic sugar for std::ops::Range[From|To]. --- gcc/rust/parse/rust-parse-impl.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index be261715c6c..7b128fff157 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -12348,6 +12348,18 @@ Parser::parse_expr (int right_binding_power, ParseRestrictions restrictions) { const_TokenPtr current_token = lexer.peek_token (); + // Special hack because we are allowed to return nullptr, in that case we + // don't want to skip the token, since we don't actually parse it. But if + // null isn't allowed it indicates an error, and we want to skip past that. + // So return early if it is one of the tokens that ends an expression + // (or at least cannot start a new expression). + if (restrictions.expr_can_be_null) + { + TokenId id = current_token->get_id (); + if (id == SEMICOLON || id == RIGHT_PAREN || id == RIGHT_CURLY + || id == RIGHT_SQUARE) + return nullptr; + } lexer.skip_token (); // parse null denotation (unary part of expression) @@ -14028,6 +14040,9 @@ Parser::parse_led_range_exclusive_expr ( { // FIXME: this probably parses expressions accidently or whatever // try parsing RHS (as tok has already been consumed in parse_expression) + // Can be nullptr, in which case it is a RangeFromExpr, otherwise a + // RangeFromToExpr. + restrictions.expr_can_be_null = true; std::unique_ptr right = parse_expr (LBP_DOT_DOT, AST::AttrVec (), restrictions); -- 2.32.0