Add pkg to ast Expr nodes

This commit is contained in:
Nigel Tao committed 2017-12-29 13:28:31 +11:00
1 parent 8b4ba30a36
commit 139bcfa7f1
5 files changed
+25 -7

No files matched your search

+1 -1
View File
@@ -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]
+6 -4
View File
@@ -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: <nil|Expr>
// - MHS: <nil|Expr>
@@ -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 }
+16 -1
View File
@@ -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)...)
+1 -1
View File
@@ -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))
+1
View File
@@ -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())