Idiomatically determine all the characters that can be used for symbols: Difference between revisions
Content added Content deleted
(→{{header|Go}}: Replace with really idiomatic code; the previous code had too many magic numbers) |
(→{{header|Go}}: Better intro, interesting code to the top) |
||
Line 71:
=={{header|Go}}==
This program uses the Go parser to check whether an identifier is indeed valid.
It checks separately which Unicode code points may appear at the beginning of an identifier, or in the remaining name.
The code assumes that the Go language does not have a keyword or otherwise reserved symbol of length 1,
or of length 2 starting with the underscore.
<lang go>package main
Line 82 ⟶ 85:
"unicode"
)
func isValidIdentifier(identifier string) bool {▼
node, err := parser.ParseExpr(identifier)▼
if err != nil {▼
return false▼
}▼
ident, ok := node.(*ast.Ident)▼
return ok && ident.Name == identifier▼
}▼
type runeRanges struct {
Line 123 ⟶ 135:
r.writeTo(&ranges)
return strings.Join(ranges, ", ")
▲}
▲func isValidIdentifier(identifier string) bool {
▲ node, err := parser.ParseExpr(identifier)
▲ if err != nil {
▲ return false
▲ }
▲ ident, ok := node.(*ast.Ident)
▲ return ok && ident.Name == identifier
}
|