diff --git a/src/main/wacc/Main.scala b/src/main/wacc/Main.scala index add3eec..48166c3 100644 --- a/src/main/wacc/Main.scala +++ b/src/main/wacc/Main.scala @@ -73,7 +73,7 @@ def frontend( file: File ): Either[NonEmptyList[Error], microWacc.Program] = parser.parse(contents) match { - case Failure(msg) => Left(NonEmptyList.one(Error.SyntaxError(msg))) + case Failure(msg) => Left(NonEmptyList.one(Error.SyntaxError(file, msg))) case Success(fn) => val ast.PartialProgram(_, prog) = fn(file) given errors: mutable.Builder[Error, List[Error]] = List.newBuilder @@ -118,7 +118,7 @@ def compile( _ <- logAction(s"Compilation failed for $filePath\nExit code: $code") _ <- IO.blocking( // Explicit println since we want this to always show without logger thread info e.t.c. - println(s"Compilation failed for ${filePath.toAbsolutePath}:\n$errorMsg") + println(s"Compilation failed for ${file.toPath.toRealPath()}:\n$errorMsg") ) } yield code diff --git a/src/main/wacc/frontend/Error.scala b/src/main/wacc/frontend/Error.scala index e515494..ed113ae 100644 --- a/src/main/wacc/frontend/Error.scala +++ b/src/main/wacc/frontend/Error.scala @@ -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() - }