Put Error in its own module

This commit is contained in:
Sasha Koshka 2022-08-18 00:51:19 -04:00
parent abc6e44fb2
commit ca5f8202bb

View File

@ -1,7 +1,8 @@
package file package error
import "os" import "os"
import "fmt" import "fmt"
import "git.tebibyte.media/sashakoshka/arf/file"
type ErrorKind int type ErrorKind int
@ -11,14 +12,14 @@ const (
) )
type Error struct { type Error struct {
Location file.Location
message string message string
kind ErrorKind kind ErrorKind
} }
// NewError creates a new error at the specified location. // NewError creates a new error at the specified location.
func NewError ( func NewError (
location Location, location file.Location,
message string, message string,
kind ErrorKind, kind ErrorKind,
) ( ) (
@ -41,24 +42,23 @@ func (err Error) Error () (formattedMessage string) {
} }
// print information about the location of the mistake // print information about the location of the mistake
if err.width > 0 { if err.Width() > 0 {
formattedMessage += fmt.Sprint ( formattedMessage += fmt.Sprint (
" \033[34m", err.Location.row + 1, " \033[34m", err.Row() + 1,
":", err.Location.column + 1) ":", err.Column() + 1)
} }
formattedMessage += formattedMessage +=
" \033[90min\033[0m " + " \033[90min\033[0m " +
err.Location.file.path + "\n" err.File().Path() + "\n"
if err.width > 0 { if err.Width() > 0 {
// print erroneous line // print erroneous line
line := err.Location.file.lines[err.Location.row] line := err.File().GetLine(err.Row())
formattedMessage += formattedMessage += line + "\n"
err.Location.file.lines[err.Location.row] + "\n"
// position error marker // position error marker
var index int var index int
for index = 0; index < err.Location.column; index ++ { for index = 0; index < err.Column(); index ++ {
if line[index] == '\t' { if line[index] == '\t' {
formattedMessage += "\t" formattedMessage += "\t"
} else { } else {
@ -67,7 +67,7 @@ func (err Error) Error () (formattedMessage string) {
} }
// print an arrow with a tail spanning the width of the mistake // print an arrow with a tail spanning the width of the mistake
for err.width > 1 { for err.Width() > 1 {
if line[index] == '\t' { if line[index] == '\t' {
formattedMessage += "--------" formattedMessage += "--------"
} else { } else {