EA-36229 - NFE: RegExpQuantifierImpl.getCount

This commit is contained in:
Gregory.Shrago
2012-05-30 22:05:01 +04:00
parent 1c944c5957
commit ece17e2a55
3 changed files with 47 additions and 30 deletions
@@ -38,30 +38,34 @@ public interface RegExpQuantifier extends RegExpAtom {
Type getType();
interface Count {
int getMin();
int getMax();
@NotNull
String getMin();
@NotNull
String getMax();
}
enum SimpleCount implements Count {
/** ? */
ONE_OR_ZERO(0, 1),
ONE_OR_ZERO("0", "1"),
/** * */
ZERO_OR_MORE(0, Integer.MAX_VALUE),
ZERO_OR_MORE("0", ""),
/** + */
ONE_OR_MORE(1, Integer.MAX_VALUE),;
ONE_OR_MORE("1", "");
private final int myMin;
private final int myMax;
private final String myMin;
private final String myMax;
SimpleCount(int min, int max) {
SimpleCount(String min, String max) {
myMin = min;
myMax = max;
}
public int getMin() {
@NotNull
public String getMin() {
return myMin;
}
public int getMax() {
@NotNull
public String getMax() {
return myMax;
}
}
@@ -58,19 +58,19 @@ public class RegExpQuantifierImpl extends RegExpElementImpl implements RegExpQua
} else if (type == RegExpTT.LBRACE) {
final ASTNode[] numbers = getNode().getChildren(TokenSet.create(RegExpTT.NUMBER));
if (numbers.length >= 1) {
final int min = Integer.parseInt(numbers[0].getText());
final int max;
final String min = numbers[0].getText();
final String max;
if (numbers.length == 2) {
max = Integer.parseInt(numbers[1].getText());
max = numbers[1].getText();
} else if (getNode().findChildByType(RegExpTT.COMMA) != null) {
max = Integer.MAX_VALUE;
max = "";
} else {
max = min;
}
return new RepeatedCount(min, max);
}
// syntactically incorrect
return new RepeatedCount(-1, -1);
return new RepeatedCount("", "");
}
assert false;
@@ -92,19 +92,21 @@ public class RegExpQuantifierImpl extends RegExpElementImpl implements RegExpQua
}
private static class RepeatedCount implements RegExpQuantifier.Count {
private final int myMin;
private final int myMax;
private final String myMin;
private final String myMax;
public RepeatedCount(int min, int max) {
public RepeatedCount(@NotNull String min, @NotNull String max) {
myMin = min;
myMax = max;
}
public int getMin() {
@NotNull
public String getMin() {
return myMin;
}
public int getMax() {
@NotNull
public String getMax() {
return myMax;
}
}
@@ -34,6 +34,8 @@ import org.intellij.lang.regexp.psi.impl.RegExpPropertyImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigInteger;
public final class RegExpAnnotator extends RegExpElementVisitor implements Annotator {
private AnnotationHolder myHolder;
@@ -236,13 +238,10 @@ public final class RegExpAnnotator extends RegExpElementVisitor implements Annot
public void visitRegExpQuantifier(RegExpQuantifier quantifier) {
final RegExpQuantifier.Count count = quantifier.getCount();
if (!(count instanceof RegExpQuantifier.SimpleCount)) {
final int min = count.getMin();
final int max = count.getMax();
if (max < min) {
myHolder.createErrorAnnotation(quantifier, "Illegal repetition range");
}
else if (max == min) {
if (max == 1) { // TODO: is this safe when reluctant or possesive modifier is present?
String min = count.getMin();
String max = count.getMax();
if (max.equals(min)) {
if ("1".equals(max)) { // TODO: is this safe when reluctant or possesive modifier is present?
final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Single repetition");
registerFix(a, new SimplifyQuantifierAction(quantifier, null));
}
@@ -254,18 +253,30 @@ public final class RegExpAnnotator extends RegExpElementVisitor implements Annot
}
}
}
else if (min == 0 && max == 1) {
else if ("0".equals(min) && "1".equals(max)) {
final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Repetition range replaceable by '?'");
registerFix(a, new SimplifyQuantifierAction(quantifier, "?"));
}
else if (min == 0 && max == Integer.MAX_VALUE) {
else if ("0".equals(min) && max.isEmpty()) {
final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Repetition range replaceable by '*'");
registerFix(a, new SimplifyQuantifierAction(quantifier, "*"));
}
else if (min == 1 && max == Integer.MAX_VALUE) {
else if ("1".equals(min) && max.isEmpty()) {
final Annotation a = myHolder.createWeakWarningAnnotation(quantifier, "Repetition range replaceable by '+'");
registerFix(a, new SimplifyQuantifierAction(quantifier, "+"));
}
else if (!min.isEmpty() && !max.isEmpty()) {
try {
BigInteger minInt = new BigInteger(min);
BigInteger maxInt = new BigInteger(max);
if (maxInt.compareTo(minInt) < 0) {
myHolder.createErrorAnnotation(quantifier, "Illegal repetition range");
}
}
catch (NumberFormatException ex) {
myHolder.createErrorAnnotation(quantifier, "Illegal repetition value");
}
}
}
if (quantifier.getType() == RegExpQuantifier.Type.POSSESSIVE) {
RegExpLanguageHost host = findRegExpHost(quantifier);