From e7e7360745d5f0aaca5291bd25e4dd0da7564060 Mon Sep 17 00:00:00 2001 From: Sri Ganesh Thota Date: Wed, 4 Dec 2024 23:07:57 +0530 Subject: [PATCH] gccrs: Changes made to ensure that '=>' token is parsed properly gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_expr): Updated parsing logic to stop on encountering '=>' or ',' while having restrictions.stop_on_token turned on. (Parser::left_denotations): Updated to stop parsing on the special tokens mentioned above. (Parser::null_denotation): Updated to stop parsing on the special tokens mentioned above. * parse/rust-parse.h (struct ParseRestrictions): Added a field stop_on_token (default false) to maintain the above functionality. --- gcc/rust/parse/rust-parse-impl.h | 20 ++++++++++++++++++++ gcc/rust/parse/rust-parse.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index aff81448deae..f81d4b5b6f9b 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -12023,6 +12023,11 @@ Parser::parse_expr (int right_binding_power, if (restrictions.expr_can_be_null) { TokenId id = current_token->get_id (); + if (restrictions.stop_on_token && (id == MATCH_ARROW || id == COMMA)) + { + rust_debug ("Stopping Parsing at special token"); + return nullptr; + } if (id == SEMICOLON || id == RIGHT_PAREN || id == RIGHT_CURLY || id == RIGHT_SQUARE || id == COMMA || id == LEFT_CURLY) return nullptr; @@ -12078,6 +12083,14 @@ Parser::left_denotations (std::unique_ptr expr, while (right_binding_power < left_binding_power (current_token)) { lexer.skip_token (); + if (restrictions.stop_on_token + && (current_token->get_id () == MATCH_ARROW + || current_token->get_id () == COMMA)) + { + rust_debug ( + "Stopping parsing at restricted token in left_denotations"); + return expr; + } // FIXME attributes should generally be applied to the null denotation. expr = left_denotation (current_token, std::move (expr), @@ -12116,6 +12129,13 @@ Parser::null_denotation (const_TokenPtr tok, /* note: tok is previous character in input stream, not current one, as * parse_expr skips it before passing it in */ + if (restrictions.stop_on_token + && (tok->get_id () == MATCH_ARROW || tok->get_id () == COMMA)) + { + rust_debug ("Stopping parsing at restricted token in null_denotation"); + return nullptr; + } + /* as a Pratt parser (which works by decomposing expressions into a null * denotation and then a left denotation), null denotations handle primaries * and unary operands (but only prefix unary operands) */ diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 95c0a0b94289..471ef729a9c9 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -89,6 +89,8 @@ struct ParseRestrictions bool entered_from_unary = false; bool expr_can_be_null = false; bool expr_can_be_stmt = false; + bool stop_on_token + = false; // This is for stopping parsing at a specific token bool consume_semi = true; /* Macro invocations that are statements can expand without a semicolon after * the final statement, if it's an expression statement. */