Commit e4571d33 authored by Matthew Dempsky's avatar Matthew Dempsky

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