1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:34:48 -07:00

go/parser: expressions may have comments

Thanks to 0xE2.0x9A.0x9B for the patch suggestion.

Fixes #2739.

R=r
CC=golang-dev
https://golang.org/cl/5536071
This commit is contained in:
Robert Griesemer 2012-01-19 13:54:31 -08:00
parent c837e612bd
commit 7b07310a69

View File

@ -135,8 +135,10 @@ func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, m
// //
func ParseExpr(x string) (ast.Expr, error) { func ParseExpr(x string) (ast.Expr, error) {
// parse x within the context of a complete package for correct scopes; // parse x within the context of a complete package for correct scopes;
// use //line directive for correct positions in error messages // use //line directive for correct positions in error messages and put
file, err := ParseFile(token.NewFileSet(), "", "package p;func _(){_=\n//line :1\n"+x+";}", 0) // x alone on a separate line (handles line comments), followed by a ';'
// to force an error if the expression is incomplete
file, err := ParseFile(token.NewFileSet(), "", "package p;func _(){_=\n//line :1\n"+x+"\n;}", 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }