Skip to content

Commit 155ad1e

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
[Pratt Parser] Refactor worker methods and parse unary operator chains iteratively.
- Extract helper methods (`ParseTernary`, `ParseSelectorChainTail`, `ParseUnaryOpsChain`, `ParseIdentOrCall`, `ParseNegativeIntLiteral`, `ParseNegativeDoubleLiteral`, `BuildBinaryCall`) to reduce the size of the call stack frames - Process prefix unary operator chains (`!`, `-`) iteratively in `ParseUnaryOpsChain` to reduce stack growth on deep chains. PiperOrigin-RevId: 953546973
1 parent ed733ed commit 155ad1e

3 files changed

Lines changed: 267 additions & 116 deletions

File tree

parser/internal/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ cc_library(
9191
"//internal:strings",
9292
"//parser:options",
9393
"//parser:parser_interface",
94+
"@com_google_absl//absl/base:core_headers",
9495
"@com_google_absl//absl/base:nullability",
9596
"@com_google_absl//absl/container:flat_hash_map",
9697
"@com_google_absl//absl/status:statusor",

parser/internal/pratt_parser_test.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,18 @@ std::vector<TestCase> GetParserTestCases() {
723723
)^#4:Expr.Call#
724724
)",
725725
},
726+
TestCase{
727+
.source = "(((10 - 3) - 2))",
728+
.expected_ast = R"(
729+
_-_(
730+
_-_(
731+
10^#1:int64#,
732+
3^#3:int64#
733+
)^#2:Expr.Call#,
734+
2^#5:int64#
735+
)^#4:Expr.Call#
736+
)",
737+
},
726738
TestCase{
727739
.source = "1 + 2 * 3 - 1 / 2 == 6 % 1",
728740
.expected_ast = R"(
@@ -747,6 +759,30 @@ std::vector<TestCase> GetParserTestCases() {
747759
)^#10:Expr.Call#
748760
)",
749761
},
762+
TestCase{
763+
.source = "(1 + (2 * 3) - (1 / 2)) == (6 % 1)",
764+
.expected_ast = R"(
765+
_==_(
766+
_-_(
767+
_+_(
768+
1^#1:int64#,
769+
_*_(
770+
2^#3:int64#,
771+
3^#5:int64#
772+
)^#4:Expr.Call#
773+
)^#2:Expr.Call#,
774+
_/_(
775+
1^#7:int64#,
776+
2^#9:int64#
777+
)^#8:Expr.Call#
778+
)^#6:Expr.Call#,
779+
_%_(
780+
6^#11:int64#,
781+
1^#13:int64#
782+
)^#12:Expr.Call#
783+
)^#10:Expr.Call#
784+
)",
785+
},
750786
TestCase{
751787
.source = "1 + 2 * 3 == 7 && true || false",
752788
.expected_ast = R"(
@@ -1039,6 +1075,18 @@ std::vector<TestCase> GetParserTestCases() {
10391075
)",
10401076
.enable_optional_syntax = true,
10411077
},
1078+
TestCase{
1079+
.source = "(((10 - 3) - 2))",
1080+
.expected_ast = R"(
1081+
_-_(
1082+
_-_(
1083+
10^#1:int64#,
1084+
3^#3:int64#
1085+
)^#2:Expr.Call#,
1086+
2^#5:int64#
1087+
)^#4:Expr.Call#
1088+
)",
1089+
},
10421090
};
10431091
}
10441092

0 commit comments

Comments
 (0)