Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages #91

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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