Skip to content

Commit

Permalink
[DSLX:FE] Add nesting guard for Pattern production in the grammar as …
Browse files Browse the repository at this point in the history
…well.
  • Loading branch information
cdleary committed Jan 1, 2025
1 parent d2a163b commit 1cf4bf4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions xls/dslx/frontend/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,8 @@ absl::StatusOr<Expr*> Parser::ParseComparisonExpression(
}

absl::StatusOr<NameDefTree*> Parser::ParsePattern(Bindings& bindings) {
XLS_ASSIGN_OR_RETURN(ExpressionDepthGuard depth_guard, BumpExpressionDepth());

XLS_ASSIGN_OR_RETURN(std::optional<Token> oparen,
TryPopToken(TokenKind::kOParen));
if (oparen.has_value()) {
Expand Down
11 changes: 11 additions & 0 deletions xls/dslx/frontend/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3207,6 +3207,17 @@ TEST_F(ParserTest, UnreasonablyDeepTypeDefinition) {
HasSubstr("Extremely deep nesting detected")));
}

// As above but guards the pattern-match production in the grammar.
TEST_F(ParserTest, UnreasonablyDeepPatternMatch) {
constexpr std::string_view kProgram = R"(fn f(x: u32) { match x {
((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()";
Scanner s{file_table_, Fileno(0), std::string(kProgram)};
Parser parser{"test", &s};
absl::StatusOr<std::unique_ptr<Module>> module = parser.ParseModule();
EXPECT_THAT(module, StatusIs(absl::StatusCode::kInvalidArgument,
HasSubstr("Extremely deep nesting detected")));
}

TEST_F(ParserTest, NonTypeDefinitionBeforeArrayLiteralColon) {
constexpr std::string_view kProgram = "const A=4[5]:[";
Scanner s{file_table_, Fileno(0), std::string(kProgram)};
Expand Down

0 comments on commit 1cf4bf4

Please sign in to comment.