]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: simplify parser.compound_stmt
authorMatthew Dempsky <mdempsky@google.com>
Thu, 10 Mar 2016 22:18:37 +0000 (14:18 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 10 Mar 2016 22:51:47 +0000 (22:51 +0000)
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>
src/cmd/compile/internal/gc/parser.go

index 22566efa7dffe00db7b37c60b8d7d881bc8460dc..d61597e194461d0cbc1d3ee632474cd38839b48b 100644 (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('}')
+       popdcl()
 
-       var stmt *Node
        if len(l) == 0 {
-               stmt = Nod(OEMPTY, nil, nil)
-       } else {
-               stmt = liststmt(l)
+               return Nod(OEMPTY, nil, nil)
        }
-       popdcl()
-
-       return stmt
+       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())