]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.inline] cmd/compile/internal/syntax: add tests for //line directives
authorRobert Griesemer <gri@golang.org>
Fri, 9 Dec 2016 22:28:49 +0000 (14:28 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 9 Dec 2016 23:34:30 +0000 (23:34 +0000)
Change-Id: I77dc73bfe79e43bbadf85d7eb3c5f8990ec72023
Reviewed-on: https://go-review.googlesource.com/34248
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/parser_test.go
src/cmd/compile/internal/syntax/scanner_test.go

index a58513478b081d4ec12ff779e3b401623e66e97a..eb9c3e4aa528a2849c28ebf388e32a2ca6acc076 100644 (file)
@@ -47,7 +47,7 @@ func (p *parser) init(filename string, src io.Reader, errh ErrorHandler, pragh P
                },
                func(line, col uint, text string) {
                        if strings.HasPrefix(text, "line ") {
-                               p.updateBase(line, col, text[5:])
+                               p.updateBase(line, col+5, text[5:])
                        }
                        if pragh != nil {
                                p.pragma |= pragh(p.pos_at(line, col), text)
@@ -67,7 +67,7 @@ func (p *parser) updateBase(line, col uint, text string) {
        // Want to use LastIndexByte below but it's not defined in Go1.4 and bootstrap fails.
        i := strings.LastIndex(text, ":") // look from right (Windows filenames may contain ':')
        if i < 0 {
-               return
+               return // ignore (not a line directive)
        }
        nstr := text[i+1:]
        n, err := strconv.Atoi(nstr)
index fb1a761531ef086a72e779fa0eae1480e9d4648f..23fed3b105f21c38276807e77af6db2a7ad45af7 100644 (file)
@@ -182,3 +182,47 @@ func TestParseFile(t *testing.T) {
                t.Errorf("got %v; want first error %v", err, first)
        }
 }
+
+func TestLineDirectives(t *testing.T) {
+       for _, test := range []struct {
+               src, msg  string
+               filename  string
+               line, col uint
+       }{
+               // test validity of //line directive
+               {`//line :`, "invalid line number: ", "", 1, 8},
+               {`//line :x`, "invalid line number: x", "", 1, 8},
+               {`//line foo :`, "invalid line number: ", "", 1, 12},
+               {`//line foo:123abc`, "invalid line number: 123abc", "", 1, 11},
+               {`/**///line foo:x`, "invalid line number: x", "", 1, 15},
+               {`//line foo:0`, "invalid line number: 0", "", 1, 11},
+               {fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), "", 1, 11},
+
+               // test effect of //line directive on (relative) position information
+               {"//line foo:123\n   foo", "syntax error: package statement must be first", "foo", 123, 3},
+               {"//line foo:123\n//line bar:345\nfoo", "syntax error: package statement must be first", "bar", 345, 0},
+       } {
+               _, err := ParseBytes("", []byte(test.src), nil, nil, 0)
+               if err == nil {
+                       t.Errorf("%s: no error reported", test.src)
+                       continue
+               }
+               perr, ok := err.(Error)
+               if !ok {
+                       t.Errorf("%s: got %v; want parser error", test.src, err)
+                       continue
+               }
+               if msg := perr.Msg; msg != test.msg {
+                       t.Errorf("%s: got msg = %q; want %q", test.src, msg, test.msg)
+               }
+               if filename := perr.Pos.RelFilename(); filename != test.filename {
+                       t.Errorf("%s: got filename = %q; want %q", test.src, filename, test.filename)
+               }
+               if line := perr.Pos.RelLine(); line != test.line {
+                       t.Errorf("%s: got line = %d; want %d", test.src, line, test.line)
+               }
+               if col := perr.Pos.Col(); col != test.col {
+                       t.Errorf("%s: got col = %d; want %d", test.src, col, test.col)
+               }
+       }
+}
index c0943e7bce587fa76669afec63e823b7876c9d28..fcdc3348e4ed0bac325d83f91ebf24bf4fb9fd30 100644 (file)
@@ -322,15 +322,6 @@ func TestScanErrors(t *testing.T) {
                {`var s string = "\x"`, "non-hex character in escape sequence: \"", 1, 18},
                {`return "\Uffffffff"`, "escape sequence is invalid Unicode code point", 1, 18},
 
-               // TODO(gri) move these test cases into an appropriate parser test
-               // {`//line :`, "invalid line number: ", 1, 8},
-               // {`//line :x`, "invalid line number: x", 1, 8},
-               // {`//line foo :`, "invalid line number: ", 1, 12},
-               // {`//line foo:123abc`, "invalid line number: 123abc", 1, 11},
-               // {`/**///line foo:x`, "invalid line number: x", 1, 15},
-               // {`//line foo:0`, "invalid line number: 0", 1, 11},
-               // {fmt.Sprintf(`//line foo:%d`, lineMax+1), fmt.Sprintf("invalid line number: %d", lineMax+1), 1, 11},
-
                // former problem cases
                {"package p\n\n\xef", "invalid UTF-8 encoding", 3, 0},
        } {