Tweak how we parse "try foo?(etc)"

This commit is contained in:
Nigel Tao committed 2018-08-04 08:51:15 +10:00
1 parent df19bcdb03
commit 7fc98dee00
2 files changed
+14 -29

No files matched your search

-4
View File
@@ -580,10 +580,6 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
setPlaceholderMBoundsMType(o.AsNode()) setPlaceholderMBoundsMType(o.AsNode())
} }
// TODO: distinguish t.IDOpenParen vs t.IDTry in a more principled way?
//
// TODO: figure out calls with and without "?" should interact with the out
// type.
if n.Operator() == t.IDTry { if n.Operator() == t.IDTry {
n.SetMType(typeExprStatus) n.SetMType(typeExprStatus)
} else { } else {
+14 -25
View File
@@ -902,18 +902,11 @@ func (p *parser) parseVarNode(inIterate bool) (*a.Node, error) {
} else if p.peek1() == t.IDEq { } else if p.peek1() == t.IDEq {
op = t.IDEq op = t.IDEq
p.src = p.src[1:] p.src = p.src[1:]
if p.peek1() == t.IDTry {
value, err = p.parseTryExpr()
if err != nil {
return nil, err
}
} else {
value, err = p.parseExpr() value, err = p.parseExpr()
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
}
return a.NewVar(op, id, typ, value).AsNode(), nil return a.NewVar(op, id, typ, value).AsNode(), nil
} }
@@ -938,24 +931,6 @@ func (p *parser) parsePossibleDollarExpr() (*a.Expr, error) {
return a.NewExpr(0, t.IDDollar, 0, 0, nil, nil, nil, args), nil return a.NewExpr(0, t.IDDollar, 0, 0, nil, nil, nil, args), nil
} }
func (p *parser) parseTryExpr() (*a.Expr, error) {
if x := p.peek1(); x != t.IDTry {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected "try", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
call, err := p.parseExpr()
if err != nil {
return nil, err
}
if call.Operator() != t.IDOpenParen {
return nil, fmt.Errorf(`parse: expected function call after "try", got %q at %s:%d`,
call.Str(p.tm), p.filename, p.line())
}
return a.NewExpr(call.AsNode().AsRaw().Flags(), t.IDTry, 0, call.Ident(),
call.LHS(), call.MHS(), call.RHS(), call.Args()), nil
}
func (p *parser) parseExprNode() (*a.Node, error) { func (p *parser) parseExprNode() (*a.Node, error) {
n, err := p.parseExpr() n, err := p.parseExpr()
if err != nil { if err != nil {
@@ -1032,6 +1007,20 @@ func (p *parser) parseOperand() (*a.Expr, error) {
default: default:
switch x { switch x {
case t.IDTry:
p.src = p.src[1:]
call, err := p.parseOperand()
if err != nil {
return nil, err
}
callFlags := call.AsNode().AsRaw().Flags()
if (call.Operator() != t.IDOpenParen) || (!callFlags.AsEffect().Optional()) {
return nil, fmt.Errorf(`parse: expected ?-function call after "try", got %q at %s:%d`,
call.Str(p.tm), p.filename, p.line())
}
return a.NewExpr(callFlags, x, 0, call.Ident(),
call.LHS(), call.MHS(), call.RHS(), call.Args()), nil
case t.IDOpenParen: case t.IDOpenParen:
p.src = p.src[1:] p.src = p.src[1:]
expr, err := p.parseExpr() expr, err := p.parseExpr()