-
Notifications
You must be signed in to change notification settings - Fork 18k
go/token: add IsIdentifier, IsKeyword, and IsExported funcs #30064
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I think this is a fine idea, but the function belongs in go/token, not go/ast. |
@ianthehat pointed out keywords, which I was forgetting about. The spec mentions:
So I presume that Ian also mentions that
However, I still think that an |
We might consider IsKeyword in go/token as well. In addition to being convenient, the implementation could use a perfect hash for faster checks (as does cmd/compile/internal/syntax). |
I'd be happy for us to add IsIdentifier, IsKeyword, and IsExported to go/token. |
Not sure if this is strictly necessary, since it's already part of |
Exactly. The go/ast function would delegate to the go/token implementation. |
I'm ok with @adonovan said. |
Please send CLs to me. Thanks. |
Hi @mvdan @adonovan @griesemer. Can I work on this one? I don't see a CL attached. |
I’m pretty sure @mvdan is working on this; he assigned it to himself. |
@josharian Cool, thanks. :) |
I will get to it soon - I just have a moderately sized backlog of stuff to go through, now that the tree is open again :) |
Change https://golang.org/cl/169018 mentions this issue: |
We already have
go/ast.IsExported
, which is helpful; it makes the operation simple and expressive, and it avoids the simple mistake of only checking ASCII and ignoring Unicode.Checking if a string is a valid identifier as per the spec (https://golang.org/ref/spec#Identifiers) falls under the same category, I believe. It seems intuitive, so developers tend to write a simple implementation by hand without much effort. However, there are many gotchas which are easy to get wrong:
_
is allowed, even as a first characterFor example, take this implementation I just came across this morning:
It gets three of the gotchas wrong; it would return true on
""
and"123"
, and return false on"αβ"
. I imagine that a correct implementation would be like:However, I'm still not 100% confident that this is correct. I'd probably compare it to implementations in
go/*
andx/tools
before using the code, just in case.The standard library implements variants of this in multiple places;
go/scanner
does it on a per-rune basis while scanning,go/build
implements it in itsimportReader
, andcmd/doc
has anisIdentifier
func. The same applies to other official repos likex/tools
, where I've foundgodoc.isIdentifier
,semver.isIdentChar
,analysis.validIdent
, andrename.isValidIdentifier
.I think we should have a canonical implementation with a proper godoc and tests. My proposal is to add this to
go/ast
, as it would already be useful to other packages within the standard library. I also think this is a fairly low-level API, and it's commonly needed whenever one is modifying Go syntax trees, creating new Go files, or otherwise tinkering with Go syntax.If adding API to
go/ast
is a problem, perhapsgolang.org/x/tools/go/ast/astutil
. However, in my mind that package contains much higher-level functions, so it feels a bit out of place.I realise this could have been a proposal, but I thought the proposed change wouldn't be controversial enough to warrant one. Feel free to retitle it as a proposal if preferred.
/cc @griesemer @josharian @alandonovan @dominikh
The text was updated successfully, but these errors were encountered: