Skip to content

Commit

Permalink
text/scanner: better error message if no error handler is installed
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
griesemer committed May 25, 2016
1 parent 786e51d commit 824e1f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
21 changes: 11 additions & 10 deletions src/text/scanner/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 :
}
13 changes: 5 additions & 8 deletions src/text/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
62 changes: 31 additions & 31 deletions src/text/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 824e1f2

Please sign in to comment.