Refactoring in CompileTimeChecker

This commit is contained in:
Natalia Ukhorskaya
2014-01-29 12:39:53 +04:00
parent d7f0e02b16
commit 420349bb95
2 changed files with 34 additions and 40 deletions
@@ -23,7 +23,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.DiagnosticFactory;
import org.jetbrains.jet.lang.diagnostics.rendering.DefaultErrorMessages;
import org.jetbrains.jet.lang.psi.JetConstantExpression;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.resolve.BindingTrace;
@@ -136,9 +135,9 @@ public class CompileTimeConstantChecker {
return false;
}
CompileTimeConstant<?> compileTimeConstant = parseCharValue(expression);
if (compileTimeConstant instanceof ErrorCharValueWithDiagnostic) {
return reportError(((ErrorCharValueWithDiagnostic) compileTimeConstant).getDiagnostic());
Diagnostic diagnostic = parseCharacter(expression).getDiagnostic();
if (diagnostic != null) {
return reportError(diagnostic);
}
return false;
}
@@ -151,30 +150,30 @@ public class CompileTimeConstantChecker {
}
@NotNull
private static CompileTimeConstant<?> parseCharValue(@NotNull JetConstantExpression expression) {
private static CharacterWithDiagnostic parseCharacter(@NotNull JetConstantExpression expression) {
String text = expression.getText();
// Strip the quotes
if (text.length() < 2 || text.charAt(0) != '\'' || text.charAt(text.length() - 1) != '\'') {
return createErrorValue(INCORRECT_CHARACTER_LITERAL.on(expression));
return createErrorCharacter(INCORRECT_CHARACTER_LITERAL.on(expression));
}
text = text.substring(1, text.length() - 1); // now there're no quotes
if (text.length() == 0) {
return createErrorValue(EMPTY_CHARACTER_LITERAL.on(expression));
return createErrorCharacter(EMPTY_CHARACTER_LITERAL.on(expression));
}
if (text.charAt(0) != '\\') {
// No escape
if (text.length() == 1) {
return new CharValue(text.charAt(0), true, true);
return new CharacterWithDiagnostic(text.charAt(0));
}
return createErrorValue(TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL.on(expression, expression));
return createErrorCharacter(TOO_MANY_CHARACTERS_IN_CHARACTER_LITERAL.on(expression, expression));
}
return escapedStringToCharValue(text, expression);
return escapedStringToCharacter(text, expression);
}
@NotNull
public static CompileTimeConstant<?> escapedStringToCharValue(@NotNull String text, @NotNull JetElement expression) {
public static CharacterWithDiagnostic escapedStringToCharacter(@NotNull String text, @NotNull JetElement expression) {
assert text.length() > 0 && text.charAt(0) == '\\' : "Only escaped sequences must be passed to this routine: " + text;
// Escape
@@ -189,13 +188,13 @@ public class CompileTimeConstantChecker {
if (escaped == null) {
return illegalEscape(expression);
}
return new CharValue(escaped, true, true);
return new CharacterWithDiagnostic(escaped);
case 5:
// unicode escape
if (escape.charAt(0) == 'u') {
try {
Integer intValue = Integer.valueOf(escape.substring(1), 16);
return new CharValue((char) intValue.intValue(), true, true);
return new CharacterWithDiagnostic((char) intValue.intValue());
} catch (NumberFormatException e) {
// Will be reported below
}
@@ -206,46 +205,41 @@ public class CompileTimeConstantChecker {
}
@NotNull
private static CompileTimeConstant<?> illegalEscape(@NotNull JetElement expression) {
return createErrorValue(ILLEGAL_ESCAPE.on(expression, expression));
private static CharacterWithDiagnostic illegalEscape(@NotNull JetElement expression) {
return createErrorCharacter(ILLEGAL_ESCAPE.on(expression, expression));
}
@NotNull
private static ErrorValue createErrorValue(@NotNull Diagnostic diagnostic) {
return new ErrorCharValueWithDiagnostic(diagnostic);
private static CharacterWithDiagnostic createErrorCharacter(@NotNull Diagnostic diagnostic) {
return new CharacterWithDiagnostic(diagnostic);
}
public static class ErrorCharValueWithDiagnostic extends ErrorValue {
private final Diagnostic diagnostic;
public static class CharacterWithDiagnostic {
private Diagnostic diagnostic;
private Character value;
public ErrorCharValueWithDiagnostic(@NotNull Diagnostic diagnostic) {
public CharacterWithDiagnostic(@NotNull Diagnostic diagnostic) {
this.diagnostic = diagnostic;
}
@NotNull
public CharacterWithDiagnostic(char value) {
this.value = value;
}
@Nullable
public Diagnostic getDiagnostic() {
return diagnostic;
}
@NotNull
@Override
public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return DefaultErrorMessages.RENDERER.render(diagnostic);
@Nullable
public Character getValue() {
return value;
}
}
@Nullable
public static Character parseChar(@NotNull JetConstantExpression expression) {
CompileTimeConstant<?> compileTimeConstant = parseCharValue(expression);
if (compileTimeConstant instanceof CharValue) {
return ((CharValue) compileTimeConstant).getValue();
}
return null;
return parseCharacter(expression).getValue();
}
@Nullable
@@ -24,9 +24,9 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.descriptors.impl.AnonymousFunctionDescriptor;
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.evaluate.ConstantExpressionEvaluator;
import org.jetbrains.jet.lang.psi.*;
@@ -74,7 +74,6 @@ import static org.jetbrains.jet.lang.diagnostics.Errors.*;
import static org.jetbrains.jet.lang.resolve.BindingContext.*;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getStaticNestedClassesScope;
import static org.jetbrains.jet.lang.resolve.calls.context.ContextDependency.INDEPENDENT;
import static org.jetbrains.jet.lang.resolve.constants.CompileTimeConstantChecker.ErrorCharValueWithDiagnostic;
import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
import static org.jetbrains.jet.lang.types.TypeUtils.*;
import static org.jetbrains.jet.lang.types.expressions.ControlStructureTypingUtils.createCallForSpecialConstruction;
@@ -1198,9 +1197,10 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
@Override
public void visitEscapeStringTemplateEntry(@NotNull JetEscapeStringTemplateEntry entry) {
CompileTimeConstant<?> compileTimeConstant = CompileTimeConstantChecker.escapedStringToCharValue(entry.getText(), entry);
if (compileTimeConstant instanceof ErrorCharValueWithDiagnostic) {
context.trace.report(((ErrorCharValueWithDiagnostic) compileTimeConstant).getDiagnostic());
CompileTimeConstantChecker.CharacterWithDiagnostic value = CompileTimeConstantChecker.escapedStringToCharacter(entry.getText(), entry);
Diagnostic diagnostic = value.getDiagnostic();
if (diagnostic != null) {
context.trace.report(diagnostic);
}
}
});