aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cover
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2019-03-23 16:20:35 +0000
committerRobert Griesemer <gri@golang.org>2019-04-15 04:12:52 +0000
commit60a8dbf3b4e10627b9f5c3a0a0bf4462247270c8 (patch)
tree19feceeabf39060c5b42150965e4600e9218eb2f /src/cmd/cover
parenta01d108e30a00f9126253e061d679b07d9ff72b7 (diff)
downloadgo-60a8dbf3b4e10627b9f5c3a0a0bf4462247270c8.tar.gz
go-60a8dbf3b4e10627b9f5c3a0a0bf4462247270c8.zip
go/token: add IsIdentifier, IsKeyword, and IsExported
Telling whether a string is a valid Go identifier can seem like an easy task, but it's easy to forget about the edge cases. For example, some implementations out there forget that an empty string or keywords like "func" aren't valid identifiers. Add a simple implementation with proper Unicode support, and start using it in cmd/cover and cmd/doc. Other pieces of the standard library reimplement part of this logic, but don't use a "func(string) bool" signature, so we're leaving them untouched for now. Add some tests too, to ensure that we actually got these edge cases correctly. Since telling whether a string is a valid identifier requires knowing that it's not a valid keyword, add IsKeyword too. The internal map was already accessible via Lookup, but "Lookup(str) != IDENT" isn't as easy to understand as IsKeyword(str). And, as per Josh's suggestion, we could have IsKeyword (and probably Lookup too) use a perfect hash function instead of a global map. Finally, for consistency with these new functions, add IsExported. That makes go/ast.IsExported a bit redundant, so perhaps it can be deprecated in favor of go/token.IsExported in the future. Clarify that token.IsExported doesn't imply token.IsIdentifier, to avoid ambiguity. Fixes #30064. Change-Id: I0e0e49215fd7e47b603ebc2b5a44086c51ba57f7 Reviewed-on: https://go-review.googlesource.com/c/go/+/169018 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/cmd/cover')
-rw-r--r--src/cmd/cover/cover.go19
1 files changed, 1 insertions, 18 deletions
diff --git a/src/cmd/cover/cover.go b/src/cmd/cover/cover.go
index 2394e57977..1748606c5e 100644
--- a/src/cmd/cover/cover.go
+++ b/src/cmd/cover/cover.go
@@ -16,7 +16,6 @@ import (
"log"
"os"
"sort"
- "unicode"
"cmd/internal/edit"
"cmd/internal/objabi"
@@ -117,7 +116,7 @@ func parseFlags() error {
return fmt.Errorf("too many options")
}
- if *varVar != "" && !isValidIdentifier(*varVar) {
+ if *varVar != "" && !token.IsIdentifier(*varVar) {
return fmt.Errorf("-var: %q is not a valid identifier", *varVar)
}
@@ -685,22 +684,6 @@ func (f *File) addVariables(w io.Writer) {
}
}
-func isValidIdentifier(ident string) bool {
- if len(ident) == 0 {
- return false
- }
- for i, c := range ident {
- if i > 0 && unicode.IsDigit(c) {
- continue
- }
- if c == '_' || unicode.IsLetter(c) {
- continue
- }
- return false
- }
- return true
-}
-
// It is possible for positions to repeat when there is a line
// directive that does not specify column information and the input
// has not been passed through gofmt.