From 1d7ee69ab745434c6a4af445e9570c614aa4bea5 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 31 Aug 2023 20:09:29 +0000 Subject: [PATCH 1/2] lexer: refactor Error.Error()'s prefix generator Signed-off-by: Alejandro Mery --- lexer/error.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lexer/error.go b/lexer/error.go index 541c7f8..065fc01 100644 --- a/lexer/error.go +++ b/lexer/error.go @@ -28,14 +28,25 @@ type Error struct { Err error } +func (err Error) prefix() string { + switch { + case err.Line > 0 || err.Column > 0: + if err.Filename != "" { + return fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column) + } + + return fmt.Sprintf("%v:%v", err.Line, err.Column) + default: + return err.Filename + } +} + func (err Error) Error() string { var s []string - switch { - case err.Line > 0 || err.Column > 0: - s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column)) - case err.Filename != "": - s = append(s, err.Filename) + prefix := err.prefix() + if prefix != "" { + s = append(s, prefix) } if err.Err != nil { From ce75299e74d30a1bdc06af392f3d92989e0119de Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 31 Aug 2023 16:36:33 +0000 Subject: [PATCH 2/2] lexer: add Hint to Error, which is expanded as "%s" instead of "%q" Signed-off-by: Alejandro Mery --- lexer/error.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lexer/error.go b/lexer/error.go index 065fc01..d18f3f2 100644 --- a/lexer/error.go +++ b/lexer/error.go @@ -25,6 +25,7 @@ type Error struct { Column int Content string + Hint string Err error } @@ -57,6 +58,10 @@ func (err Error) Error() string { s = append(s, fmt.Sprintf("%q", err.Content)) } + if err.Hint != "" { + s = append(s, err.Hint) + } + return strings.Join(s, ": ") }