From c89c67bef6aa3cc9eb7d75bbf26ef1e2c0fd449c Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Fri, 28 Aug 2026 14:55:56 -0700 Subject: [PATCH] [Pratt parser] Fix operation precedence following negative numbers: `-42.foo` PiperOrigin-RevId: 972804901 --- parser/internal/pratt_parser_test.cc | 33 +++++++++++++++++++++++++++ parser/internal/pratt_parser_worker.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/parser/internal/pratt_parser_test.cc b/parser/internal/pratt_parser_test.cc index 6c85dd1d7..2db65b98e 100644 --- a/parser/internal/pratt_parser_test.cc +++ b/parser/internal/pratt_parser_test.cc @@ -482,6 +482,39 @@ std::vector GetParserTestCases() { )^#1:Expr.Call# )", }, + TestCase{ + .source = "-.2.V", + .expected_ast = R"( + -0.2^#1:double#.V^#2:Expr.Select# + )", + }, + TestCase{ + .source = "!-.2.V", + .expected_ast = R"( + !_( + -0.2^#2:double#.V^#3:Expr.Select# + )^#1:Expr.Call# + )", + }, + TestCase{ + .source = "!-2.V", + .expected_ast = R"( + !_( + -2^#2:int64#.V^#3:Expr.Select# + )^#1:Expr.Call# + )", + }, + TestCase{ + .source = "!-.2[0]", + .expected_ast = R"( + !_( + _[_]( + -0.2^#2:double#, + 0^#4:int64# + )^#3:Expr.Call# + )^#1:Expr.Call# + )", + }, TestCase{ .source = "a + b", .expected_ast = R"( diff --git a/parser/internal/pratt_parser_worker.h b/parser/internal/pratt_parser_worker.h index f4886aa90..1f99984c5 100644 --- a/parser/internal/pratt_parser_worker.h +++ b/parser/internal/pratt_parser_worker.h @@ -556,10 +556,12 @@ ExprNode PrattParserWorker::ParseUnaryOpsChain(Token first_op) { int64_t op_id = ops.back().id; ops.pop_back(); operand = ParseNegativeIntLiteral(op_id); + ParseSelectorChainTail(operand); } else if (peek_token_.type == TokenType::kFloat) { int64_t op_id = ops.back().id; ops.pop_back(); operand = ParseNegativeDoubleLiteral(op_id); + ParseSelectorChainTail(operand); } else { operand = ParseSelectorChain(); }