Skip to content

Commit

Permalink
gccrs: Changes made to ensure that '=>' token is parsed properly
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

        * parse/rust-parse-impl.h (Parser::parse_expr): made change to parsing logic so that it
          will stop when encountered a '=>' or ',' while having restrictions.stop_on_token
          turned on.
        (Parser::left_denotations): Same as above stopping parsing on above special tokens.
        (Parser::null_denotation): Same as above stopping parsing on above special tokens.
        * parse/rust-parse.h (struct ParseRestrictions): Added a field stop_on_token (default
          false) to maintain the above functionality.
  • Loading branch information
sriganeshres committed Dec 5, 2024
1 parent 4564a4f commit 39f91fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 21 additions & 1 deletion gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10444,7 +10444,7 @@ Parser<ManagedTokenSource>::parse_pattern ()
{
lexer.skip_token ();
alts.push_back (parse_pattern_no_alt ());
}
}

while (lexer.peek_token ()->get_id () == PIPE);

Expand Down Expand Up @@ -12023,6 +12023,11 @@ Parser<ManagedTokenSource>::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;
Expand Down Expand Up @@ -12078,6 +12083,14 @@ Parser<ManagedTokenSource>::left_denotations (std::unique_ptr<AST::Expr> 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),
Expand Down Expand Up @@ -12116,6 +12129,13 @@ Parser<ManagedTokenSource>::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) */
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 39f91fe

Please sign in to comment.