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: Line 71:
=={{header|Go}}==
=={{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
<lang go>package main
Line 82: Line 85:
"unicode"
"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 {
type runeRanges struct {
Line 123: Line 135:
r.writeTo(&ranges)
r.writeTo(&ranges)
return strings.Join(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
}
}