]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: report error if ParseExpr argument contains extra tokens
authorRobert Griesemer <gri@golang.org>
Wed, 26 Feb 2014 17:54:01 +0000 (09:54 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 26 Feb 2014 17:54:01 +0000 (09:54 -0800)
This partly addresses issue 6099 where a gofmt rewrite is behaving
unexpectedly because the provided rewrite term is not a valid expression
but is silently consumed anyway.

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/68920044

src/pkg/go/parser/interface.go
src/pkg/go/parser/parser_test.go

index 0f83ca9314362f3d6cd09e86a0931bc0b7dcbb9b..57da4ddcd9302352c168c005799cb75e4f75a677 100644 (file)
@@ -182,6 +182,13 @@ func ParseExpr(x string) (ast.Expr, error) {
        p.closeScope()
        assert(p.topScope == nil, "unbalanced scopes")
 
+       // If a semicolon was inserted, consume it;
+       // report an error if there's more tokens.
+       if p.tok == token.SEMICOLON {
+               p.next()
+       }
+       p.expect(token.EOF)
+
        if p.errors.Len() > 0 {
                p.errors.Sort()
                return nil, p.errors.Err()
index 0a34b7e505e9b3277b9872ff0d54bdc0097e3187..2797ea518bd0e23c23aaf021444d242286da6865 100644 (file)
@@ -78,7 +78,7 @@ func TestParseExpr(t *testing.T) {
        }
        // sanity check
        if _, ok := x.(*ast.BinaryExpr); !ok {
-               t.Errorf("ParseExpr(%s): got %T, expected *ast.BinaryExpr", src, x)
+               t.Errorf("ParseExpr(%s): got %T, want *ast.BinaryExpr", src, x)
        }
 
        // a valid type expression
@@ -89,17 +89,24 @@ func TestParseExpr(t *testing.T) {
        }
        // sanity check
        if _, ok := x.(*ast.StructType); !ok {
-               t.Errorf("ParseExpr(%s): got %T, expected *ast.StructType", src, x)
+               t.Errorf("ParseExpr(%s): got %T, want *ast.StructType", src, x)
        }
 
        // an invalid expression
        src = "a + *"
        _, err = ParseExpr(src)
        if err == nil {
-               t.Fatalf("ParseExpr(%s): %v", src, err)
+               t.Fatalf("ParseExpr(%s): got no error", src)
+       }
+
+       // a valid expression followed by extra tokens is invalid
+       src = "a[i] := x"
+       _, err = ParseExpr(src)
+       if err == nil {
+               t.Fatalf("ParseExpr(%s): got no error", src)
        }
 
-       // it must not crash
+       // ParseExpr must not crash
        for _, src := range valids {
                ParseExpr(src)
        }