feat: filenames in errors
This commit is contained in:
@@ -2,6 +2,7 @@ package wacc
|
||||
|
||||
import wacc.ast.Position
|
||||
import wacc.types._
|
||||
import java.io.File
|
||||
|
||||
private val SYNTAX_ERROR = 100
|
||||
private val SEMANTIC_ERROR = 200
|
||||
@@ -18,13 +19,13 @@ enum Error {
|
||||
case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String)
|
||||
case InternalError(pos: Position, msg: String)
|
||||
|
||||
case SyntaxError(msg: String)
|
||||
case SyntaxError(file: File, msg: String)
|
||||
}
|
||||
|
||||
extension (e: Error) {
|
||||
def exitCode: Int = e match {
|
||||
case Error.SyntaxError(_) => SYNTAX_ERROR
|
||||
case _ => SEMANTIC_ERROR
|
||||
case Error.SyntaxError(_, _) => SYNTAX_ERROR
|
||||
case _ => SEMANTIC_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +39,22 @@ extension (e: Error) {
|
||||
def formatError(error: Error)(using errorContent: String): String = {
|
||||
val sb = new StringBuilder()
|
||||
|
||||
/** Format the file of an error
|
||||
*
|
||||
* @param file
|
||||
* File of the error
|
||||
*/
|
||||
def formatFile(file: File): Unit = {
|
||||
sb.append(s"File: ${file.toPath.toRealPath()}\n")
|
||||
}
|
||||
|
||||
/** Function to format the position of an error
|
||||
*
|
||||
* @param pos
|
||||
* Position of the error
|
||||
*/
|
||||
def formatPosition(pos: Position): Unit = {
|
||||
formatFile(pos.file)
|
||||
sb.append(s"(line ${pos.line}, column ${pos.column}):\n")
|
||||
}
|
||||
|
||||
@@ -67,7 +78,7 @@ def formatError(error: Error)(using errorContent: String): String = {
|
||||
}
|
||||
|
||||
error match {
|
||||
case Error.SyntaxError(_) =>
|
||||
case Error.SyntaxError(_, _) =>
|
||||
sb.append("Syntax error:\n")
|
||||
case _ =>
|
||||
sb.append("Semantic error:\n")
|
||||
@@ -105,11 +116,11 @@ def formatError(error: Error)(using errorContent: String): String = {
|
||||
formatPosition(pos)
|
||||
sb.append(s"Internal error: $msg")
|
||||
formatHighlight(pos, 1)
|
||||
case Error.SyntaxError(msg) =>
|
||||
case Error.SyntaxError(file, msg) =>
|
||||
formatFile(file)
|
||||
sb.append(msg)
|
||||
sb.append("\n")
|
||||
}
|
||||
|
||||
sb.toString()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user