From: Robert Griesemer Date: Tue, 24 May 2016 19:54:14 +0000 (-0700) Subject: text/scanner: better error message if no error handler is installed X-Git-Tag: go1.7beta1~100 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=824e1f2e083c9c1df8455554744e49471becbaa2;p=gostls13.git text/scanner: better error message if no error handler is installed This is reverting golang.org/cl/19622 and introducing "" as filename if no filename is specified. Fixes #15813. Change-Id: Iafc74b789fa33f48ee639c42d4aebc6f06435f95 Reviewed-on: https://go-review.googlesource.com/23402 Reviewed-by: Russ Cox --- diff --git a/src/text/scanner/example_test.go b/src/text/scanner/example_test.go index 101145948f..1d5d34a015 100644 --- a/src/text/scanner/example_test.go +++ b/src/text/scanner/example_test.go @@ -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 : } diff --git a/src/text/scanner/scanner.go b/src/text/scanner/scanner.go index a3da1fdabf..e085f8a7d9 100644 --- a/src/text/scanner/scanner.go +++ b/src/text/scanner/scanner.go @@ -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 = "" + } + 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 { diff --git a/src/text/scanner/scanner_test.go b/src/text/scanner/scanner_test.go index 798bed7e92..3e92d659ca 100644 --- a/src/text/scanner/scanner_test.go +++ b/src/text/scanner/scanner_test.go @@ -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", ":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) } // An errReader returns (0, err) where err is not io.EOF.