From a87973c141ad6c2716c700069e084071844ca5b4 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 18 Aug 2022 02:04:49 -0400 Subject: [PATCH] Error widths now work properly --- file/location.go | 5 +++++ infoerr/error.go | 2 +- lexer/lexer_test.go | 2 +- lexer/text.go | 5 +++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/file/location.go b/file/location.go index c05fb7e..6b52f5c 100644 --- a/file/location.go +++ b/file/location.go @@ -34,6 +34,11 @@ func (location Location) Width () (width int) { return location.width } +// SetWidth sets the location's width +func (location *Location) SetWidth (width int) { + location.width = width +} + // Describe generates a description of the location for debug purposes func (location Location) Describe () (description string) { return fmt.Sprint ( diff --git a/infoerr/error.go b/infoerr/error.go index 54bd2cc..0d34a04 100644 --- a/infoerr/error.go +++ b/infoerr/error.go @@ -67,7 +67,7 @@ func (err Error) Error () (formattedMessage string) { } // print an arrow with a tail spanning the width of the mistake - for err.Width() > 1 { + for index < err.Column() + err.Width() - 1 { if line[index] == '\t' { formattedMessage += "--------" } else { diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 08b6942..2defa29 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -242,6 +242,6 @@ func TestTokenizeErr (test *testing.T) { "../tests/lexer/error/unknownEscape.arf", infoerr.ErrorKindError, "unknown escape character g", - 1, 2, 1, + 1, 1, 7, test) } diff --git a/lexer/text.go b/lexer/text.go index 108ad34..030c93d 100644 --- a/lexer/text.go +++ b/lexer/text.go @@ -9,9 +9,9 @@ func (lexer *LexingOperation) tokenizeString (isRuneLiteral bool) (err error) { if err != nil { return } token := lexer.newToken() - got := "" + beginning := lexer.file.Location(1) for { // TODO: add hexadecimal escape codes if lexer.char == '\\' { @@ -40,10 +40,11 @@ func (lexer *LexingOperation) tokenizeString (isRuneLiteral bool) (err error) { err = lexer.nextRune() if err != nil { return } + beginning.SetWidth(len(got)) if isRuneLiteral { if len(got) > 1 { err = infoerr.NewError ( - lexer.file.Location(1), + beginning, "excess data in rune literal", infoerr.ErrorKindError) return