Commit 824e1f2e authored by Robert Griesemer's avatar Robert Griesemer

text/scanner: better error message if no error handler is installed

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/23402Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 786e51d7
......@@ -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 :
}
......@@ -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 {
......
......@@ -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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment