diff --git a/cmd/wuffs/gen.go b/cmd/wuffs/gen.go index d4a2cec1..313b3f8f 100644 --- a/cmd/wuffs/gen.go +++ b/cmd/wuffs/gen.go @@ -315,7 +315,8 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error if !n.Public() { continue } - fmt.Fprintf(out, "pub %s %s\n", n.Keyword().Str(&h.tm), n.QID().Str(&h.tm)) + fmt.Fprintf(out, "pub %s (%s) %s\n", + n.Keyword().Str(&h.tm), n.Value().Str(&h.tm), n.QID().Str(&h.tm)) case a.KStruct: n := n.Struct() diff --git a/lang/ast/ast.go b/lang/ast/ast.go index fa360dcb..6a853bab 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -477,9 +477,9 @@ func NewVar(op t.ID, name t.ID, xType *TypeExpr, value *Expr) *Var { // - LHS: type Field Node -func (n *Field) Node() *Node { return (*Node)(n) } -func (n *Field) Name() t.ID { return n.id2 } -func (n *Field) XType() *TypeExpr { return n.lhs.TypeExpr() } +func (n *Field) Node() *Node { return (*Node)(n) } +func (n *Field) Name() t.ID { return n.id2 } +func (n *Field) XType() *TypeExpr { return n.lhs.TypeExpr() } func NewField(name t.ID, xType *TypeExpr) *Field { return &Field{ @@ -837,7 +837,7 @@ func NewFunc(flags Flags, filename string, line uint32, receiverName t.ID, funcN } } -// Status is "error ID2" or "suspension ID2": +// Status is "error (RHS) ID2" or "suspension (RHS) ID2": // - FlagsPublic is "pub" vs "pri" // - ID0: // - ID1: <0|pkg> (set by calling SetPackage) @@ -850,8 +850,9 @@ 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) QID() t.QID { return t.QID{n.id1, n.id2} } +func (n *Status) Value() *Expr { return n.rhs.Expr() } -func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message t.ID) *Status { +func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, value *Expr, message t.ID) *Status { return &Status{ kind: KStatus, flags: flags, @@ -859,6 +860,7 @@ func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message line: line, id0: keyword, id2: message, + rhs: value.Node(), } } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index e00b52b0..5edac35e 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -35,20 +35,21 @@ var numTypeBounds = [...][2]*big.Int{ } var ( - minusOne = big.NewInt(-1) - zero = big.NewInt(+0) - one = big.NewInt(+1) - two = big.NewInt(+2) - three = big.NewInt(+3) - four = big.NewInt(+4) - five = big.NewInt(+5) - six = big.NewInt(+6) - seven = big.NewInt(+7) - eight = big.NewInt(+8) - sixteen = big.NewInt(+16) - thirtyTwo = big.NewInt(+32) - sixtyFour = big.NewInt(+64) - ffff = big.NewInt(0xFFFF) + minusOne = big.NewInt(-1) + zero = big.NewInt(+0) + one = big.NewInt(+1) + two = big.NewInt(+2) + three = big.NewInt(+3) + four = big.NewInt(+4) + five = big.NewInt(+5) + six = big.NewInt(+6) + seven = big.NewInt(+7) + eight = big.NewInt(+8) + sixteen = big.NewInt(+16) + thirtyTwo = big.NewInt(+32) + sixtyFour = big.NewInt(+64) + oneTwentyEight = big.NewInt(+128) + ffff = big.NewInt(0xFFFF) maxIntBits = big.NewInt(t.MaxIntBits) diff --git a/lang/check/check.go b/lang/check/check.go index 9fa81596..84ba6a3c 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -178,6 +178,8 @@ type Checker struct { builtInSliceFuncs map[t.QQID]*a.Func builtInTableFuncs map[t.QQID]*a.Func unsortedStructs []*a.Struct + + statusByValue [256]t.ID } func (c *Checker) PackageID() uint32 { return c.packageID } @@ -296,6 +298,30 @@ func (c *Checker) checkStatus(node *a.Node) error { } } c.statuses[qid] = n + + q := &checker{ + c: c, + tm: c.tm, + } + if err := q.tcheckExpr(n.Value(), 0); err != nil { + return fmt.Errorf("%v in status %s", err, qid.Str(c.tm)) + } + if cv := n.Value().ConstValue(); cv == nil { + return fmt.Errorf("status %s value is not a constant expression", qid.Str(c.tm)) + } else if cv.Cmp(zero) <= 0 || oneTwentyEight.Cmp(cv) <= 0 { + return fmt.Errorf("status %s value %v is out of range [0x01..0x7F]", qid.Str(c.tm), cv) + } else if n.QID()[0] == 0 { + x := uint8(cv.Int64()) + if n.Keyword() == t.IDError { + x = -x + } + if other := c.statusByValue[x]; other != 0 { + return fmt.Errorf("duplicate %s value 0x%02X: %s and %s", + n.Keyword().Str(c.tm), cv, other.Str(c.tm), n.QID()[1].Str(c.tm)) + } + c.statusByValue[x] = n.QID()[1] + } + n.Node().SetMType(typeExprPlaceholder) return nil } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index af3e21ed..a5115ee8 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -229,6 +229,22 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { case t.IDError, t.IDSuspension: keyword := p.src[0].ID p.src = p.src[1:] + + if x := p.peek1(); x != t.IDOpenParen { + got := p.tm.ByID(x) + return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + value, err := p.parseExpr() + if err != nil { + return nil, err + } + if x := p.peek1(); x != t.IDCloseParen { + got := p.tm.ByID(x) + return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + message := p.peek1() if !message.IsStrLiteral(p.tm) { got := p.tm.ByID(message) @@ -240,7 +256,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { 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 + return a.NewStatus(flags, p.filename, line, keyword, value, message).Node(), nil case t.IDStruct: p.src = p.src[1:] diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index 93528860..b0cc50b8 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -14,24 +14,24 @@ packageid "dflt" -pub error "bad Huffman code (over-subscribed)" -pub error "bad Huffman code (under-subscribed)" -pub error "bad Huffman code length count" -pub error "bad Huffman code length repetition" -pub error "bad Huffman code" -pub error "bad Huffman minimum code length" -pub error "bad block" -pub error "bad distance" -pub error "bad distance code count" -pub error "bad literal/length code count" -pub error "inconsistent stored block length" -pub error "missing end-of-block code" -pub error "no Huffman codes" +pub error (0x01) "bad Huffman code (over-subscribed)" +pub error (0x02) "bad Huffman code (under-subscribed)" +pub error (0x03) "bad Huffman code length count" +pub error (0x04) "bad Huffman code length repetition" +pub error (0x05) "bad Huffman code" +pub error (0x06) "bad Huffman minimum code length" +pub error (0x07) "bad block" +pub error (0x08) "bad distance" +pub error (0x09) "bad distance code count" +pub error (0x0A) "bad literal/length code count" +pub error (0x0B) "inconsistent stored block length" +pub error (0x0C) "missing end-of-block code" +pub error (0x0D) "no Huffman codes" -pri error "internal error: inconsistent Huffman decoder state" -pri error "internal error: inconsistent Huffman end_of_block" -pri error "internal error: inconsistent distance" -pri error "internal error: inconsistent n_bits" +pri error (0x40) "internal error: inconsistent Huffman decoder state" +pri error (0x41) "internal error: inconsistent Huffman end_of_block" +pri error (0x42) "internal error: inconsistent distance" +pri error (0x43) "internal error: inconsistent n_bits" // The next two tables were created by script/print-deflate-magic-numbers.go. // diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 133cdef8..90f24590 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -16,15 +16,15 @@ packageid "gif " use "std/lzw" -pub error "bad block" -pub error "bad extension label" -pub error "bad graphic control" -pub error "bad header" -pub error "bad literal width" -pub error "not enough pixel data" -pub error "too much pixel data" +pub error (0x01) "bad block" +pub error (0x02) "bad extension label" +pub error (0x03) "bad graphic control" +pub error (0x04) "bad header" +pub error (0x05) "bad literal width" +pub error (0x06) "not enough pixel data" +pub error (0x07) "too much pixel data" -pri error "internal error: inconsistent ri/wi" +pri error (0x40) "internal error: inconsistent ri/wi" // See the spec appendix E "Interlaced Images" on page 29. The first element // represents either that the frame was non-interlaced, or that all interlace diff --git a/std/gzip/decode_gzip.wuffs b/std/gzip/decode_gzip.wuffs index 91cc694f..9b7b2aa8 100644 --- a/std/gzip/decode_gzip.wuffs +++ b/std/gzip/decode_gzip.wuffs @@ -17,10 +17,10 @@ packageid "gzip" use "std/crc32" use "std/deflate" -pub error "bad checksum" -pub error "bad compression method" -pub error "bad encoding flags" -pub error "bad header" +pub error (0x01) "bad checksum" +pub error (0x02) "bad compression method" +pub error (0x03) "bad encoding flags" +pub error (0x04) "bad header" pub struct decoder?( flate deflate.decoder, diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index ff65322a..15136229 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -14,10 +14,8 @@ packageid "lzw " -// TODO: some mechanism for fixing the error code's value, such as a trailing -// "= 100", to maintain API stability. -pub error "bad code" -pub error "cyclical prefix chain" +pub error (0x01) "bad code" +pub error (0x02) "cyclical prefix chain" pub struct decoder?( literal_width base.u32[..8], diff --git a/std/zlib/decode_zlib.wuffs b/std/zlib/decode_zlib.wuffs index 902aff08..1630c259 100644 --- a/std/zlib/decode_zlib.wuffs +++ b/std/zlib/decode_zlib.wuffs @@ -17,12 +17,12 @@ packageid "zlib" use "std/adler32" use "std/deflate" -pub error "bad checksum" -pub error "bad compression method" -pub error "bad compression window size" -pub error "bad parity check" +pub error (0x01) "bad checksum" +pub error (0x02) "bad compression method" +pub error (0x03) "bad compression window size" +pub error (0x04) "bad parity check" -pub error "TODO: unsupported preset dictionary" +pri error (0x40) "TODO: unsupported preset dictionary" pub struct decoder?( flate deflate.decoder,