From eb2e4ef83c0dc954b99e8196fd9bdfbc16211798 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 31 Aug 2023 16:36:33 +0000 Subject: [PATCH] lexer: add Hint to Error, which is expanded as "%s" instead of "%q" Signed-off-by: Alejandro Mery --- lexer/error.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lexer/error.go b/lexer/error.go index 541c7f8..d854541 100644 --- a/lexer/error.go +++ b/lexer/error.go @@ -25,17 +25,27 @@ type Error struct { Column int Content string + Hint string Err error } func (err Error) Error() string { var s []string + var prefix string switch { case err.Line > 0 || err.Column > 0: - s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column)) + if err.Filename != "" { + prefix = fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column) + } else { + prefix = fmt.Sprintf("%v:%v", err.Line, err.Column) + } case err.Filename != "": - s = append(s, err.Filename) + prefix = err.Filename + } + + if prefix != "" { + s = append(s, prefix) } if err.Err != nil { @@ -46,6 +56,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, ": ") }