Skip to content

Commit

Permalink
internal/wire: replace keyword list with token api (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored and shantuo committed Jul 16, 2019
1 parent 2f518a0 commit 66f78fc
Showing 1 changed file with 3 additions and 33 deletions.
36 changes: 3 additions & 33 deletions internal/wire/wire.go
Expand Up @@ -845,7 +845,7 @@ func typeVariableName(t types.Type, defaultName string, transform func(string) s

// See if there's an unambiguous name; if so, use it.
for _, name := range names {
if !reservedKeyword[name] && !collides(name) {
if !token.Lookup(name).IsKeyword() && !collides(name) {
return name
}
}
Expand Down Expand Up @@ -903,40 +903,10 @@ func export(name string) string {
return sbuf.String()
}

// reservedKeyword is a set of Go's reserved keywords:
// https://golang.org/ref/spec#Keywords
var reservedKeyword = map[string]bool{
"break": true,
"case": true,
"chan": true,
"const": true,
"continue": true,
"default": true,
"defer": true,
"else": true,
"fallthrough": true,
"for": true,
"func": true,
"go": true,
"goto": true,
"if": true,
"import": true,
"interface": true,
"map": true,
"package": true,
"range": true,
"return": true,
"select": true,
"struct": true,
"switch": true,
"type": true,
"var": true,
}

// disambiguate picks a unique name, preferring name if it is already unique.
// It also disambiguates against Go's reserved keywords.
func disambiguate(name string, collides func(string) bool) string {
if !reservedKeyword[name] && !collides(name) {
if !token.Lookup(name).IsKeyword() && !collides(name) {
return name
}
buf := []byte(name)
Expand All @@ -947,7 +917,7 @@ func disambiguate(name string, collides func(string) bool) string {
for n := 2; ; n++ {
buf = strconv.AppendInt(buf[:base], int64(n), 10)
sbuf := string(buf)
if !reservedKeyword[sbuf] && !collides(sbuf) {
if !token.Lookup(sbuf).IsKeyword() && !collides(sbuf) {
return sbuf
}
}
Expand Down

0 comments on commit 66f78fc

Please sign in to comment.