refactor: created printPosition method for unified error message format
This commit is contained in:
parent
a65cc01815
commit
ee1ea6c63b
@ -17,19 +17,31 @@ def printError(error: Error)(using errorContent: String): Unit = {
|
|||||||
println("Semantic error:")
|
println("Semantic error:")
|
||||||
error match {
|
error match {
|
||||||
case Error.DuplicateDeclaration(ident) =>
|
case Error.DuplicateDeclaration(ident) =>
|
||||||
|
printPosition(ident.getPosition)
|
||||||
println(
|
println(
|
||||||
s"Duplicate declaration of identifier ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
|
s"Duplicate declaration of identifier ${ident.v}"
|
||||||
)
|
)
|
||||||
highlight(ident.getPosition, ident.v.length)
|
highlight(ident.getPosition, ident.v.length)
|
||||||
case Error.UndefinedIdentifier(ident, identType) =>
|
case Error.UndefinedIdentifier(ident, identType) =>
|
||||||
|
printPosition(ident.getPosition)
|
||||||
println(
|
println(
|
||||||
s"Undefined ${identType.toString.toLowerCase()} ${ident.v} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
|
s"Undefined ${identType.toString.toLowerCase()} ${ident.v}"
|
||||||
)
|
)
|
||||||
highlight(ident.getPosition, ident.v.length)
|
highlight(ident.getPosition, ident.v.length)
|
||||||
case Error.FunctionParamsMismatch(ident, expected, got) =>
|
case Error.FunctionParamsMismatch(ident, expected, got) =>
|
||||||
|
printPosition(ident.getPosition)
|
||||||
println(s"Function ${ident.v} expects $expected parameters, got $got")
|
println(s"Function ${ident.v} expects $expected parameters, got $got")
|
||||||
|
highlight(ident.getPosition, ident.v.length)
|
||||||
case Error.TypeMismatch(expected, got) =>
|
case Error.TypeMismatch(expected, got) =>
|
||||||
println(s"Type mismatch: expected $expected, got $got")
|
println(s"Type mismatch: expected $expected, got $got")
|
||||||
|
case Error.SemanticError(pos, msg) =>
|
||||||
|
printPosition(pos)
|
||||||
|
println(s"$msg at line: ${pos.line} column: ${pos.column}")
|
||||||
|
highlight(pos, 1)
|
||||||
|
case wacc.Error.InternalError(pos, msg) =>
|
||||||
|
printPosition(pos)
|
||||||
|
println(s"Internal error: $msg")
|
||||||
|
highlight(pos, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -39,20 +51,14 @@ def highlight(pos: Position, size: Int)(using errorContent: String): Unit = {
|
|||||||
|
|
||||||
val preLine = if (pos.line > 1) lines(pos.line - 2) else ""
|
val preLine = if (pos.line > 1) lines(pos.line - 2) else ""
|
||||||
val midLine = lines(pos.line - 1)
|
val midLine = lines(pos.line - 1)
|
||||||
val postLine = lines(pos.line)
|
val postLine = if (pos.line < lines.size) lines(pos.line) else ""
|
||||||
val linePointer = " " * (pos.column) + ("^" * (size)) + "\n"
|
val linePointer = " " * (pos.column + 2) + ("^" * (size)) + "\n"
|
||||||
|
|
||||||
println(
|
println(
|
||||||
s" >$preLine\n >$midLine\n$linePointer >$postLine"
|
s" >$preLine\n >$midLine\n$linePointer >$postLine"
|
||||||
)
|
)
|
||||||
|
}
|
||||||
// var lines: Iterator[String] = errorContent.linesIterator
|
|
||||||
|
def printPosition(pos: Position): Unit = {
|
||||||
// lines = lines.drop(Math.max(0, ident.getPosition.line - 2))
|
println(s"(line ${pos.line}, column ${pos.column}):")
|
||||||
// println(s">${lines.next()}")
|
|
||||||
// if (lines.hasNext)
|
|
||||||
// println(s">${lines.next()}")
|
|
||||||
// println(" " * (ident.getPosition.column) + ("^" * (ident.v.length)))
|
|
||||||
// if(lines.hasNext)
|
|
||||||
// println(s">${lines.next()}")
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user