Skip to content

Commit

Permalink
improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rokostik committed Jan 8, 2024
1 parent 621ffa8 commit c82a472
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 13 additions & 3 deletions env/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,28 @@ 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 {
bu.WriteString("<<< NIL >>>" + " ")
}
}
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()
Expand Down
16 changes: 15 additions & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,30 @@ 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()
val, err := parser.ParseAndGetValue(input, nil)

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)
Expand Down

0 comments on commit c82a472

Please sign in to comment.