Skip to content

Commit

Permalink
simplify error
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetatz committed Sep 9, 2023
1 parent e6627e5 commit 6f5c80c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 169 deletions.
6 changes: 3 additions & 3 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func TestOutput(t *testing.T) {
`),
out: d(`
1
$$filename:5:7 identifier a is undefined
$$filename:5:7 a is undefined
`),
},
"scope2": {
Expand All @@ -353,7 +353,7 @@ func TestOutput(t *testing.T) {
2
2
3
$$filename:5:7 identifier i is undefined
$$filename:5:7 i is undefined
`),
},
"scope3": {
Expand Down Expand Up @@ -393,7 +393,7 @@ func TestOutput(t *testing.T) {
}
`),
out: d(`
$$filename:2:8 identifier a is undefined
$$filename:2:8 a is undefined
`),
},
"scope5": {
Expand Down
112 changes: 18 additions & 94 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ type shibaErr interface {
loc() *loc
}

func newsberr(n node, format string, args ...any) shibaErr {
return &sberr{l: n.token().loc, msg: fmt.Sprintf(format, args...)}
}

// sberr is a primarily used error object.
type sberr struct {
l *loc
msg string
Expand All @@ -19,103 +16,40 @@ type sberr struct {
func (e *sberr) loc() *loc { return e.l }
func (e *sberr) Error() string { return e.msg }

type errSimple struct {
l *loc
msg string
}

func (e *errSimple) loc() *loc { return e.l }
func (e *errSimple) Error() string { return e.msg }

/*
* Parse error
* helpers to create simple sberr
*/

type errTokenize struct {
l *loc
msg string
}

func (e *errTokenize) loc() *loc { return e.l }
func (e *errTokenize) Error() string { return e.msg }

type errParse struct {
l *loc
msg string
}

func (e *errParse) loc() *loc { return e.l }
func (e *errParse) Error() string { return e.msg }

/*
* Evaluation error
*/

type errTypeMismatch struct {
l *loc
expected string
actual string
func newsberr(n node, format string, args ...any) shibaErr {
return &sberr{l: n.token().loc, msg: fmt.Sprintf(format, args...)}
}

func (e *errTypeMismatch) loc() *loc { return e.l }
func (e *errTypeMismatch) Error() string {
return fmt.Sprintf("type %s is expected but got %s", e.expected, e.actual)
func newsberr2(l *loc, format string, args ...any) shibaErr {
return &sberr{l: l, msg: fmt.Sprintf(format, args...)}
}

type errInvalidIndex struct {
l *loc
idx int
length int
func newTypeMismatchErr(n node, expected, actual objtyp) shibaErr {
return &sberr{
l: n.token().loc,
msg: fmt.Sprintf("type %s is expected but got %s", expected, actual),
}
}

func (e *errInvalidIndex) loc() *loc { return e.l }
func (e *errInvalidIndex) Error() string {
return fmt.Sprintf("index out of range [%d] with length %d", e.idx, e.length)
func newinterr(n node, format string, args ...any) shibaErr {
return &sberr{l: n.token().loc, msg: "[internal]" + fmt.Sprintf(format, args...)}
}

/*
* Define some errors which must be handled.
*/
type errUndefinedIdent struct {
l *loc
ident string
}

func (e *errUndefinedIdent) loc() *loc { return e.l }
func (e *errUndefinedIdent) Error() string {
return fmt.Sprintf("identifier %s is undefined", e.ident)
}

type errInvalidAssignOp struct {
l *loc
op string
left string
right string
}

func (e *errInvalidAssignOp) loc() *loc { return e.l }
func (e *errInvalidAssignOp) Error() string {
return fmt.Sprintf("invalid assignment %s [%s] %s", e.left, e.op, e.right)
}

type errInvalidBinaryOp struct {
l *loc
op string
left string
right string
}

func (e *errInvalidBinaryOp) loc() *loc { return e.l }
func (e *errInvalidBinaryOp) Error() string {
return fmt.Sprintf("invalid operation %s [%s] %s", e.left, e.op, e.right)
}

type errInvalidUnaryOp struct {
l *loc
op string
target string
}

func (e *errInvalidUnaryOp) loc() *loc { return e.l }
func (e *errInvalidUnaryOp) Error() string {
return fmt.Sprintf("invalid operation [%s]%s", e.op, e.target)
return fmt.Sprintf("%s is undefined", e.ident)
}

type errDictKeyNotFound struct {
Expand All @@ -125,15 +59,5 @@ type errDictKeyNotFound struct {

func (e *errDictKeyNotFound) loc() *loc { return e.l }
func (e *errDictKeyNotFound) Error() string {
return fmt.Sprintf("key %s is not found in dict", e.key)
}

type errInternal struct {
l *loc
msg string
}

func (e *errInternal) loc() *loc { return e.l }
func (e *errInternal) Error() string {
return fmt.Sprintf("shiba internal error: %s", e.msg)
return fmt.Sprintf("key %s is not found", e.key)
}
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *parser) parsestmt() (n node, err shibaErr) {
// Returning error will make the parser code not easy to read.
defer func() {
if r := recover(); r != nil {
err = &errParse{msg: fmt.Sprintf("%v", r), l: p.cur.loc}
err = newsberr2(p.cur.loc, "%v", r)
}
}()

Expand Down
Loading

0 comments on commit 6f5c80c

Please sign in to comment.