-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
63 lines (50 loc) · 1.33 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import "fmt"
type shibaErr interface {
error
loc() *loc
}
// sberr is a primarily used error object.
type sberr struct {
l *loc
msg string
}
func (e *sberr) loc() *loc { return e.l }
func (e *sberr) Error() string { return e.msg }
/*
* helpers to create simple sberr
*/
func newsberr(n node, format string, args ...any) shibaErr {
return &sberr{l: n.token().loc, msg: fmt.Sprintf(format, args...)}
}
func newsberr2(l *loc, format string, args ...any) shibaErr {
return &sberr{l: l, msg: fmt.Sprintf(format, args...)}
}
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 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("%s is undefined", e.ident)
}
type errDictKeyNotFound struct {
l *loc
key *obj
}
func (e *errDictKeyNotFound) loc() *loc { return e.l }
func (e *errDictKeyNotFound) Error() string {
return fmt.Sprintf("key %s is not found", e.key)
}