Skip to content

Commit 77cb08d

Browse files
committed
[Parse] Better recover from regex error
Instead of returning a parser error, which results in generic parser recovery that skips until the next decl, return an `ErrorExpr` so we can continue parsing.
1 parent 6d97905 commit 77cb08d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/Parse/ParseRegex.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ ParserResult<Expr> Parser::parseExprRegexLiteral() {
5151
regexLiteralParsingFn(regexText.str().c_str(), &errorStr, &version,
5252
/*captureStructureOut*/ capturesBuf.data(),
5353
/*captureStructureSize*/ capturesBuf.size());
54+
auto loc = consumeToken();
5455
if (errorStr) {
55-
diagnose(Tok, diag::regex_literal_parsing_error, errorStr);
56-
return makeParserError();
56+
diagnose(loc, diag::regex_literal_parsing_error, errorStr);
57+
return makeParserResult(new (Context) ErrorExpr(loc));
5758
}
58-
59-
auto loc = consumeToken();
6059
return makeParserResult(RegexLiteralExpr::createParsed(
6160
Context, loc, regexText, version, capturesBuf));
6261
}

test/StringProcessing/Parse/regex_parse_error.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// RUN: %target-typecheck-verify-swift -enable-experimental-string-processing
22
// REQUIRES: swift_in_compiler
33

4+
_ = re'(' // expected-error {{expected ')'}}
5+
6+
// FIXME: Should be 'group openings'
7+
_ = re')' // expected-error {{closing ')' does not balance any groups openings}}
8+
49
let s = #/\\/''/ // expected-error {{unterminated regex literal}}
510
_ = #|\| // expected-error {{unterminated regex literal}}
611
_ = #// // expected-error {{unterminated regex literal}}

0 commit comments

Comments
 (0)