Properly display compiler error messages

parent c942f19a
Pipeline #13968 failed with stages
in 17 seconds
......@@ -721,6 +721,7 @@ internal class MainPane(private val stage: Window) : AnchorPane() {
}
}.apply {
onError { Platform.runLater { ExceptionAlert(it).showAndWait() } }
onCompilerError { System.err.println(it) }
}
if (hasChanged()) {
......
package pseuco.javaCompiler
/**
* An exception indicating an error within a pseuCo program.
* Holds either a [message] or a [cause].
*
* @constructor Creates a new exception.
* @param message The compiler error message.
* @param cause The actual exception generated by the compiler.
*/
class PseuCoCompilerException private constructor(message: String?, cause: Throwable?) : Exception(message, cause) {
/**
* Creates a new exception holding a compiler error message.
*
* @author Konstantin Kopper
* @since 2.1
*/
constructor(message: String) : this(message, null)
/**
* Creates a new exception holding an exception thrown by the compiler.
*
* @author Konstantin Kopper
* @since 2.1
*/
constructor(cause: Throwable) : this(null, cause)
override fun toString(): String = message ?: ""
}
......@@ -7,7 +7,10 @@ import codeGen.MainCodeGen
import fxGui.Main
import org.apache.commons.io.FileUtils
import pseuco.PseuCoTask
import tree.ParseException
import tree.PseuCoParser
import typeChecker.TypeEnvironmentException
import typeChecker.TypeIllegalException
import util.FileUtilities
import util.ZipUtilities
import java.io.File
......@@ -70,6 +73,25 @@ abstract class PseuCoCompilerTask(
onError = action
}
/**
* Callback to be called if an error was generated by the compiler.
* Default value just prints the stack trace.
*
* @author Konstantin Kopper
* @since 2.1
*/
protected var onCompilerError: (Throwable) -> Unit = { e -> e.printStackTrace() }
/**
* Assigns [action] to [onCompilerError], i.e. [action] is executed whenever the compiler generates an error.
*
* @author Konstantin Kopper
* @since 2.1
*/
fun onCompilerError(action: (Throwable) -> Unit) {
onCompilerError = action
}
/**
* Initialize the pseuCo task.
* Useful to configure compiler.
......@@ -119,7 +141,7 @@ abstract class PseuCoCompilerTask(
* @author Konstantin Kopper
* @since 2.0.0
*/
@Throws(Exception::class)
@Throws(PseuCoCompilerError::class, Exception::class)
protected open fun parsePseuCoProgram() {
// Call the pseuco parser. This will generate Java code in the 'include' folder.
val args = arrayOf(MainCodeGen.getPlatformIndependentPath(this.toBeExecuted.absolutePath))
......@@ -128,7 +150,17 @@ abstract class PseuCoCompilerTask(
javaClass.classLoader.setPackageAssertionStatus("tree", false)
javaClass.classLoader.setPackageAssertionStatus("codeGen", false)
PseuCoParser.main(args)
try {
PseuCoParser.main(args)
} catch (e: Exception) {
if (Main.DEBUG && System.err != Main.STANDARD_ERR)
e.printStackTrace(Main.STANDARD_ERR)
throw when (e) {
is TypeIllegalException, is TypeEnvironmentException, is ParseException -> PseuCoCompilerError(e)
else -> e
}
}
}
/**
......@@ -154,20 +186,20 @@ abstract class PseuCoCompilerTask(
val fileManager = compiler.getStandardFileManager(diagnostics, null, null)
val files = listOf(*this.includeDir.listFiles())
val files = listOf(*this.includeDir.listFiles()!!)
val task = compiler.getTask(PrintWriter(Main.STANDARD_ERR), fileManager, diagnostics,
null, null, fileManager.getJavaFileObjectsFromFiles(files))
null, null, fileManager.getJavaFileObjectsFromFiles(files))
return task.call().also {
if (Main.DEBUG)
Main.STANDARD_OUT.println("${if (it) "Successfully compiled" else "Could not compile"} generated Java code.")
if (!it) {
diagnostics.diagnostics.forEach {
System.err.println(it)
if (Main.DEBUG && System.err != Main.STANDARD_ERR)
Main.STANDARD_ERR.println(it)
if (Main.DEBUG && System.err != Main.STANDARD_ERR) {
diagnostics.diagnostics.forEach((Main.STANDARD_ERR)::println)
}
throw PseuCoCompilerError(diagnostics.diagnostics.joinToString(separator = "\n") { d -> d.toString() })
}
}
}
......@@ -240,6 +272,8 @@ abstract class PseuCoCompilerTask(
parsePseuCoProgram()
compileGeneratedJavaCode().takeIf { it } ?: return
executeGeneratedJavaCode()
} catch (e: PseuCoCompilerError) {
onCompilerError(e.cause ?: e)
} catch (e: Exception) {
onError(e)
} finally {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment