improve error reporting when the scanner breaks

This commit is contained in:
Renaud Pawlak 2016-11-08 14:58:58 +01:00
parent 34074893b9
commit 4e98dd9ce4
2 changed files with 38 additions and 3 deletions

View File

@ -34,6 +34,11 @@ public enum JSweetProblem {
* Raised when the Java compiler reports an error.
*/
INTERNAL_JAVA_ERROR(Severity.ERROR),
/**
* Raised when the transpiler meets an error while scanning the program's
* AST.
*/
INTERNAL_TRANSPILER_ERROR(Severity.ERROR),
/**
* Raised when the Tsc transpiler reports an error.
*/
@ -266,10 +271,10 @@ public enum JSweetProblem {
* Raised when a class tries to extend a Globals class.
*/
GLOBALS_CLASS_CANNOT_BE_SUBCLASSED(Severity.ERROR),
/**
/**
* Raised when trying to access this from scope it isn't defined.
*/
CANNOT_ACCESS_THIS(Severity.ERROR),
CANNOT_ACCESS_THIS(Severity.ERROR),
/**
* Raised when invoking a static method on this (this is allowed in Java,
* but not in JSweet).
@ -422,7 +427,7 @@ public enum JSweetProblem {
return String.format("globals classes cannot extend any class", params);
case GLOBALS_CLASS_CANNOT_BE_SUBCLASSED:
return String.format("globals classes cannot be subclassed", params);
case CANNOT_ACCESS_THIS:
case CANNOT_ACCESS_THIS:
return String.format("'this' isn't defined in scope of %s", params);
case CANNOT_ACCESS_STATIC_MEMBER_ON_THIS:
return String.format("member '%s' is static and cannot be accessed on 'this'", params);
@ -432,6 +437,8 @@ public enum JSweetProblem {
return String.format("wrong key: method '$object' expects a list of key/value pairs as parameters, where keys are string literals", params);
case CYCLE_IN_STATIC_INITIALIZER_DEPENDENCIES:
return String.format("a cycle was detected in static intializers involving '%s'", params);
case INTERNAL_TRANSPILER_ERROR:
return String.format("internal transpiler error");
}
return null;
}

View File

@ -155,11 +155,39 @@ public abstract class AbstractTreeScanner extends TreeScanner {
} else {
throw rollback;
}
} catch (Exception e) {
report(tree, JSweetProblem.INTERNAL_TRANSPILER_ERROR);
dumpStackTrace();
} finally {
exit();
}
}
protected void dumpStackTrace() {
System.err.println("dumping transpiler's strack trace:");
for (int i = stack.size() - 1; i >= 0; i--) {
JCTree tree = stack.get(i);
if (tree == null) {
continue;
}
String str = tree.toString().trim();
int intialLength = str.length();
int index = str.indexOf('\n');
if (index > 0) {
str = str.substring(0, index + 1);
}
str = str.replace('\n', ' ');
str = str.substring(0, Math.min(str.length() - 1, 30));
System.err.print(" [" + stack.get(i).getClass().getSimpleName() + "] " + str + (str.length() < intialLength ? "..." : "") + " ("
+ compilationUnit.getSourceFile().getName() + ":");
if (diagnosticSource == null) {
System.err.println(tree.pos + ")");
} else {
System.err.println(diagnosticSource.getLineNumber(tree.pos) + ")");
}
}
}
protected void onRollbacked(JCTree target) {
}