]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: don't remove syntactically relevant blank in f(42 ...)
authorRobert Griesemer <gri@golang.org>
Thu, 23 Sep 2010 21:20:13 +0000 (14:20 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 23 Sep 2010 21:20:13 +0000 (14:20 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/2246046

src/pkg/go/printer/nodes.go
src/pkg/go/printer/printer.go
src/pkg/go/printer/testdata/expressions.golden
src/pkg/go/printer/testdata/expressions.input
src/pkg/go/printer/testdata/expressions.raw

index 2451116fdb64f39e77d2d195c03f6051d88e47d2..4650df6448d9ed4266d6b5b00b48eda6aa8343bb 100644 (file)
@@ -848,6 +848,14 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
                p.print(x.Lparen, token.LPAREN)
                p.exprList(x.Lparen, x.Args, depth, commaSep|commaTerm, multiLine, x.Rparen)
                if x.Ellipsis.IsValid() {
+                       if p.lastTok == token.INT {
+                               // w/o a blank, the previous int will become a float
+                               // (this could be solved more generally in the print
+                               // function but it appears that this is the only
+                               // place in the grammar where a token starting with
+                               // a do may legally extend the previous token)
+                               p.print(blank)
+                       }
                        p.print(x.Ellipsis, token.ELLIPSIS)
                }
                p.print(x.Rparen, token.RPAREN)
index 5ee2491958576b9e5a31434be2901dc7353cdc8c..bb87b9f58c531ca4ff50b04ece4bb1b4925a6beb 100644 (file)
@@ -65,10 +65,11 @@ type printer struct {
        errors chan os.Error
 
        // Current state
-       nesting int  // nesting level (0: top-level (package scope), >0: functions/decls.)
-       written int  // number of bytes written
-       indent  int  // current indentation
-       escape  bool // true if in escape sequence
+       nesting int         // nesting level (0: top-level (package scope), >0: functions/decls.)
+       written int         // number of bytes written
+       indent  int         // current indentation
+       escape  bool        // true if in escape sequence
+       lastTok token.Token // the last token printed (token.ILLEGAL if it's whitespace)
 
        // Buffered whitespace
        buffer []whiteSpace
@@ -762,6 +763,7 @@ func (p *printer) print(args ...interface{}) {
                var data []byte
                var tag HTMLTag
                var tok token.Token
+
                switch x := f.(type) {
                case whiteSpace:
                        if x == ignore {
@@ -798,7 +800,7 @@ func (p *printer) print(args ...interface{}) {
                        // bytes since they do not appear in legal UTF-8 sequences)
                        // TODO(gri): do this more efficiently.
                        data = []byte("\xff" + string(data) + "\xff")
-                       tok = token.INT // representing all literal tokens
+                       tok = x.Kind
                case token.Token:
                        if p.Styler != nil {
                                data, tag = p.Styler.Token(x)
@@ -810,10 +812,12 @@ func (p *printer) print(args ...interface{}) {
                        if x.IsValid() {
                                next = x // accurate position of next item
                        }
+                       tok = p.lastTok
                default:
                        fmt.Fprintf(os.Stderr, "print: unsupported argument type %T\n", f)
                        panic("go/printer type")
                }
+               p.lastTok = tok
                p.pos = next
 
                if data != nil {
index d8cd90efed428bdac21b024a6d7801b163fcfeea..c828dbe675f018c9a7103ce4b1c89c39388c3c37 100644 (file)
@@ -173,6 +173,17 @@ func f(x int, args ...int) {
        f(0, args...)
        f(1, args)
        f(2, args[0])
+
+       // make sure syntactically legal code remains syntactically legal
+       f(3, 42 ...)    // a blank must remain between 42 and ...
+       f(4, 42....)
+       f(5, 42....)
+       f(6, 42.0...)
+       f(7, 42.0...)
+       f(8, .42...)
+       f(9, .42...)
+       f(10, 42e0...)
+       f(11, 42e0...)
 }
 
 
index 7dac6fd74abc1a4e6012b5b9dc243264e5cedd17..06e17a666263a3e352cffaeebe2310cde441f945 100644 (file)
@@ -173,6 +173,17 @@ func f(x int, args ...int) {
        f(0, args...)
        f(1, args)
        f(2, args[0])
+
+       // make sure syntactically legal code remains syntactically legal
+       f(3, 42 ...) // a blank must remain between 42 and ...
+       f(4, 42. ...)
+       f(5, 42....)
+       f(6, 42.0 ...)
+       f(7, 42.0...)
+       f(8, .42 ...)
+       f(9, .42...)
+       f(10, 42e0 ...)
+       f(11, 42e0...)
 }
 
 
index 6b3f579def09eed06c134df2e85728fd51c19381..56ec39b96839871664053fa14bf487891387a80c 100644 (file)
@@ -173,6 +173,17 @@ func f(x int, args ...int) {
        f(0, args...)
        f(1, args)
        f(2, args[0])
+
+       // make sure syntactically legal code remains syntactically legal
+       f(3, 42 ...)    // a blank must remain between 42 and ...
+       f(4, 42....)
+       f(5, 42....)
+       f(6, 42.0...)
+       f(7, 42.0...)
+       f(8, .42...)
+       f(9, .42...)
+       f(10, 42e0...)
+       f(11, 42e0...)
 }