Skip to content

Commit c1b66fc

Browse files
committed
Fix crash on (x[i)
Emit 'E055: unmatched indexing bracket' in more situations.
1 parent 4e6125d commit c1b66fc

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/parse.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,7 @@ expression* parser::parse_index_expression_remainder(expression* lhs) {
14211421
this->skip();
14221422
expression* subscript =
14231423
this->parse_expression(precedence{.trailing_identifiers = true});
1424+
const char8* end;
14241425
switch (this->peek().type) {
14251426
case token_type::right_square:
14261427
if (subscript->kind() == expression_kind::_invalid) {
@@ -1431,18 +1432,16 @@ expression* parser::parse_index_expression_remainder(expression* lhs) {
14311432
right_square_span.end()),
14321433
});
14331434
}
1435+
end = this->peek().end;
1436+
this->skip();
14341437
break;
14351438
case token_type::end_of_file:
1439+
default:
14361440
this->error_reporter_->report(
14371441
error_unmatched_indexing_bracket{.left_square = left_square_span});
1438-
break;
1439-
default:
1440-
QLJS_PARSER_UNIMPLEMENTED();
1442+
end = this->lexer_.end_of_previous_token();
14411443
break;
14421444
}
1443-
1444-
const char8* end = this->peek().end;
1445-
this->skip();
14461445
return this->make_expression<expression::index>(lhs, subscript, end);
14471446
}
14481447

test/test-parse-expression.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,16 @@ TEST_F(test_parse_expression, parse_unclosed_indexing_expression) {
856856
error_unmatched_indexing_bracket, left_square,
857857
offsets_matcher(p.code(), strlen(u8"xs"), u8"["))));
858858
}
859+
860+
{
861+
test_parser p(u8"(xs[i)"_sv);
862+
expression* ast = p.parse_expression();
863+
EXPECT_EQ(summarize(ast), "index(var xs, var i)");
864+
EXPECT_THAT(p.errors(),
865+
ElementsAre(ERROR_TYPE_FIELD(
866+
error_unmatched_indexing_bracket, left_square,
867+
offsets_matcher(p.code(), strlen(u8"(xs"), u8"["))));
868+
}
859869
}
860870

861871
TEST_F(test_parse_expression, empty_indexing_expression) {

0 commit comments

Comments
 (0)