mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Add user-defined error and status codes.
This commit is contained in:
1 parent
90e49caf51
commit
3863427127
10 files changed
+282
-42
No files matched your search
+46
-9
@@ -48,6 +48,13 @@ func (e *Error) Error() string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func trimQuotes(s string) string {
|
||||
if len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"' {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// TypeExprFoo is an *ast.Node MType (implicit type).
|
||||
var (
|
||||
TypeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||
@@ -60,17 +67,22 @@ func numeric(n *a.TypeExpr) bool { return n == TypeExprIdeal || n.IsNumType() }
|
||||
// TypeMap maps from variable names (as token IDs) to types.
|
||||
type TypeMap map[t.ID]*a.TypeExpr
|
||||
|
||||
type Struct struct {
|
||||
ID t.ID
|
||||
Struct *a.Struct
|
||||
}
|
||||
|
||||
type Func struct {
|
||||
QID t.QID
|
||||
QID t.QID // Qualified ID of the func name.
|
||||
Func *a.Func
|
||||
LocalVars TypeMap
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
ID t.ID // ID of the status message.
|
||||
Status *a.Status
|
||||
}
|
||||
|
||||
type Struct struct {
|
||||
ID t.ID // ID of the struct name.
|
||||
Struct *a.Struct
|
||||
}
|
||||
|
||||
func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) {
|
||||
for _, f := range files {
|
||||
if f == nil {
|
||||
@@ -98,8 +110,9 @@ func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) {
|
||||
flags: flags,
|
||||
tm: tm,
|
||||
reasonMap: rMap,
|
||||
structs: map[t.ID]Struct{},
|
||||
funcs: map[t.QID]Func{},
|
||||
statuses: map[t.ID]Status{},
|
||||
structs: map[t.ID]Struct{},
|
||||
}
|
||||
|
||||
for _, phase := range phases {
|
||||
@@ -123,6 +136,7 @@ var phases = [...]struct {
|
||||
check func(*Checker, *a.Node) error
|
||||
}{
|
||||
{a.KUse, (*Checker).checkUse},
|
||||
{a.KStatus, (*Checker).checkStatus},
|
||||
{a.KStruct, (*Checker).checkStruct},
|
||||
{a.KFunc, (*Checker).checkFuncSignature},
|
||||
{a.KFunc, (*Checker).checkFuncContract},
|
||||
@@ -133,17 +147,40 @@ type Checker struct {
|
||||
flags Flags
|
||||
tm *t.Map
|
||||
reasonMap reasonMap
|
||||
structs map[t.ID]Struct
|
||||
funcs map[t.QID]Func
|
||||
statuses map[t.ID]Status
|
||||
structs map[t.ID]Struct
|
||||
}
|
||||
|
||||
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
|
||||
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
|
||||
func (c *Checker) Statuses() map[t.ID]Status { return c.statuses }
|
||||
func (c *Checker) Structs() map[t.ID]Struct { return c.structs }
|
||||
|
||||
func (c *Checker) checkUse(node *a.Node) error {
|
||||
// TODO.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Checker) checkStatus(node *a.Node) error {
|
||||
n := node.Status()
|
||||
id := n.Message()
|
||||
if other, ok := c.statuses[id]; ok {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("check: duplicate status %q", trimQuotes(id.String(c.tm))),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
OtherFilename: other.Status.Filename(),
|
||||
OtherLine: other.Status.Line(),
|
||||
}
|
||||
}
|
||||
c.statuses[id] = Status{
|
||||
ID: id,
|
||||
Status: n,
|
||||
}
|
||||
n.Node().SetTypeChecked()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool) error {
|
||||
if len(fields) == 0 {
|
||||
return nil
|
||||
|
||||
+9
-1
@@ -106,7 +106,15 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
|
||||
case a.KReturn:
|
||||
n := n.Return()
|
||||
if value := n.Value(); value != nil {
|
||||
if nk := n.Keyword(); nk != 0 {
|
||||
if s, ok := q.c.statuses[n.Message()]; !ok {
|
||||
return fmt.Errorf("check: no error or status with message %q",
|
||||
trimQuotes(n.Message().String(q.tm)))
|
||||
} else if sk := s.Status.Keyword(); nk != sk {
|
||||
return fmt.Errorf("check: return statement says %q but declaration says %q",
|
||||
nk.String(q.tm), sk.String(q.tm))
|
||||
}
|
||||
} else if value := n.Value(); value != nil {
|
||||
if err := q.tcheckExpr(value, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user