1
0
mirror of https://github.com/golang/go synced 2024-11-24 08:20:03 -07:00

cmd/compile: simplify parser.compound_stmt

Eliminate "else_clause" parameter and move error messages about bad if
statements into the if_stmt parsing method.

Passes toolstash -cmp.

Change-Id: Ibc31619bdb2e7e0cf28712b14640f7d9b6124a40
Reviewed-on: https://go-review.googlesource.com/20543
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2016-03-10 14:18:37 -08:00
parent e6ea01685f
commit e4571d33ac

View File

@ -815,33 +815,21 @@ func (p *parser) case_(tswitch *Node) *Node {
// Block = "{" StatementList "}" .
// StatementList = { Statement ";" } .
func (p *parser) compound_stmt(else_clause bool) *Node {
func (p *parser) compound_stmt() *Node {
if trace && Debug['x'] != 0 {
defer p.trace("compound_stmt")()
}
markdcl()
if p.got('{') {
// ok
} else if else_clause {
p.syntax_error("else must be followed by if or statement block")
p.advance(LNAME, '}')
} else {
panic("unreachable")
}
p.want('{')
l := p.stmt_list()
p.want('}')
var stmt *Node
if len(l) == 0 {
stmt = Nod(OEMPTY, nil, nil)
} else {
stmt = liststmt(l)
}
popdcl()
return stmt
if len(l) == 0 {
return Nod(OEMPTY, nil, nil)
}
return liststmt(l)
}
// caseblock parses a superset of switch and select clauses.
@ -1046,15 +1034,19 @@ func (p *parser) if_stmt() *Node {
stmt.Nbody.Set(p.loop_body("if clause"))
if p.got(LELSE) {
if p.tok == LIF {
switch p.tok {
case LIF:
stmt.Rlist.Set1(p.if_stmt())
} else {
cs := p.compound_stmt(true)
case '{':
cs := p.compound_stmt()
if cs.Op == OBLOCK && cs.Ninit.Len() == 0 {
stmt.Rlist.Set(cs.List.Slice())
} else {
stmt.Rlist.Set1(cs)
}
default:
p.syntax_error("else must be followed by if or statement block")
p.advance(LNAME, '}')
}
}
@ -2452,7 +2444,7 @@ func (p *parser) stmt() *Node {
switch p.tok {
case '{':
return p.compound_stmt(false)
return p.compound_stmt()
case LVAR, LCONST, LTYPE:
return liststmt(p.common_dcl())