From 4e98dd9ce413e3b44ea94e897b97adad435a59c4 Mon Sep 17 00:00:00 2001 From: Renaud Pawlak Date: Tue, 8 Nov 2016 14:58:58 +0100 Subject: [PATCH] improve error reporting when the scanner breaks --- .../org/jsweet/transpiler/JSweetProblem.java | 13 +++++++-- .../transpiler/util/AbstractTreeScanner.java | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jsweet/transpiler/JSweetProblem.java b/src/main/java/org/jsweet/transpiler/JSweetProblem.java index 9364f49b..e1c4c1dd 100644 --- a/src/main/java/org/jsweet/transpiler/JSweetProblem.java +++ b/src/main/java/org/jsweet/transpiler/JSweetProblem.java @@ -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; } diff --git a/src/main/java/org/jsweet/transpiler/util/AbstractTreeScanner.java b/src/main/java/org/jsweet/transpiler/util/AbstractTreeScanner.java index 738318a7..40400958 100644 --- a/src/main/java/org/jsweet/transpiler/util/AbstractTreeScanner.java +++ b/src/main/java/org/jsweet/transpiler/util/AbstractTreeScanner.java @@ -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) { }