From ece17e2a556246d5854ca7e73b9ddd68326cf5f5 Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Wed, 30 May 2012 22:02:00 +0400 Subject: [PATCH] EA-36229 - NFE: RegExpQuantifierImpl.getCount --- .../lang/regexp/psi/RegExpQuantifier.java | 24 ++++++++------ .../regexp/psi/impl/RegExpQuantifierImpl.java | 22 +++++++------ .../regexp/validation/RegExpAnnotator.java | 31 +++++++++++++------ 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/RegExpSupport/src/org/intellij/lang/regexp/psi/RegExpQuantifier.java b/RegExpSupport/src/org/intellij/lang/regexp/psi/RegExpQuantifier.java index c18224642318..d3f14138f20c 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/psi/RegExpQuantifier.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/psi/RegExpQuantifier.java @@ -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; } } diff --git a/RegExpSupport/src/org/intellij/lang/regexp/psi/impl/RegExpQuantifierImpl.java b/RegExpSupport/src/org/intellij/lang/regexp/psi/impl/RegExpQuantifierImpl.java index dccb75dbe681..b450f607c826 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/psi/impl/RegExpQuantifierImpl.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/psi/impl/RegExpQuantifierImpl.java @@ -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; } } diff --git a/RegExpSupport/src/org/intellij/lang/regexp/validation/RegExpAnnotator.java b/RegExpSupport/src/org/intellij/lang/regexp/validation/RegExpAnnotator.java index 359a4871acf6..eab0fbd74389 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/validation/RegExpAnnotator.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/validation/RegExpAnnotator.java @@ -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);