Jump to content

Idiomatically determine all the characters that can be used for symbols: Difference between revisions

→‎{{header|Go}}: Better intro, interesting code to the top
(→‎{{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.
Most of the code is concerned with printing the Unicode ranges of the valid characters. The remaining part of the code parses the possible identifier and verifies that it is indeed an identifier.
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
}
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.