]> Cypherpunks repositories - gostls13.git/commitdiff
text/scanner: better error message if no error handler is installed
authorRobert Griesemer <gri@golang.org>
Tue, 24 May 2016 19:54:14 +0000 (12:54 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 25 May 2016 15:44:28 +0000 (15:44 +0000)
This is reverting golang.org/cl/19622 and introducing "<input>"
as filename if no filename is specified.

Fixes #15813.

Change-Id: Iafc74b789fa33f48ee639c42d4aebc6f06435f95
Reviewed-on: https://go-review.googlesource.com/23402
Reviewed-by: Russ Cox <rsc@golang.org>
src/text/scanner/example_test.go
src/text/scanner/scanner.go
src/text/scanner/scanner_test.go

index 101145948fac436bdc002368e03176d59fd62be8..1d5d34a01526277502af3b34cb37ad6732519264 100644 (file)
@@ -17,6 +17,7 @@ func Example() {
                someParsable = text
        }`
        var s scanner.Scanner
+       s.Filename = "example"
        s.Init(strings.NewReader(src))
        var tok rune
        for tok != scanner.EOF {
@@ -25,14 +26,14 @@ func Example() {
        }
 
        // Output:
-       // At position 3:4 : if
-       // At position 3:6 : a
-       // At position 3:8 : >
-       // At position 3:11 : 10
-       // At position 3:13 : {
-       // At position 4:15 : someParsable
-       // At position 4:17 : =
-       // At position 4:22 : text
-       // At position 5:3 : }
-       // At position 5:3 :
+       // At position example:3:4 : if
+       // At position example:3:6 : a
+       // At position example:3:8 : >
+       // At position example:3:11 : 10
+       // At position example:3:13 : {
+       // At position example:4:15 : someParsable
+       // At position example:4:17 : =
+       // At position example:4:22 : text
+       // At position example:5:3 : }
+       // At position example:5:3 :
 }
index a3da1fdabf445570a20f0006419daea83b45502e..e085f8a7d956bb2ee432254c4a8790c729d45bea 100644 (file)
@@ -37,14 +37,11 @@ func (pos *Position) IsValid() bool { return pos.Line > 0 }
 
 func (pos Position) String() string {
        s := pos.Filename
-       if pos.IsValid() {
-               if s != "" {
-                       s += ":"
-               }
-               s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
-       }
        if s == "" {
-               s = "???"
+               s = "<input>"
+       }
+       if pos.IsValid() {
+               s += fmt.Sprintf(":%d:%d", pos.Line, pos.Column)
        }
        return s
 }
@@ -333,7 +330,7 @@ func (s *Scanner) error(msg string) {
        if !pos.IsValid() {
                pos = s.Pos()
        }
-       fmt.Fprintf(os.Stderr, "text/scanner: %s: %s\n", pos, msg)
+       fmt.Fprintf(os.Stderr, "%s: %s\n", pos, msg)
 }
 
 func (s *Scanner) isIdentRune(ch rune, i int) bool {
index 798bed7e92aa0aa0c17e53e81022d64e79f0a03d..3e92d659ca029e3d56c0236a589e8d82b52c2507 100644 (file)
@@ -451,37 +451,37 @@ func testError(t *testing.T, src, pos, msg string, tok rune) {
 }
 
 func TestError(t *testing.T) {
-       testError(t, "\x00", "1:1", "illegal character NUL", 0)
-       testError(t, "\x80", "1:1", "illegal UTF-8 encoding", utf8.RuneError)
-       testError(t, "\xff", "1:1", "illegal UTF-8 encoding", utf8.RuneError)
-
-       testError(t, "a\x00", "1:2", "illegal character NUL", Ident)
-       testError(t, "ab\x80", "1:3", "illegal UTF-8 encoding", Ident)
-       testError(t, "abc\xff", "1:4", "illegal UTF-8 encoding", Ident)
-
-       testError(t, `"a`+"\x00", "1:3", "illegal character NUL", String)
-       testError(t, `"ab`+"\x80", "1:4", "illegal UTF-8 encoding", String)
-       testError(t, `"abc`+"\xff", "1:5", "illegal UTF-8 encoding", String)
-
-       testError(t, "`a"+"\x00", "1:3", "illegal character NUL", String)
-       testError(t, "`ab"+"\x80", "1:4", "illegal UTF-8 encoding", String)
-       testError(t, "`abc"+"\xff", "1:5", "illegal UTF-8 encoding", String)
-
-       testError(t, `'\"'`, "1:3", "illegal char escape", Char)
-       testError(t, `"\'"`, "1:3", "illegal char escape", String)
-
-       testError(t, `01238`, "1:6", "illegal octal number", Int)
-       testError(t, `01238123`, "1:9", "illegal octal number", Int)
-       testError(t, `0x`, "1:3", "illegal hexadecimal number", Int)
-       testError(t, `0xg`, "1:3", "illegal hexadecimal number", Int)
-       testError(t, `'aa'`, "1:4", "illegal char literal", Char)
-
-       testError(t, `'`, "1:2", "literal not terminated", Char)
-       testError(t, `'`+"\n", "1:2", "literal not terminated", Char)
-       testError(t, `"abc`, "1:5", "literal not terminated", String)
-       testError(t, `"abc`+"\n", "1:5", "literal not terminated", String)
-       testError(t, "`abc\n", "2:1", "literal not terminated", String)
-       testError(t, `/*/`, "1:4", "comment not terminated", EOF)
+       testError(t, "\x00", "<input>:1:1", "illegal character NUL", 0)
+       testError(t, "\x80", "<input>:1:1", "illegal UTF-8 encoding", utf8.RuneError)
+       testError(t, "\xff", "<input>:1:1", "illegal UTF-8 encoding", utf8.RuneError)
+
+       testError(t, "a\x00", "<input>:1:2", "illegal character NUL", Ident)
+       testError(t, "ab\x80", "<input>:1:3", "illegal UTF-8 encoding", Ident)
+       testError(t, "abc\xff", "<input>:1:4", "illegal UTF-8 encoding", Ident)
+
+       testError(t, `"a`+"\x00", "<input>:1:3", "illegal character NUL", String)
+       testError(t, `"ab`+"\x80", "<input>:1:4", "illegal UTF-8 encoding", String)
+       testError(t, `"abc`+"\xff", "<input>:1:5", "illegal UTF-8 encoding", String)
+
+       testError(t, "`a"+"\x00", "<input>:1:3", "illegal character NUL", String)
+       testError(t, "`ab"+"\x80", "<input>:1:4", "illegal UTF-8 encoding", String)
+       testError(t, "`abc"+"\xff", "<input>:1:5", "illegal UTF-8 encoding", String)
+
+       testError(t, `'\"'`, "<input>:1:3", "illegal char escape", Char)
+       testError(t, `"\'"`, "<input>:1:3", "illegal char escape", String)
+
+       testError(t, `01238`, "<input>:1:6", "illegal octal number", Int)
+       testError(t, `01238123`, "<input>:1:9", "illegal octal number", Int)
+       testError(t, `0x`, "<input>:1:3", "illegal hexadecimal number", Int)
+       testError(t, `0xg`, "<input>:1:3", "illegal hexadecimal number", Int)
+       testError(t, `'aa'`, "<input>:1:4", "illegal char literal", Char)
+
+       testError(t, `'`, "<input>:1:2", "literal not terminated", Char)
+       testError(t, `'`+"\n", "<input>:1:2", "literal not terminated", Char)
+       testError(t, `"abc`, "<input>:1:5", "literal not terminated", String)
+       testError(t, `"abc`+"\n", "<input>:1:5", "literal not terminated", String)
+       testError(t, "`abc\n", "<input>:2:1", "literal not terminated", String)
+       testError(t, `/*/`, "<input>:1:4", "comment not terminated", EOF)
 }
 
 // An errReader returns (0, err) where err is not io.EOF.