aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2017-02-16 12:52:01 -0800
committerRobert Griesemer <gri@golang.org>2017-02-16 21:46:43 +0000
commit1693e7b6f2ad1bd2a800161e92b5ac8d3d882663 (patch)
treecd481aa17fedca57de165740e78e439e98af4e17 /src/cmd/compile/internal/syntax
parent990124da2a6ca5a54b38733b51018e2f8758cfae (diff)
downloadgo-1693e7b6f2ad1bd2a800161e92b5ac8d3d882663.tar.xz
cmd/compile/internal/syntax: better errors and recovery for invalid character literals
Fixes #15611. Change-Id: I352b145026466cafef8cf87addafbd30716bda24 Reviewed-on: https://go-review.googlesource.com/37138 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/syntax')
-rw-r--r--src/cmd/compile/internal/syntax/scanner.go79
-rw-r--r--src/cmd/compile/internal/syntax/scanner_test.go9
2 files changed, 52 insertions, 36 deletions
diff --git a/src/cmd/compile/internal/syntax/scanner.go b/src/cmd/compile/internal/syntax/scanner.go
index edd60609a0..c2de9ce686 100644
--- a/src/cmd/compile/internal/syntax/scanner.go
+++ b/src/cmd/compile/internal/syntax/scanner.go
@@ -466,6 +466,53 @@ done:
s.tok = _Literal
}
+func (s *scanner) rune() {
+ s.startLit()
+
+ ok := true // only report errors if we're ok so far
+ n := 0
+ for ; ; n++ {
+ r := s.getr()
+ if r == '\'' {
+ break
+ }
+ if r == '\\' {
+ if !s.escape('\'') {
+ ok = false
+ }
+ continue
+ }
+ if r == '\n' {
+ s.ungetr() // assume newline is not part of literal
+ if ok {
+ s.error("newline in character literal")
+ ok = false
+ }
+ break
+ }
+ if r < 0 {
+ if ok {
+ s.errh(s.line, s.col, "invalid character literal (missing closing ')")
+ ok = false
+ }
+ break
+ }
+ }
+
+ if ok {
+ if n == 0 {
+ s.error("empty character literal or unescaped ' in character literal")
+ } else if n != 1 {
+ s.errh(s.line, s.col, "invalid character literal (more than one character)")
+ }
+ }
+
+ s.nlsemi = true
+ s.lit = string(s.stopLit())
+ s.kind = RuneLit
+ s.tok = _Literal
+}
+
func (s *scanner) stdString() {
s.startLit()
@@ -518,38 +565,6 @@ func (s *scanner) rawString() {
s.tok = _Literal
}
-func (s *scanner) rune() {
- s.startLit()
-
- r := s.getr()
- ok := false
- if r == '\'' {
- s.error("empty character literal or unescaped ' in character literal")
- } else if r == '\n' {
- s.ungetr() // assume newline is not part of literal
- s.error("newline in character literal")
- } else {
- ok = true
- if r == '\\' {
- ok = s.escape('\'')
- }
- }
-
- r = s.getr()
- if r != '\'' {
- // only report error if we're ok so far
- if ok {
- s.error("missing '")
- }
- s.ungetr()
- }
-
- s.nlsemi = true
- s.lit = string(s.stopLit())
- s.kind = RuneLit
- s.tok = _Literal
-}
-
func (s *scanner) skipLine(r rune) {
for r >= 0 {
if r == '\n' {
diff --git a/src/cmd/compile/internal/syntax/scanner_test.go b/src/cmd/compile/internal/syntax/scanner_test.go
index ed240df069..4d04e62875 100644
--- a/src/cmd/compile/internal/syntax/scanner_test.go
+++ b/src/cmd/compile/internal/syntax/scanner_test.go
@@ -292,9 +292,9 @@ func TestScanErrors(t *testing.T) {
{`''`, "empty character literal or unescaped ' in character literal", 1, 1},
{"'\n", "newline in character literal", 1, 1},
- {`'\`, "missing '", 1, 2},
- {`'\'`, "missing '", 1, 3},
- {`'\x`, "missing '", 1, 3},
+ {`'\`, "invalid character literal (missing closing ')", 1, 0},
+ {`'\'`, "invalid character literal (missing closing ')", 1, 0},
+ {`'\x`, "invalid character literal (missing closing ')", 1, 0},
{`'\x'`, "non-hex character in escape sequence: '", 1, 3},
{`'\y'`, "unknown escape sequence", 1, 2},
{`'\x0'`, "non-hex character in escape sequence: '", 1, 4},
@@ -302,7 +302,8 @@ func TestScanErrors(t *testing.T) {
{`'\377' /*`, "comment not terminated", 1, 7}, // valid octal escape
{`'\378`, "non-octal character in escape sequence: 8", 1, 4},
{`'\400'`, "octal escape value > 255: 256", 1, 5},
- {`'xx`, "missing '", 1, 2},
+ {`'xx`, "invalid character literal (missing closing ')", 1, 0},
+ {`'xx'`, "invalid character literal (more than one character)", 1, 0},
{"\"\n", "newline in string", 1, 1},
{`"`, "string not terminated", 1, 0},