mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +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
+38
-5
@@ -31,6 +31,7 @@ const (
|
||||
KIf
|
||||
KJump
|
||||
KReturn
|
||||
KStatus
|
||||
KStruct
|
||||
KTypeExpr
|
||||
KUse
|
||||
@@ -58,6 +59,7 @@ var kindStrings = [...]string{
|
||||
KIf: "KIf",
|
||||
KJump: "KJump",
|
||||
KReturn: "KReturn",
|
||||
KStatus: "KStatus",
|
||||
KStruct: "KStruct",
|
||||
KTypeExpr: "KTypeExpr",
|
||||
KUse: "KUse",
|
||||
@@ -113,6 +115,7 @@ func (n *Node) If() *If { return (*If)(n) }
|
||||
func (n *Node) Jump() *Jump { return (*Jump)(n) }
|
||||
func (n *Node) Raw() *Raw { return (*Raw)(n) }
|
||||
func (n *Node) Return() *Return { return (*Return)(n) }
|
||||
func (n *Node) Status() *Status { return (*Status)(n) }
|
||||
func (n *Node) Struct() *Struct { return (*Struct)(n) }
|
||||
func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) }
|
||||
func (n *Node) Use() *Use { return (*Use)(n) }
|
||||
@@ -394,16 +397,22 @@ func NewIf(condition *Expr, elseIf *If, bodyIfTrue []*Node, bodyIfFalse []*Node)
|
||||
}
|
||||
}
|
||||
|
||||
// Return is "return LHS":
|
||||
// Return is "return LHS", "return error ID1" or "return status ID1":
|
||||
// - ID0: <0|IDError|IDStatus>
|
||||
// - ID1: message
|
||||
// - LHS: <nil|Expr>
|
||||
type Return Node
|
||||
|
||||
func (n *Return) Node() *Node { return (*Node)(n) }
|
||||
func (n *Return) Value() *Expr { return n.lhs.Expr() }
|
||||
func (n *Return) Node() *Node { return (*Node)(n) }
|
||||
func (n *Return) Keyword() t.ID { return n.id0 }
|
||||
func (n *Return) Message() t.ID { return n.id1 }
|
||||
func (n *Return) Value() *Expr { return n.lhs.Expr() }
|
||||
|
||||
func NewReturn(value *Expr) *Return {
|
||||
func NewReturn(keyword t.ID, message t.ID, value *Expr) *Return {
|
||||
return &Return{
|
||||
kind: KReturn,
|
||||
id0: keyword,
|
||||
id1: message,
|
||||
lhs: value.Node(),
|
||||
}
|
||||
}
|
||||
@@ -536,6 +545,30 @@ func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID
|
||||
}
|
||||
}
|
||||
|
||||
// Status is "error ID1" or "status ID1":
|
||||
// - FlagsPublic is "pub" vs "pri"
|
||||
// - ID0: <IDError|IDStatus>
|
||||
// - ID1: message
|
||||
type Status Node
|
||||
|
||||
func (n *Status) Node() *Node { return (*Node)(n) }
|
||||
func (n *Status) Public() bool { return n.flags&FlagsPublic != 0 }
|
||||
func (n *Status) Filename() string { return n.filename }
|
||||
func (n *Status) Line() uint32 { return n.line }
|
||||
func (n *Status) Keyword() t.ID { return n.id0 }
|
||||
func (n *Status) Message() t.ID { return n.id1 }
|
||||
|
||||
func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message t.ID) *Status {
|
||||
return &Status{
|
||||
kind: KStatus,
|
||||
flags: flags,
|
||||
filename: filename,
|
||||
line: line,
|
||||
id0: keyword,
|
||||
id1: message,
|
||||
}
|
||||
}
|
||||
|
||||
// Struct is "struct ID1(List0)":
|
||||
// - FlagsSuspendible is "ID1" vs "ID1?"
|
||||
// - FlagsPublic is "pub" vs "pri"
|
||||
@@ -581,7 +614,7 @@ func NewUse(filename string, line uint32, path t.ID) *Use {
|
||||
}
|
||||
|
||||
// File is a file of source code:
|
||||
// - List0: <Func|Struct|Use> top-level declarations
|
||||
// - List0: <Func|Status|Struct|Use> top-level declarations
|
||||
type File Node
|
||||
|
||||
func (n *File) Node() *Node { return (*Node)(n) }
|
||||
|
||||
+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
|
||||
}
|
||||
|
||||
+31
-3
@@ -148,6 +148,22 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
|
||||
out := a.NewStruct(0, p.filename, line, t.IDOut, outFields)
|
||||
return a.NewFunc(flags, p.filename, line, id0, id1, in, out, asserts, body).Node(), nil
|
||||
|
||||
case t.KeyError, t.KeyStatus:
|
||||
keyword := p.src[0].ID
|
||||
p.src = p.src[1:]
|
||||
message := p.peek1()
|
||||
if !message.IsStrLiteral() {
|
||||
got := p.tm.ByID(message)
|
||||
return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
if x := p.peek1().Key(); x != t.KeySemicolon {
|
||||
got := p.tm.ByKey(x)
|
||||
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
return a.NewStatus(flags, p.filename, line, keyword, message).Node(), nil
|
||||
|
||||
case t.KeyStruct:
|
||||
p.src = p.src[1:]
|
||||
name, err := p.parseIdent()
|
||||
@@ -515,14 +531,26 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
|
||||
case t.KeyReturn:
|
||||
p.src = p.src[1:]
|
||||
value, err := (*a.Expr)(nil), error(nil)
|
||||
if p.peek1().Key() != t.KeySemicolon {
|
||||
keyword, message, value, err := t.ID(0), t.ID(0), (*a.Expr)(nil), error(nil)
|
||||
switch x := p.peek1(); x.Key() {
|
||||
case t.KeySemicolon:
|
||||
// No-op.
|
||||
case t.KeyError, t.KeyStatus:
|
||||
keyword = x
|
||||
p.src = p.src[1:]
|
||||
message = p.peek1()
|
||||
if !message.IsStrLiteral() {
|
||||
got := p.tm.ByID(message)
|
||||
return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
default:
|
||||
value, err = p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return a.NewReturn(value).Node(), nil
|
||||
return a.NewReturn(keyword, message, value).Node(), nil
|
||||
|
||||
case t.KeyVar:
|
||||
p.src = p.src[1:]
|
||||
|
||||
@@ -207,6 +207,8 @@ const (
|
||||
KeyVia = Key(IDVia >> KeyShift)
|
||||
KeyPub = Key(IDPub >> KeyShift)
|
||||
KeyPri = Key(IDPri >> KeyShift)
|
||||
KeyError = Key(IDError >> KeyShift)
|
||||
KeyStatus = Key(IDStatus >> KeyShift)
|
||||
|
||||
KeyFalse = Key(IDFalse >> KeyShift)
|
||||
KeyTrue = Key(IDTrue >> KeyShift)
|
||||
@@ -356,6 +358,8 @@ const (
|
||||
IDVia = ID(0x70<<KeyShift | FlagsOther)
|
||||
IDPub = ID(0x71<<KeyShift | FlagsOther)
|
||||
IDPri = ID(0x72<<KeyShift | FlagsOther)
|
||||
IDError = ID(0x73<<KeyShift | FlagsOther)
|
||||
IDStatus = ID(0x74<<KeyShift | FlagsOther)
|
||||
|
||||
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||
@@ -525,6 +529,8 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
||||
KeyVia: {"via", IDVia},
|
||||
KeyPub: {"pub", IDPub},
|
||||
KeyPri: {"pri", IDPri},
|
||||
KeyError: {"error", IDError},
|
||||
KeyStatus: {"status", IDStatus},
|
||||
|
||||
KeyFalse: {"false", IDFalse},
|
||||
KeyTrue: {"true", IDTrue},
|
||||
|
||||
Reference in new issue
Block a user