Skip to content

Commit

Permalink
Merge pull request #39577 from mohanvive/regex-fix
Browse files Browse the repository at this point in the history
Fix regex handling for cases with slash symbol
  • Loading branch information
mohanvive authored Feb 14, 2023
2 parents 175ced2 + 569198f commit 1411e7e
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ private STToken readTokenInReDisjunction() {
case LexerTerminals.QUESTION_MARK:
return getRegExpSyntaxToken(SyntaxKind.QUESTION_MARK_TOKEN);
case LexerTerminals.BACKSLASH:
// This is used to identify `\-` in ReCharSetAtomNoDash. It's an invalid node if it's in any other
// position.
if (peek() == LexerTerminals.MINUS) {
this.reader.advance();
return getRegExpSyntaxToken(SyntaxKind.ESCAPED_MINUS_TOKEN);
}
return processReEscape();
// Start parsing ReSyntaxChar character class [[^] [ReCharSet]].
case LexerTerminals.OPEN_BRACKET:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,15 @@ private STNode parseCharSetAtom(STToken nextToken, STNode prevNode) {
case RE_NUMERIC_ESCAPE:
case RE_CONTROL_ESCAPE:
return parseChars();
case ESCAPED_MINUS_TOKEN:
return consume();
case BACK_SLASH_TOKEN:
STToken token = peek(2);
if (token.kind == SyntaxKind.RE_LITERAL_CHAR &&
token.text().equals(Character.toString(LexerTerminals.MINUS))) {
consume();
STNode minusToken = consume();
return STNodeFactory.createToken(SyntaxKind.ESCAPED_MINUS_TOKEN, minusToken.leadingMinutiae(),
minusToken.trailingMinutiae());
}
return parseReEscape();
default:
STNode consumedToken = consume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,115 @@
]
}
]
},
{
"kind": "ASSIGNMENT_STATEMENT",
"children": [
{
"kind": "SIMPLE_NAME_REFERENCE",
"children": [
{
"kind": "IDENTIFIER_TOKEN",
"value": "x",
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
],
"trailingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
]
}
]
},
{
"kind": "EQUAL_TOKEN",
"trailingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
]
},
{
"kind": "REGEX_TEMPLATE_EXPRESSION",
"children": [
{
"kind": "RE_KEYWORD",
"trailingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
]
},
{
"kind": "BACKTICK_TOKEN"
},
{
"kind": "LIST",
"children": [
{
"kind": "RE_SEQUENCE",
"children": [
{
"kind": "LIST",
"children": [
{
"kind": "RE_ATOM_QUANTIFIER",
"children": [
{
"kind": "RE_QUOTE_ESCAPE",
"children": [
{
"kind": "BACK_SLASH_TOKEN"
},
{
"kind": "BACK_SLASH_TOKEN"
}
]
}
]
},
{
"kind": "RE_ATOM_QUANTIFIER",
"children": [
{
"kind": "RE_LITERAL_CHAR_DOT_OR_ESCAPE",
"children": [
{
"kind": "RE_LITERAL_CHAR",
"value": "-"
}
]
}
]
}
]
}
]
}
]
},
{
"kind": "BACKTICK_TOKEN"
}
]
},
{
"kind": "SEMICOLON_TOKEN",
"trailingMinutiae": [
{
"kind": "END_OF_LINE_MINUTIAE",
"value": "\n"
}
]
}
]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,152 @@
}
]
},
{
"kind": "ASSIGNMENT_STATEMENT",
"children": [
{
"kind": "SIMPLE_NAME_REFERENCE",
"children": [
{
"kind": "IDENTIFIER_TOKEN",
"value": "x",
"leadingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
],
"trailingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
]
}
]
},
{
"kind": "EQUAL_TOKEN",
"trailingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
]
},
{
"kind": "REGEX_TEMPLATE_EXPRESSION",
"children": [
{
"kind": "RE_KEYWORD",
"trailingMinutiae": [
{
"kind": "WHITESPACE_MINUTIAE",
"value": " "
}
]
},
{
"kind": "BACKTICK_TOKEN"
},
{
"kind": "LIST",
"children": [
{
"kind": "RE_SEQUENCE",
"children": [
{
"kind": "LIST",
"children": [
{
"kind": "RE_ATOM_QUANTIFIER",
"children": [
{
"kind": "RE_CAPTURING_GROUP",
"children": [
{
"kind": "OPEN_PAREN_TOKEN"
},
{
"kind": "LIST",
"children": [
{
"kind": "RE_SEQUENCE",
"children": [
{
"kind": "LIST",
"children": [
{
"kind": "RE_ATOM_QUANTIFIER",
"children": [
{
"kind": "RE_CHARACTER_CLASS",
"children": [
{
"kind": "OPEN_BRACKET_TOKEN"
},
{
"kind": "RE_CHAR_SET_ATOM_WITH_RE_CHAR_SET_NO_DASH",
"children": [
{
"kind": "RE_QUOTE_ESCAPE",
"children": [
{
"kind": "BACK_SLASH_TOKEN"
},
{
"kind": "BACK_SLASH_TOKEN"
}
]
},
{
"kind": "RE_LITERAL_CHAR",
"value": "-"
}
]
},
{
"kind": "CLOSE_BRACKET_TOKEN"
}
]
}
]
}
]
}
]
}
]
},
{
"kind": "CLOSE_PAREN_TOKEN"
}
]
}
]
}
]
}
]
}
]
},
{
"kind": "BACKTICK_TOKEN"
}
]
},
{
"kind": "SEMICOLON_TOKEN",
"trailingMinutiae": [
{
"kind": "END_OF_LINE_MINUTIAE",
"value": "\n"
}
]
}
]
},
{
"kind": "ASSIGNMENT_STATEMENT",
"children": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ function foo() {
x = re `[\{]`;
x = re `[^\}]`;
x = re `[^\|]`;
x = re `\\-`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ function foo() {
x = re `([\t]*)`;
x = re `([\-d-hM-N\tQ-T])`;
x = re `([\-])`;
x = re `([\\-])`;
x = re `(x|y|z*)`;
}

0 comments on commit 1411e7e

Please sign in to comment.