var typ *Node
var exprs *NodeList
if p.tok != EOF && p.tok != ';' && p.tok != ')' {
- if p.tok != '=' {
- typ = p.ntype()
- }
+ typ = p.try_ntype()
if p.got('=') {
exprs = p.expr_list()
}
name := typedcl0(p.sym())
+ typ := p.try_ntype()
// handle case where type is missing
- var typ *Node
- if p.tok != ';' {
- typ = p.ntype()
- } else {
+ if typ == nil {
p.syntax_error("in type declaration")
p.advance(';', ')')
}
}
p.want(LDDD)
- switch p.tok {
- case LCOMM, LFUNC, '[', LCHAN, LMAP, LSTRUCT, LINTERFACE, '*', LNAME, '@', '?', '(':
- return Nod(ODDD, p.ntype(), nil)
+ if typ := p.try_ntype(); typ != nil {
+ return Nod(ODDD, typ, nil)
}
Yyerror("final argument in variadic function missing type")
defer p.trace("ntype")()
}
+ if typ := p.try_ntype(); typ != nil {
+ return typ
+ }
+
+ p.syntax_error("")
+ p.advance()
+ return nil
+}
+
+// try_ntype is like ntype but it returns nil if there was no type
+// instead of reporting an error.
+func (p *parser) try_ntype() *Node {
+ if trace && Debug['x'] != 0 {
+ defer p.trace("try_ntype")()
+ }
+
switch p.tok {
case LCOMM:
// recvchantype
return t
default:
- p.syntax_error("")
- p.advance()
return nil
}
}
defer p.trace("chan_elem")()
}
- switch p.tok {
- case LCOMM, LFUNC,
- '[', LCHAN, LMAP, LSTRUCT, LINTERFACE,
- '*',
- LNAME, '@', '?',
- '(':
- return p.ntype()
-
- default:
- p.syntax_error("missing channel element type")
- // assume element type is simply absent - don't advance
- return nil
+ if typ := p.try_ntype(); typ != nil {
+ return typ
}
+
+ p.syntax_error("missing channel element type")
+ // assume element type is simply absent - don't advance
+ return nil
}
// go.y:dotname (partial)
defer p.trace("fnres")()
}
- switch p.tok {
- default:
- return nil
-
- case LCOMM, LFUNC, '[', LCHAN, LMAP, LSTRUCT, LINTERFACE, '*', LNAME, '@', '?':
- result := p.ntype()
- return list1(Nod(ODCLFIELD, nil, result))
-
- case '(':
+ if p.tok == '(' {
result := p.param_list()
return checkarglist(result, 0)
}
+
+ if result := p.try_ntype(); result != nil {
+ return list1(Nod(ODCLFIELD, nil, result))
+ }
+
+ return nil
}
// go.y:xdcl_list