mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile/internal/syntax: better scanner error messages
This is one of several changes that were part of a larger rewrite which I made in early 2019 after switching to the new number literal syntax implementation. The purpose of the rewrite was to simplify reading of source code (Unicode character by character) and speed up the scanner but was never submitted for review due to other priorities. Part 2 of 3: This change contains improvements to the scanner error messages: - Use "rune literal" rather than "character literal" to match the spec nomenclature. - Shorter, more to the point error messages. (For instance, "more than one character in rune literal" rather than "invalid character literal (more than one character)", etc.) Change-Id: I1aaf79003374a68dbb05926437ed305cf2a8ec96 Reviewed-on: https://go-review.googlesource.com/c/go/+/221602 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
17e6252c05
commit
bfb903f252
5 changed files with 42 additions and 42 deletions
|
|
@ -385,7 +385,7 @@ func (s *scanner) isIdentRune(c rune, first bool) bool {
|
|||
s.errorf("identifier cannot begin with digit %#U", c)
|
||||
}
|
||||
case c >= utf8.RuneSelf:
|
||||
s.errorf("invalid identifier character %#U", c)
|
||||
s.errorf("invalid character %#U in identifier", c)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
|
@ -612,13 +612,13 @@ func (s *scanner) rune() {
|
|||
if r == '\n' {
|
||||
s.ungetr() // assume newline is not part of literal
|
||||
if !s.bad {
|
||||
s.errorf("newline in character literal")
|
||||
s.errorf("newline in rune literal")
|
||||
}
|
||||
break
|
||||
}
|
||||
if r < 0 {
|
||||
if !s.bad {
|
||||
s.errorAtf(0, "invalid character literal (missing closing ')")
|
||||
s.errorAtf(0, "rune literal not terminated")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
|
@ -626,9 +626,9 @@ func (s *scanner) rune() {
|
|||
|
||||
if !s.bad {
|
||||
if n == 0 {
|
||||
s.errorf("empty character literal or unescaped ' in character literal")
|
||||
s.errorf("empty rune literal or unescaped '")
|
||||
} else if n != 1 {
|
||||
s.errorAtf(0, "invalid character literal (more than one character)")
|
||||
s.errorAtf(0, "more than one character in rune literal")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -815,7 +815,7 @@ func (s *scanner) escape(quote rune) {
|
|||
if c < 0 {
|
||||
return // complain in caller about EOF
|
||||
}
|
||||
s.errorf("unknown escape sequence")
|
||||
s.errorf("unknown escape")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -836,7 +836,7 @@ func (s *scanner) escape(quote rune) {
|
|||
if base == 8 {
|
||||
kind = "octal"
|
||||
}
|
||||
s.errorf("non-%s character in escape sequence: %c", kind, c)
|
||||
s.errorf("invalid character %q in %s escape", c, kind)
|
||||
s.ungetr()
|
||||
return
|
||||
}
|
||||
|
|
@ -847,11 +847,11 @@ func (s *scanner) escape(quote rune) {
|
|||
s.ungetr()
|
||||
|
||||
if x > max && base == 8 {
|
||||
s.errorf("octal escape value > 255: %d", x)
|
||||
s.errorf("octal escape value %d > 255", x)
|
||||
return
|
||||
}
|
||||
|
||||
if x > max || 0xD800 <= x && x < 0xE000 /* surrogate range */ {
|
||||
s.errorf("escape sequence is invalid Unicode code point %#U", x)
|
||||
s.errorf("escape is invalid Unicode code point %#U", x)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue