go/parser: eats \r in comments (original) (raw)
In the following program go/parser/printer turn a correct program into incorrect one, and so the second parsing fails:
package main
import ( "bytes" "fmt" "go/parser" "go/printer" "go/token" )
func main() { data := []byte("package A /*\r//\n") fset := token.NewFileSet() f, err := parser.ParseFile(fset, "src.go", data, parser.ParseComments|parser.DeclarationErrors|parser.AllErrors) if err != nil { return } buf := new(bytes.Buffer) printer.Fprint(buf, fset, f) fset1 := token.NewFileSet() _, err = parser.ParseFile(fset1, "src.go", buf.Bytes(), parser.ParseComments|parser.DeclarationErrors|parser.AllErrors) if err != nil { fmt.Printf("source0: %q\n", data) fmt.Printf("source1: %q\n", buf.Bytes()) panic(err) } }
source0: "package A\t/**\r/*/\n"
source1: "package A\t/**/*/\n"
panic: src.go:1:15: expected ';', found '*'
go version devel +b0532a9 Mon Jun 8 05:13:15 2015 +0000 linux/amd64