From c82a472f9bc1a46d67a8fe3b714279033c222946 Mon Sep 17 00:00:00 2001 From: rokostik Date: Mon, 8 Jan 2024 11:36:20 +0100 Subject: [PATCH] improve error messages --- env/series.go | 16 +++++++++++++--- loader/loader.go | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/env/series.go b/env/series.go index ecea9416..e1a1336d 100755 --- a/env/series.go +++ b/env/series.go @@ -107,10 +107,17 @@ func (ser TSeries) Len() int { func (ser TSeries) Probe(idxs Idxs) string { var bu strings.Builder bu.WriteString("{ ") - for i, v := range ser.S { + st := 0 + if ser.Pos() > 10 { + bu.WriteString("... ") + st = ser.Pos() - 11 + } + for i := st; i < ser.Pos()+9 && i < ser.Len(); i++ { if i == ser.Pos()-1 { - bu.WriteString("<-here-> ") + bu.WriteString("\x1b[1m(here) \x1b[22m") } + + v := ser.S[i] if v != nil { bu.WriteString(v.Probe(idxs) + " ") } else { @@ -118,7 +125,10 @@ func (ser TSeries) Probe(idxs Idxs) string { } } if ser.Len() == ser.Pos()-1 { - bu.WriteString("<-here-> ") + bu.WriteString("\x1b[1m(here)\x1b[22m") + } + if ser.Len() > ser.Pos()+9 { + bu.WriteString("... ") } bu.WriteString("}") return bu.String() diff --git a/loader/loader.go b/loader/loader.go index 32a03f06..4242717e 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -83,8 +83,10 @@ func LoadString(input string, sig bool) (env.Object, *env.Idxs) { input = removeBangLine(input) inp1 := strings.TrimSpace(input) + var noBraces bool if strings.Index("{", inp1) != 0 { input = "{ " + input + " }" + noBraces = true } parser := newParser() @@ -92,7 +94,19 @@ func LoadString(input string, sig bool) (env.Object, *env.Idxs) { if err != nil { fmt.Print("\x1b[35;3m") - fmt.Print(err.Error()) + errStr := err.Error() + if noBraces { + // hacky way to remove the first and last curly braces and + // fix the error position without changing the parser library + errStr = strings.Replace(errStr, "{", "", 1) + if i := strings.LastIndex(errStr, "}"); i >= 0 { + errStr = errStr[:i] + errStr[i+1:] + } + if i := strings.LastIndex(errStr, "-"); i >= 0 { + errStr = errStr[:i] + errStr[i+1:] + } + } + fmt.Print(errStr) fmt.Println("\x1b[0m") empty1 := make([]env.Object, 0)