From 4aa6233e4fc303c679ab9f459c9e0a6a8b02addb Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 31 Aug 2023 20:09:29 +0000 Subject: [PATCH] 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 {