aboutsummaryrefslogtreecommitdiff
path: root/src/go/parser/parser.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2019-10-21 17:01:14 -0700
committerRobert Griesemer <gri@golang.org>2019-10-28 20:47:07 +0000
commit3e457030d9a0b9ed23d9d5b346723c54ccae1a8e (patch)
tree2b05e7743a26b313a10ea0872f6bb475dca4afa6 /src/go/parser/parser.go
parent9be36ba7b46c71e9b58c71cfb75a890c3ed4e8a3 (diff)
downloadgo-3e457030d9a0b9ed23d9d5b346723c54ccae1a8e.tar.gz
go-3e457030d9a0b9ed23d9d5b346723c54ccae1a8e.zip
go/parser, go/ast: correctly take into account presence of } in block
Correctly track whether the closing } of a block (or a function body) is present or not in the AST and report correct End() positions in each case. There are more cases like this but this CL addresses an immediate issue and sets a precedent for how to fix similar cases if a need arises. Fixes #33649. Change-Id: Id6662ddaac09f3c15f8003edc9275fe2b0c41c78 Reviewed-on: https://go-review.googlesource.com/c/go/+/202581 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/go/parser/parser.go')
-rw-r--r--src/go/parser/parser.go16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/go/parser/parser.go b/src/go/parser/parser.go
index 3a468d096b..beb563f25f 100644
--- a/src/go/parser/parser.go
+++ b/src/go/parser/parser.go
@@ -397,6 +397,18 @@ func (p *parser) expect(tok token.Token) token.Pos {
return pos
}
+// expect2 is like expect, but it returns an invalid position
+// if the expected token is not found.
+func (p *parser) expect2(tok token.Token) (pos token.Pos) {
+ if p.tok == tok {
+ pos = p.pos
+ } else {
+ p.errorExpected(pos, "'"+tok.String()+"'")
+ }
+ p.next() // make progress
+ return
+}
+
// expectClosing is like expect but provides a better error message
// for the common case of a missing comma before a newline.
//
@@ -1082,7 +1094,7 @@ func (p *parser) parseBody(scope *ast.Scope) *ast.BlockStmt {
list := p.parseStmtList()
p.closeLabelScope()
p.closeScope()
- rbrace := p.expect(token.RBRACE)
+ rbrace := p.expect2(token.RBRACE)
return &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
}
@@ -1096,7 +1108,7 @@ func (p *parser) parseBlockStmt() *ast.BlockStmt {
p.openScope()
list := p.parseStmtList()
p.closeScope()
- rbrace := p.expect(token.RBRACE)
+ rbrace := p.expect2(token.RBRACE)
return &ast.BlockStmt{Lbrace: lbrace, List: list, Rbrace: rbrace}
}