Rewrite the code comments for consistent tone and accuracy
Comments only: the tokens of every Go file are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+22
-4
@@ -20,7 +20,8 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// entry is an opcode as it goes into the registry.
|
||||
// entry is an opcode as it goes into the registry, with the fields of
|
||||
// OpcodeInfo that the data gives it.
|
||||
type entry struct {
|
||||
name string
|
||||
aliases []string
|
||||
@@ -35,18 +36,21 @@ type entry struct {
|
||||
description string
|
||||
}
|
||||
|
||||
// notes are the integer opcodes that also take a note name.
|
||||
// notes matches the names of the integer opcodes that also take a note name.
|
||||
var notes = regexp.MustCompile(`^(?:key|(?:lo|hi)key|pitch_keycenter|sw_(?:lokey|hikey|last|previous|down|up|default)|xf(?:in|out)_(?:lo|hi)key|(?:lo|hi)_?key)$`)
|
||||
|
||||
// tags matches an HTML tag.
|
||||
var tags = regexp.MustCompile(`<[^>]*>`)
|
||||
|
||||
// text turns the data's HTML snippets into plain text.
|
||||
// text turns one of the data's HTML snippets into plain text on one line. It
|
||||
// returns an empty string for a value that isn't text.
|
||||
func text(v any) string {
|
||||
s, _ := v.(string)
|
||||
s = html.UnescapeString(tags.ReplaceAllString(s, ""))
|
||||
return strings.Join(strings.Fields(s), " ")
|
||||
}
|
||||
|
||||
// str returns a value of the data as text, or an empty string if there is none.
|
||||
func str(v any) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
@@ -54,6 +58,7 @@ func str(v any) string {
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
|
||||
// number returns a number of the data, or nil if the value isn't one.
|
||||
func number(v any) *float64 {
|
||||
switch n := v.(type) {
|
||||
case int:
|
||||
@@ -65,23 +70,28 @@ func number(v any) *float64 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// asMap returns v as a map, or nil if it isn't one.
|
||||
func asMap(v any) map[string]any {
|
||||
m, _ := v.(map[string]any)
|
||||
return m
|
||||
}
|
||||
|
||||
// asList returns v as a list, or nil if it isn't one.
|
||||
func asList(v any) []any {
|
||||
l, _ := v.([]any)
|
||||
return l
|
||||
}
|
||||
|
||||
// generator collects the opcodes of the data.
|
||||
type generator struct {
|
||||
// entries are the opcodes read so far, in the order they are listed.
|
||||
entries []*entry
|
||||
byName map[string]*entry
|
||||
}
|
||||
|
||||
// add reads an opcode of the data, and its aliases and modulations, into
|
||||
// entries. inherit is the opcode whose value an opcode without one takes.
|
||||
// entries, and returns it, or nil if the data's entry isn't an opcode. inherit
|
||||
// is the opcode whose value an opcode without one takes, or nil.
|
||||
func (g *generator) add(x map[string]any, category string, inherit *entry) *entry {
|
||||
name := str(x["name"])
|
||||
if name == "" || x["macro"] == true {
|
||||
@@ -173,6 +183,9 @@ func (g *generator) modulations(x map[string]any, e *entry, category string, par
|
||||
}
|
||||
}
|
||||
|
||||
// walk reads the opcodes under a part of the data's tree of categories into
|
||||
// entries. The category of an opcode is the names of the categories it is under,
|
||||
// joined with a slash; category is those of the parts above o.
|
||||
func (g *generator) walk(o any, category string) {
|
||||
switch v := o.(type) {
|
||||
case map[string]any:
|
||||
@@ -206,8 +219,11 @@ func (g *generator) walk(o any, category string) {
|
||||
}
|
||||
}
|
||||
|
||||
// quote returns s as a Go string literal.
|
||||
func quote(s string) string { return strconv.Quote(s) }
|
||||
|
||||
// fl returns the Go expression for an optional number: nil, or a pointer made
|
||||
// with ptr.
|
||||
func fl(p *float64) string {
|
||||
if p == nil {
|
||||
return "nil"
|
||||
@@ -215,6 +231,7 @@ func fl(p *float64) string {
|
||||
return "ptr(float64(" + strconv.FormatFloat(*p, 'g', -1, 64) + "))"
|
||||
}
|
||||
|
||||
// main reads the syntax data and writes the registry's Go source.
|
||||
func main() {
|
||||
in := flag.String("in", "syntax.yml", "the syntax data")
|
||||
out := flag.String("out", "registry_data.go", "the Go file to write")
|
||||
@@ -272,6 +289,7 @@ func main() {
|
||||
log.Printf("wrote %d opcodes", count)
|
||||
}
|
||||
|
||||
// strs returns the Go expression for a list of strings.
|
||||
func strs(s []string) string {
|
||||
if len(s) == 0 {
|
||||
return "nil"
|
||||
|
||||
Reference in New Issue
Block a user