This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/infoerr/error.go

104 lines
2.0 KiB
Go
Raw Normal View History

2022-08-17 22:56:45 -06:00
package infoerr
2022-08-09 18:12:33 -06:00
import "os"
import "fmt"
2022-08-29 23:11:10 -06:00
import "git.tebibyte.media/arf/arf/file"
type ErrorKind int
const (
ErrorKindError ErrorKind = iota
ErrorKindWarn
)
type Error struct {
2022-08-17 22:51:19 -06:00
file.Location
message string
kind ErrorKind
}
// NewError creates a new error at the specified location.
func NewError (
2022-08-17 22:51:19 -06:00
location file.Location,
message string,
kind ErrorKind,
) (
err Error,
) {
2022-10-12 10:39:00 -06:00
if location.File() == nil {
panic("cannot create new Error in a blank file")
}
return Error {
Location: location,
message: message,
kind: kind,
}
}
// Error returns a formatted error message as a string.
func (err Error) Error () (formattedMessage string) {
switch err.kind {
case ErrorKindError:
2022-08-11 12:15:57 -06:00
formattedMessage += "\033[31mERR\033[0m"
case ErrorKindWarn:
2022-08-11 12:15:57 -06:00
formattedMessage += "\033[33m!!!\033[0m"
}
// print information about the location of the mistake
2022-08-17 22:51:19 -06:00
if err.Width() > 0 {
formattedMessage += fmt.Sprint (
2022-08-17 22:51:19 -06:00
" \033[34m", err.Row() + 1,
":", err.Column() + 1)
}
2022-08-11 12:15:57 -06:00
formattedMessage +=
" \033[90min\033[0m " +
2022-08-17 22:51:19 -06:00
err.File().Path() + "\n"
2022-08-17 22:51:19 -06:00
if err.Width() > 0 {
// print erroneous line
2022-08-17 22:51:19 -06:00
line := err.File().GetLine(err.Row())
formattedMessage += line + "\n"
// position error marker
var index int
2022-08-17 22:51:19 -06:00
for index = 0; index < err.Column(); index ++ {
if line[index] == '\t' {
formattedMessage += "\t"
} else {
formattedMessage += " "
}
}
// print an arrow with a tail spanning the width of the mistake
2022-08-18 00:04:49 -06:00
for index < err.Column() + err.Width() - 1 {
if line[index] == '\t' {
formattedMessage += "--------"
} else {
formattedMessage += "-"
}
index ++
}
formattedMessage += "^\n"
}
formattedMessage += err.message + "\n"
return
}
2022-08-09 18:12:33 -06:00
// Print formats the error and prints it to stderr.
func (err Error) Print () {
os.Stderr.Write([]byte(err.Error()))
}
2022-08-17 22:39:08 -06:00
// Message returns the error's message string
func (err Error) Message () (message string) {
return err.message
}
// Kind returns what kind of error the error is.
func (err Error) Kind () (kind ErrorKind) {
return err.kind
}