Skip to content

Commit

Permalink
Merge pull request #40607 from LakshanWeerasinghe/fix-#40499
Browse files Browse the repository at this point in the history
Fix compiler crash for interpolations inside regex char sets
  • Loading branch information
LakshanWeerasinghe authored Jul 6, 2023
2 parents 531f246 + 93b4834 commit 3775e22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,11 @@ private STToken readRegExpTemplateToken() {
shouldProcessInterpolations = false;
}
while (!reader.isEOF()) {
if (shouldProcessInterpolations && this.reader.peek() == LexerTerminals.BACKSLASH
&& this.reader.peek(1) == LexerTerminals.OPEN_BRACKET) {
// Escaped open brackets are not considered as the start of a no interpolation context.
reader.advance();
}
reader.advance();
nextChar = this.reader.peek();
switch (nextChar) {
Expand All @@ -1536,6 +1541,11 @@ private STToken readRegExpTemplateToken() {
case LexerTerminals.CLOSE_BRACKET:
shouldProcessInterpolations = true;
continue;
case LexerTerminals.BACKSLASH:
if (!shouldProcessInterpolations && this.reader.peek(1) == LexerTerminals.CLOSE_BRACKET) {
reader.advance();
}
continue;
default:
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ private STNode parseCharSetAtom(STToken nextToken, STNode prevNode) {
return STNodeFactory.createToken(SyntaxKind.ESCAPED_MINUS_TOKEN, minusToken.leadingMinutiae(),
minusToken.trailingMinutiae());
}
if (token.kind == SyntaxKind.CLOSE_BRACKET_TOKEN) {
this.tokenReader.startMode(ParserMode.RE_CHAR_CLASS);
}
return parseReEscape();
default:
STNode consumedToken = consume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ function testRegExpValueWithCharacterClass() {

string:RegExp x7 = re `[\p{sc=Latin}\p{gc=Lu}\p{Lt}\tA\)]??`;
assertEquality("[\\p{sc=Latin}\\p{gc=Lu}\\p{Lt}\\tA\\)]??", x7.toString());

string:RegExp x8 = re `abc[a-z\]A-Z${"abc"}]`;
assertEquality("abc[a-z\\]A-Z${\"abc\"}]", x8.toString());

string:RegExp x9 = re `abc\[a-z\]A-Z${"abc"}`;
assertEquality("abc\\[a-z\\]A-Zabc", x9.toString());

string:RegExp x10 = re `\[${"a"}\]`;
assertEquality("\\[a\\]", x10.toString());
}

function testRegExpValueWithCharacterClass2() {
Expand Down

0 comments on commit 3775e22

Please sign in to comment.