diff options
author | Robert Griesemer <gri@golang.org> | 2020-03-05 14:14:54 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2020-03-11 19:59:39 +0000 |
commit | f6a0d723859752a2e7c10a470eadd395ba6892a6 (patch) | |
tree | 3e3c8c2412f07ae09a306d2fd37eed607752c775 /src/cmd/compile/internal/syntax/scanner_test.go | |
parent | eafb4d8f8f57f52cbb6792aeff535783525186c5 (diff) | |
download | go-f6a0d723859752a2e7c10a470eadd395ba6892a6.tar.gz go-f6a0d723859752a2e7c10a470eadd395ba6892a6.zip |
cmd/compile/internal/syntax: various cleanups following CL 221603
1) Introduced setLit method to uniformly set the scanner state for
literals instead of directly manipulating the scanner fields.
2) Use a local variable 'ok' to track validity of literals instead
of relying on the side-effect of error reporters setting s.bad.
More code but clearer because it is local and explicit.
3) s/litname/baseName/ and use this function uniformly, also for
escapes. Consequently we now report always "hexadecimal" and
not "hex" (in the case of invalid escapes).
4) Added TestDirectives verifying that we get the correct directive
string (even if that string contains '%').
Verified that lines/s parsing performance is unchanged by comparing
go test -run StdLib -fast -skip "syntax/(scanner|scanner_test)\.go"
before and after (no relevant difference).
Change-Id: I143e4648fdaa31d1c365fb794a1cae4bc1c3f5ba
Reviewed-on: https://go-review.googlesource.com/c/go/+/222258
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/scanner_test.go')
-rw-r--r-- | src/cmd/compile/internal/syntax/scanner_test.go | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/syntax/scanner_test.go b/src/cmd/compile/internal/syntax/scanner_test.go index 78e470c45c..04338629d4 100644 --- a/src/cmd/compile/internal/syntax/scanner_test.go +++ b/src/cmd/compile/internal/syntax/scanner_test.go @@ -613,9 +613,9 @@ func TestScanErrors(t *testing.T) { {`'\`, "rune literal not terminated", 0, 0}, {`'\'`, "rune literal not terminated", 0, 0}, {`'\x`, "rune literal not terminated", 0, 0}, - {`'\x'`, "invalid character '\\'' in hex escape", 0, 3}, + {`'\x'`, "invalid character '\\'' in hexadecimal escape", 0, 3}, {`'\y'`, "unknown escape", 0, 2}, - {`'\x0'`, "invalid character '\\'' in hex escape", 0, 4}, + {`'\x0'`, "invalid character '\\'' in hexadecimal escape", 0, 4}, {`'\00'`, "invalid character '\\'' in octal escape", 0, 4}, {`'\377' /*`, "comment not terminated", 0, 7}, // valid octal escape {`'\378`, "invalid character '8' in octal escape", 0, 4}, @@ -633,9 +633,9 @@ func TestScanErrors(t *testing.T) { {`"\`, "string not terminated", 0, 0}, {`"\"`, "string not terminated", 0, 0}, {`"\x`, "string not terminated", 0, 0}, - {`"\x"`, "invalid character '\"' in hex escape", 0, 3}, + {`"\x"`, "invalid character '\"' in hexadecimal escape", 0, 3}, {`"\y"`, "unknown escape", 0, 2}, - {`"\x0"`, "invalid character '\"' in hex escape", 0, 4}, + {`"\x0"`, "invalid character '\"' in hexadecimal escape", 0, 4}, {`"\00"`, "invalid character '\"' in octal escape", 0, 4}, {`"\377" /*`, "comment not terminated", 0, 7}, // valid octal escape {`"\378"`, "invalid character '8' in octal escape", 0, 4}, @@ -644,8 +644,8 @@ func TestScanErrors(t *testing.T) { {`s := "foo\z"`, "unknown escape", 0, 10}, {`s := "foo\z00\nbar"`, "unknown escape", 0, 10}, {`"\x`, "string not terminated", 0, 0}, - {`"\x"`, "invalid character '\"' in hex escape", 0, 3}, - {`var s string = "\x"`, "invalid character '\"' in hex escape", 0, 18}, + {`"\x"`, "invalid character '\"' in hexadecimal escape", 0, 3}, + {`var s string = "\x"`, "invalid character '\"' in hexadecimal escape", 0, 18}, {`return "\Uffffffff"`, "escape is invalid Unicode code point U+FFFFFFFF", 0, 18}, {"0b.0", "invalid radix point in binary literal", 0, 2}, @@ -687,6 +687,48 @@ func TestScanErrors(t *testing.T) { } } +func TestDirectives(t *testing.T) { + for _, src := range []string{ + "line", + "// line", + "//line", + "//line foo", + "//line foo%bar", + + "go", + "// go:", + "//go:", + "//go :foo", + "//go:foo", + "//go:foo%bar", + } { + got := "" + var s scanner + s.init(strings.NewReader(src), func(_, col uint, msg string) { + if col != colbase { + t.Errorf("%s: got col = %d; want %d", src, col, colbase) + } + if msg == "" { + t.Errorf("%s: handler called with empty msg", src) + } + got = msg + }, directives) + + s.next() + if strings.HasPrefix(src, "//line ") || strings.HasPrefix(src, "//go:") { + // handler should have been called + if got != src { + t.Errorf("got %s; want %s", got, src) + } + } else { + // handler should not have been called + if got != "" { + t.Errorf("got %s for %s", got, src) + } + } + } +} + func TestIssue21938(t *testing.T) { s := "/*" + strings.Repeat(" ", 4089) + "*/ .5" |