RegExp: more clear error message

GitOrigin-RevId: aa6b5d2de81037f7291867c314dbdc1b5cd9549e
This commit is contained in:
Bas Leijdekkers
2021-12-18 17:05:28 +01:00
committed by intellij-monorepo-bot
parent ccc210eeb2
commit 6fecec667a
20 changed files with 46 additions and 38 deletions

View File

@@ -27,6 +27,7 @@ error.back.reference.is.nested.into.the.capturing.group.it.refers.to=Back refere
error.conditional.group.reference.not.allowed.inside.lookbehind=Conditional group reference not allowed inside lookbehind
error.conditionals.are.not.supported.in.this.regex.dialect=Conditionals are not supported in this regex dialect
error.dangling.metacharacter=Dangling quantifier ''{0}''
error.dangling.opening.bracket=Unexpected start of quantifier '{'
error.embedded.comments.are.not.supported.in.this.regex.dialect=Embedded comments are not supported in this regex dialect
error.empty.group=Empty group
error.group.reference.is.nested.into.the.named.group.it.refers.to=Group reference is nested into the named group it refers to

View File

@@ -192,7 +192,7 @@ public class RegExpParser implements PsiParser, LightPsiParser {
}
else {
if (RegExpTT.QUANTIFIERS.contains(builder.getTokenType())) {
builder.error(RegExpBundle.message("error.dangling.metacharacter"));
builder.error(RegExpBundle.message("error.dangling.metacharacter", builder.getTokenText()));
}
}
}
@@ -600,11 +600,22 @@ public class RegExpParser implements PsiParser, LightPsiParser {
private static void patternExpected(PsiBuilder builder) {
final IElementType token = builder.getTokenType();
if (token == RegExpTT.GROUP_END) {
builder.error(RegExpBundle.message("parse.error.unmatched.closing.parenthesis"));
if (token == RegExpTT.GROUP_END || token == RegExpTT.RBRACE || token == RegExpTT.CLASS_END) {
builder.error(RegExpBundle.message("parse.error.unmatched.closing.bracket", builder.getTokenText()));
}
else if (RegExpTT.QUANTIFIERS.contains(token) || token == RegExpTT.RBRACE || token == RegExpTT.CLASS_END) {
builder.error(RegExpBundle.message("error.dangling.metacharacter"));
else if (token == RegExpTT.LBRACE) {
builder.error(RegExpBundle.message("error.dangling.opening.bracket"));
// try to recover
builder.advanceLexer();
while (builder.getTokenType() == RegExpTT.NUMBER || builder.getTokenType() == RegExpTT.COMMA) {
builder.advanceLexer();
}
if (builder.getTokenType() == RegExpTT.RBRACE) {
builder.advanceLexer();
}
}
else if (RegExpTT.QUANTIFIERS.contains(token)) {
builder.error(RegExpBundle.message("error.dangling.metacharacter", builder.getTokenText()));
}
else {
builder.error(RegExpBundle.message("parse.error.pattern.expected"));

View File

@@ -357,8 +357,9 @@ public final class RegExpAnnotator extends RegExpElementVisitor implements Annot
@Override
public void visitRegExpClosure(RegExpClosure closure) {
if (closure.getAtom() instanceof RegExpSetOptions) {
myHolder.newAnnotation(HighlightSeverity.ERROR, RegExpBundle.message("error.dangling.metacharacter"))
.range(closure.getQuantifier())
final RegExpQuantifier quantifier = closure.getQuantifier();
myHolder.newAnnotation(HighlightSeverity.ERROR, RegExpBundle.message("error.dangling.metacharacter", quantifier.getUnescapedText()))
.range(quantifier)
.create();
}
}

View File

@@ -1,6 +1,6 @@
JS_UNICODE_REGEXP_FILE
RegExpPatternImpl: <{>
RegExpBranchImpl: <{>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Unexpected start of quantifier '{'
<empty list>
PsiElement(LBRACE)('{')

View File

@@ -1,6 +1,6 @@
JS_UNICODE_REGEXP_FILE
RegExpPatternImpl: <}>
RegExpBranchImpl: <}>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Unmatched closing '}'
<empty list>
PsiElement(RBRACE)('}')

View File

@@ -1,6 +1,6 @@
JS_UNICODE_REGEXP_FILE
RegExpPatternImpl: <]>
RegExpBranchImpl: <]>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Unmatched closing ']'
<empty list>
PsiElement(CLASS_END)(']')

View File

@@ -1,6 +1,6 @@
REGEXP_FILE
RegExpPatternImpl: <{>
RegExpBranchImpl: <{>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Unexpected start of quantifier '{'
<empty list>
PsiElement(LBRACE)('{')

View File

@@ -8,7 +8,7 @@ REGEXP_FILE
PsiElement(COLON)(':')
RegExpPatternImpl: <*>
RegExpBranchImpl: <*>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')
PsiElement(GROUP_END)(')')

View File

@@ -7,7 +7,7 @@ REGEXP_FILE
PsiElement(GT)('>')
RegExpPatternImpl: <{>
RegExpBranchImpl: <{>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Unexpected start of quantifier '{'
<empty list>
PsiElement(LBRACE)('{')
PsiErrorElement:')' expected

View File

@@ -32,7 +32,7 @@ REGEXP_FILE
PsiElement(CHARACTER)('n')
PsiElement(UNION)('|')
RegExpBranchImpl: <{>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Unexpected start of quantifier '{'
<empty list>
PsiElement(LBRACE)('{')
PsiErrorElement:')' expected

View File

@@ -5,7 +5,7 @@ REGEXP_FILE
PsiElement(GROUP_BEGIN)('(')
RegExpPatternImpl: <*>
RegExpBranchImpl: <*>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')
PsiElement(GROUP_END)(')')

View File

@@ -5,6 +5,6 @@ REGEXP_FILE
PsiElement(CHARACTER)('a')
PsiElement(UNION)('|')
RegExpBranchImpl: <*>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')

View File

@@ -6,6 +6,6 @@ REGEXP_FILE
PsiElement(CHARACTER)('1')
RegExpQuantifierImpl: <*>
PsiElement(STAR)('*')
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')

View File

@@ -6,6 +6,6 @@ REGEXP_FILE
PsiElement(CHARACTER)('a')
RegExpQuantifierImpl: <*>
PsiElement(STAR)('*')
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')

View File

@@ -1,15 +1,10 @@
REGEXP_FILE
RegExpPatternImpl: <{1,2>
RegExpBranchImpl: <{1,2>
PsiErrorElement:Dangling metacharacter
RegExpPatternImpl: <{1,2}>
RegExpBranchImpl: <{1,2}>
PsiErrorElement:Unexpected start of quantifier '{'
<empty list>
PsiElement(LBRACE)('{')
RegExpCharImpl: <1>
PsiElement(CHARACTER)('1')
RegExpCharImpl: <,>
PsiElement(CHARACTER)(',')
RegExpCharImpl: <2>
PsiElement(CHARACTER)('2')
PsiErrorElement:Dangling metacharacter
<empty list>
PsiElement(RBRACE)('}')
PsiElement(NUMBER)('1')
PsiElement(COMMA)(',')
PsiElement(NUMBER)('2')
PsiElement(RBRACE)('}')

View File

@@ -1,7 +1,7 @@
REGEXP_FILE
RegExpPatternImpl: <*a>
RegExpBranchImpl: <*a>
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')
RegExpCharImpl: <a>

View File

@@ -6,6 +6,6 @@ REGEXP_FILE
PsiElement(CHARACTER)('a')
RegExpQuantifierImpl: <*>
PsiElement(STAR)('*')
PsiErrorElement:Dangling metacharacter
PsiErrorElement:Dangling quantifier '*'
<empty list>
PsiElement(STAR)('*')

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeInsight;
import com.intellij.ide.highlighter.JavaFileType;
@@ -214,9 +214,9 @@ public class RegExpHighlightingTest extends LightJavaCodeInsightFixtureTestCase
}
public void testOptions() {
doTest("(?i)<error descr=\"Dangling metacharacter\">+</error>");
doTest("(?i)<error descr=\"Dangling metacharacter\">*</error>");
doTest("(?i)<error descr=\"Dangling metacharacter\">{5,6}</error>");
doTest("(?i)<error descr=\"Dangling quantifier '+'\">+</error>");
doTest("(?i)<error descr=\"Dangling quantifier '*'\">*</error>");
doTest("(?i)<error descr=\"Dangling quantifier '{5,6}'\">{5,6}</error>");
}
public void testLookbehind() {

View File

@@ -2,7 +2,7 @@ class SuspicousRegexExpressionArgument {{
"a.s.d.f".split(<warning descr="Suspicious regex expression \".\" in call to 'split()'"><caret>"."</warning>);
"vb|amna".replaceAll(<warning descr="Suspicious regex expression \"|\" in call to 'replaceAll()'">"|"</warning>, "-");
"1+2+3".split("<error descr="Dangling metacharacter">+</error>");
"1+2+3".split("<error descr="Dangling quantifier '+'">+</error>");
"one two".split(" ");
"[][][]".split("]");
"{}{}{}".split("}");

View File

@@ -1,7 +1,7 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="TestType">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]+-([0-9]+|<error descr="Dangling metacharacter">*</error>)"/>
<xs:pattern value="[0-9]+-([0-9]+|<error descr="Dangling quantifier '*'">*</error>)"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="TestType2">