diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 2c0a56f1..33016875 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -440,7 +440,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare return nil case t.KeyError, t.KeyStatus, t.KeySuspension: - status := g.statusMap[t.QID{0, n.Ident()}] + status := g.statusMap[n.StatusQID()] if status.name == "" { msg, _ := t.Unescape(n.Ident().Str(g.tm)) z := builtin.StatusMap[msg] diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 943a254c..711882e6 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -140,7 +140,7 @@ type Node struct { // Assert keyword reason . Assert // Assign operator . . Assign // Const . pkg name Const - // Expr operator . literal/ident Expr + // Expr operator pkg literal/ident Expr // Field . . name Field // File . . . File // Func funcName receiverPkg receiverName Func @@ -238,7 +238,7 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l } func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error { return n.Node().Walk(func(o *Node) error { switch o.Kind() { - case KConst, KFunc, KStatus, KStruct, KTypeExpr: + case KConst, KExpr, KFunc, KStatus, KStruct, KTypeExpr: if o.id1 != 0 { return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`, pkg.Str(tm), o.id1.Str(tm)) @@ -268,6 +268,7 @@ const MaxExprDepth = 255 // - FlagsCallImpure is "f(x)" vs "f!(x)" // - FlagsCallSuspendible is "f(x)" vs "f?(x)", it implies FlagsCallImpure // - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot> +// - ID1: <0|pkg> (for statuses) // - ID2: <0|literal|ident> // - LHS: // - MHS: @@ -297,8 +298,8 @@ const MaxExprDepth = 255 // // For lists, like "$(0, 1, 2)", ID0 is IDDollar. // -// For statuses, like `error "foo"` and `"suspension "bar"`, ID0 is the keyword -// and ID1 is the message. +// For statuses, like `error "foo"` and `suspension bar."baz"`, ID0 is the +// keyword, ID1 is the package and ID2 is the message. type Expr Node func (n *Expr) Node() *Node { return (*Node)(n) } @@ -313,6 +314,7 @@ func (n *Expr) BoundsCheckOptimized() bool { return n.flags&FlagsBoundsCheckOpti func (n *Expr) ConstValue() *big.Int { return n.constValue } func (n *Expr) MType() *TypeExpr { return n.mType } func (n *Expr) Operator() t.ID { return n.id0 } +func (n *Expr) StatusQID() t.QID { return t.QID{n.id1, n.id2} } func (n *Expr) Ident() t.ID { return n.id2 } func (n *Expr) LHS() *Node { return n.lhs } func (n *Expr) MHS() *Node { return n.mhs } diff --git a/lang/ast/string.go b/lang/ast/string.go index b54c7388..44c3daa3 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -23,7 +23,7 @@ func (n *Expr) Str(tm *t.Map) string { if n == nil { return "" } - if n.id0 == 0 { + if n.id0 == 0 && n.id1 == 0 { return tm.ByID(n.id2) } return string(n.appendStr(nil, tm, false, 0)) @@ -39,6 +39,21 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32) switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { case 0: switch n.id0.Key() { + case t.KeyError, t.KeyStatus, t.KeySuspension: + switch n.id0.Key() { + case t.KeyError: + buf = append(buf, "error "...) + case t.KeyStatus: + buf = append(buf, "status "...) + case t.KeySuspension: + buf = append(buf, "suspension "...) + } + if n.id1 != 0 { + buf = append(buf, tm.ByID(n.id1)...) + buf = append(buf, '.') + } + fallthrough + case 0: buf = append(buf, tm.ByID(n.id2)...) diff --git a/lang/check/type.go b/lang/check/type.go index 20506460..638f7fbe 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -523,7 +523,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyError, t.KeyStatus, t.KeySuspension: nominalKeyword := n.Operator() declaredKeyword := t.ID(0) - if s, ok := q.c.statuses[t.QID{0, n.Ident()}]; ok { + if s, ok := q.c.statuses[n.StatusQID()]; ok { declaredKeyword = s.Status.Keyword() } else { msg, _ := t.Unescape(n.Ident().Str(q.tm)) diff --git a/lang/parse/parse.go b/lang/parse/parse.go index b448c632..38caf263 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -932,6 +932,7 @@ func (p *parser) parseOperand() (*a.Expr, error) { keyword := x p.src = p.src[1:] message := p.peek1() + // TODO: parse the "pkg" in `error pkg."foo"`. 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())