fix: quotes around char and str errors, added errors for assign and arrayIndex

This commit is contained in:
Barf-Vader 2025-02-06 23:52:13 +00:00 committed by Guy C
parent f24f8c87d8
commit b3ecae5dbb
2 changed files with 12 additions and 10 deletions

View File

@ -20,12 +20,12 @@ def printError(error: Error)(using errorContent: String): Unit = {
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} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
) )
highlight(ident) highlight(ident.getPosition, ident.v.length)
case Error.UndefinedIdentifier(ident, identType) => case Error.UndefinedIdentifier(ident, identType) =>
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} at line: ${ident.getPosition.line} column: ${ident.getPosition.column}"
) )
highlight(ident) highlight(ident.getPosition, ident.v.length)
case Error.FunctionParamsMismatch(ident, expected, got) => case Error.FunctionParamsMismatch(ident, expected, got) =>
println(s"Function ${ident.v} expects $expected parameters, got $got") println(s"Function ${ident.v} expects $expected parameters, got $got")
case Error.TypeMismatch(expected, got) => case Error.TypeMismatch(expected, got) =>
@ -34,13 +34,13 @@ def printError(error: Error)(using errorContent: String): Unit = {
} }
def highlight(ident: ast.Ident)(using errorContent: String): Unit = { def highlight(pos: Position, size: Int)(using errorContent: String): Unit = {
val lines = errorContent.split("\n") val lines = errorContent.split("\n")
val preLine = if (ident.getPosition.line > 1) lines(ident.getPosition.line - 2) else "" val preLine = if (pos.line > 1) lines(pos.line - 2) else ""
val midLine = lines(ident.getPosition.line - 1) val midLine = lines(pos.line - 1)
val postLine = lines(ident.getPosition.line) val postLine = lines(pos.line)
val linePointer = " " * (ident.getPosition.column) + ("^" * (ident.v.length)) + "\n" val linePointer = " " * (pos.column) + ("^" * (size)) + "\n"
println( println(
s">$preLine\n>$midLine\n$linePointer>$postLine" s">$preLine\n>$midLine\n$linePointer>$postLine"

View File

@ -31,7 +31,9 @@ val errConfig = new ErrorConfig {
"fst" -> Label("pair extraction"), "fst" -> Label("pair extraction"),
"snd" -> Label("pair extraction"), "snd" -> Label("pair extraction"),
"false" -> Label("boolean literal"), "false" -> Label("boolean literal"),
"true" -> Label("boolean literal") "true" -> Label("boolean literal"),
"=" -> Label("assignment"),
"[" -> Label("array index")
) )
} }
object lexer { object lexer {
@ -83,8 +85,8 @@ object lexer {
val errTokens = Seq( val errTokens = Seq(
lexer.nonlexeme.names.identifier.map(v => s"identifier $v"), lexer.nonlexeme.names.identifier.map(v => s"identifier $v"),
lexer.nonlexeme.integer.decimal32[Int].map(n => s"integer $n"), lexer.nonlexeme.integer.decimal32[Int].map(n => s"integer $n"),
lexer.nonlexeme.character.ascii.map(c => s"character literal $c"), lexer.nonlexeme.character.ascii.map(c => s"character literal \'$c\'"),
lexer.nonlexeme.string.ascii.map(s => s"string literal $s"), lexer.nonlexeme.string.ascii.map(s => s"string literal \"$s\""),
character.whitespace.map(_ => "") character.whitespace.map(_ => "")
) ++ desc.symbolDesc.hardKeywords.map { k => ) ++ desc.symbolDesc.hardKeywords.map { k =>
lexer.nonlexeme.symbol(k).as(s"keyword $k") lexer.nonlexeme.symbol(k).as(s"keyword $k")