feat: filenames in errors
This commit is contained in:
@@ -73,7 +73,7 @@ def frontend(
|
|||||||
file: File
|
file: File
|
||||||
): Either[NonEmptyList[Error], microWacc.Program] =
|
): Either[NonEmptyList[Error], microWacc.Program] =
|
||||||
parser.parse(contents) match {
|
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) =>
|
case Success(fn) =>
|
||||||
val ast.PartialProgram(_, prog) = fn(file)
|
val ast.PartialProgram(_, prog) = fn(file)
|
||||||
given errors: mutable.Builder[Error, List[Error]] = List.newBuilder
|
given errors: mutable.Builder[Error, List[Error]] = List.newBuilder
|
||||||
@@ -118,7 +118,7 @@ def compile(
|
|||||||
_ <- logAction(s"Compilation failed for $filePath\nExit code: $code")
|
_ <- logAction(s"Compilation failed for $filePath\nExit code: $code")
|
||||||
_ <- IO.blocking(
|
_ <- IO.blocking(
|
||||||
// Explicit println since we want this to always show without logger thread info e.t.c.
|
// 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
|
} yield code
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package wacc
|
|||||||
|
|
||||||
import wacc.ast.Position
|
import wacc.ast.Position
|
||||||
import wacc.types._
|
import wacc.types._
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
private val SYNTAX_ERROR = 100
|
private val SYNTAX_ERROR = 100
|
||||||
private val SEMANTIC_ERROR = 200
|
private val SEMANTIC_ERROR = 200
|
||||||
@@ -18,12 +19,12 @@ enum Error {
|
|||||||
case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String)
|
case TypeMismatch(pos: Position, expected: SemType, got: SemType, msg: String)
|
||||||
case InternalError(pos: Position, msg: String)
|
case InternalError(pos: Position, msg: String)
|
||||||
|
|
||||||
case SyntaxError(msg: String)
|
case SyntaxError(file: File, msg: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
extension (e: Error) {
|
extension (e: Error) {
|
||||||
def exitCode: Int = e match {
|
def exitCode: Int = e match {
|
||||||
case Error.SyntaxError(_) => SYNTAX_ERROR
|
case Error.SyntaxError(_, _) => SYNTAX_ERROR
|
||||||
case _ => SEMANTIC_ERROR
|
case _ => SEMANTIC_ERROR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,12 +39,22 @@ extension (e: Error) {
|
|||||||
def formatError(error: Error)(using errorContent: String): String = {
|
def formatError(error: Error)(using errorContent: String): String = {
|
||||||
val sb = new StringBuilder()
|
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
|
/** Function to format the position of an error
|
||||||
*
|
*
|
||||||
* @param pos
|
* @param pos
|
||||||
* Position of the error
|
* Position of the error
|
||||||
*/
|
*/
|
||||||
def formatPosition(pos: Position): Unit = {
|
def formatPosition(pos: Position): Unit = {
|
||||||
|
formatFile(pos.file)
|
||||||
sb.append(s"(line ${pos.line}, column ${pos.column}):\n")
|
sb.append(s"(line ${pos.line}, column ${pos.column}):\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +78,7 @@ def formatError(error: Error)(using errorContent: String): String = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
error match {
|
error match {
|
||||||
case Error.SyntaxError(_) =>
|
case Error.SyntaxError(_, _) =>
|
||||||
sb.append("Syntax error:\n")
|
sb.append("Syntax error:\n")
|
||||||
case _ =>
|
case _ =>
|
||||||
sb.append("Semantic error:\n")
|
sb.append("Semantic error:\n")
|
||||||
@@ -105,11 +116,11 @@ def formatError(error: Error)(using errorContent: String): String = {
|
|||||||
formatPosition(pos)
|
formatPosition(pos)
|
||||||
sb.append(s"Internal error: $msg")
|
sb.append(s"Internal error: $msg")
|
||||||
formatHighlight(pos, 1)
|
formatHighlight(pos, 1)
|
||||||
case Error.SyntaxError(msg) =>
|
case Error.SyntaxError(file, msg) =>
|
||||||
|
formatFile(file)
|
||||||
sb.append(msg)
|
sb.append(msg)
|
||||||
sb.append("\n")
|
sb.append("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.toString()
|
sb.toString()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user