Commit 656a732a authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: simplify parsing of possibly absent type

Introduce a try_ntype function which doesn't return an error upon
not finding a type. Use it instead of having separate repeated
token checks. Simpler, less code, and more efficient.

Change-Id: I81e482158b71901eb179470269349688636aa0ba
Reviewed-on: https://go-review.googlesource.com/17157Reviewed-by: 's avatarChris Manghane <cmang@golang.org>
parent 8ab6e16c
...@@ -574,9 +574,7 @@ func (p *parser) constdcl() *NodeList { ...@@ -574,9 +574,7 @@ func (p *parser) constdcl() *NodeList {
var typ *Node var typ *Node
var exprs *NodeList var exprs *NodeList
if p.tok != EOF && p.tok != ';' && p.tok != ')' { if p.tok != EOF && p.tok != ';' && p.tok != ')' {
if p.tok != '=' { typ = p.try_ntype()
typ = p.ntype()
}
if p.got('=') { if p.got('=') {
exprs = p.expr_list() exprs = p.expr_list()
} }
...@@ -593,11 +591,9 @@ func (p *parser) typedcl() *NodeList { ...@@ -593,11 +591,9 @@ func (p *parser) typedcl() *NodeList {
name := typedcl0(p.sym()) name := typedcl0(p.sym())
typ := p.try_ntype()
// handle case where type is missing // handle case where type is missing
var typ *Node if typ == nil {
if p.tok != ';' {
typ = p.ntype()
} else {
p.syntax_error("in type declaration") p.syntax_error("in type declaration")
p.advance(';', ')') p.advance(';', ')')
} }
...@@ -1754,9 +1750,8 @@ func (p *parser) dotdotdot() *Node { ...@@ -1754,9 +1750,8 @@ func (p *parser) dotdotdot() *Node {
} }
p.want(LDDD) p.want(LDDD)
switch p.tok { if typ := p.try_ntype(); typ != nil {
case LCOMM, LFUNC, '[', LCHAN, LMAP, LSTRUCT, LINTERFACE, '*', LNAME, '@', '?', '(': return Nod(ODDD, typ, nil)
return Nod(ODDD, p.ntype(), nil)
} }
Yyerror("final argument in variadic function missing type") Yyerror("final argument in variadic function missing type")
...@@ -1769,6 +1764,22 @@ func (p *parser) ntype() *Node { ...@@ -1769,6 +1764,22 @@ func (p *parser) ntype() *Node {
defer p.trace("ntype")() 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 { switch p.tok {
case LCOMM: case LCOMM:
// recvchantype // recvchantype
...@@ -1848,8 +1859,6 @@ func (p *parser) ntype() *Node { ...@@ -1848,8 +1859,6 @@ func (p *parser) ntype() *Node {
return t return t
default: default:
p.syntax_error("")
p.advance()
return nil return nil
} }
} }
...@@ -1859,19 +1868,13 @@ func (p *parser) chan_elem() *Node { ...@@ -1859,19 +1868,13 @@ func (p *parser) chan_elem() *Node {
defer p.trace("chan_elem")() defer p.trace("chan_elem")()
} }
switch p.tok { if typ := p.try_ntype(); typ != nil {
case LCOMM, LFUNC, return typ
'[', 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
} }
p.syntax_error("missing channel element type")
// assume element type is simply absent - don't advance
return nil
} }
// go.y:dotname (partial) // go.y:dotname (partial)
...@@ -2158,18 +2161,16 @@ func (p *parser) fnres() *NodeList { ...@@ -2158,18 +2161,16 @@ func (p *parser) fnres() *NodeList {
defer p.trace("fnres")() defer p.trace("fnres")()
} }
switch p.tok { if p.tok == '(' {
default:
return nil
case LCOMM, LFUNC, '[', LCHAN, LMAP, LSTRUCT, LINTERFACE, '*', LNAME, '@', '?':
result := p.ntype()
return list1(Nod(ODCLFIELD, nil, result))
case '(':
result := p.param_list() result := p.param_list()
return checkarglist(result, 0) return checkarglist(result, 0)
} }
if result := p.try_ntype(); result != nil {
return list1(Nod(ODCLFIELD, nil, result))
}
return nil
} }
// go.y:xdcl_list // go.y:xdcl_list
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment