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:
+32
-14
@@ -9,9 +9,11 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Validate returns the problems in the document, in the order the files are
|
||||
// read. It reports what is wrong with the text and with values the format
|
||||
// describes, and never fails on an opcode it doesn't know.
|
||||
// Validate returns the problems in the document: those found while it was
|
||||
// read, then those in each file, in the order the files are read. It reports
|
||||
// what is wrong with the text, and with the values of opcodes that the format
|
||||
// describes. An opcode or header that it doesn't know is only information, and
|
||||
// is never an error.
|
||||
func (d *Document) Validate() []Problem {
|
||||
defs := defines{}
|
||||
v := &validator{defs: defs, known: map[string]known{}}
|
||||
@@ -21,25 +23,27 @@ func (d *Document) Validate() []Problem {
|
||||
return v.problems
|
||||
}
|
||||
|
||||
// opcodeAt is an opcode read in a header.
|
||||
// opcodeAt is the numeric value of an opcode of a header, with where it is.
|
||||
type opcodeAt struct {
|
||||
value float64
|
||||
at *Node
|
||||
file *File
|
||||
}
|
||||
|
||||
// known is the result of a [Lookup].
|
||||
// known is the result of a [Lookup], kept so it needn't be made again.
|
||||
type known struct {
|
||||
info OpcodeInfo
|
||||
ok bool
|
||||
}
|
||||
|
||||
// validator is the [visitor] that finds the problems in a document's nodes.
|
||||
type validator struct {
|
||||
// defs are the variables defined so far, which [walk] sets.
|
||||
defs defines
|
||||
// known holds the lookups made, as a document names the same few opcodes
|
||||
// many times.
|
||||
known map[string]known
|
||||
known map[string]known
|
||||
// problems are those found so far.
|
||||
problems []Problem
|
||||
// inHeader is whether a header has been read.
|
||||
inHeader bool
|
||||
@@ -48,13 +52,17 @@ type validator struct {
|
||||
numbers map[string]opcodeAt
|
||||
}
|
||||
|
||||
// add records a problem at the node n of the file f.
|
||||
func (v *validator) add(f *File, n *Node, sev Severity, code, msg string) {
|
||||
line, col := f.Position(n)
|
||||
v.problems = append(v.problems, Problem{File: f.Path, Line: line, Col: col, Severity: sev, Code: code, Msg: msg})
|
||||
}
|
||||
|
||||
// define reports the undefined variables that a #define's value uses.
|
||||
func (v *validator) define(f *File, n *Node) { v.undefined(f, n, n.Value) }
|
||||
|
||||
// include reports the undefined variables that an #include's path uses, and a
|
||||
// path that leaves the folder of the main file.
|
||||
func (v *validator) include(f *File, n *Node) {
|
||||
v.undefined(f, n, n.Value)
|
||||
if n.Resolved != "" && !fs.ValidPath(n.Resolved) {
|
||||
@@ -62,8 +70,11 @@ func (v *validator) include(f *File, n *Node) {
|
||||
}
|
||||
}
|
||||
|
||||
// variables reports the undefined variables that a node uses.
|
||||
func (v *validator) variables(f *File, n *Node) { v.undefined(f, n, n.Raw) }
|
||||
|
||||
// text reports text that isn't SFZ. Text that holds a variable is left, as the
|
||||
// variable may be undefined.
|
||||
func (v *validator) text(f *File, n *Node, raw string) {
|
||||
if strings.Contains(raw, "$") {
|
||||
return
|
||||
@@ -71,6 +82,7 @@ func (v *validator) text(f *File, n *Node, raw string) {
|
||||
v.add(f, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(raw)))
|
||||
}
|
||||
|
||||
// header ends the header before it, and checks that the format has the header.
|
||||
func (v *validator) header(f *File, n *Node, name string, _ bool) {
|
||||
v.endHeader()
|
||||
v.inHeader = true
|
||||
@@ -80,8 +92,8 @@ func (v *validator) header(f *File, n *Node, name string, _ bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// endHeader checks the ranges of the header that has just ended: a lo opcode
|
||||
// above its hi opcode.
|
||||
// endHeader checks the header that has just ended, for an opcode that starts
|
||||
// with lo and is above the one that is its hi.
|
||||
func (v *validator) endHeader() {
|
||||
names := make([]string, 0, len(v.numbers))
|
||||
for name := range v.numbers {
|
||||
@@ -101,6 +113,10 @@ func (v *validator) endHeader() {
|
||||
v.numbers = nil
|
||||
}
|
||||
|
||||
// opcode checks that the format has the opcode, and that its value is one it
|
||||
// allows. An opcode before any header is reported once. A name or value that
|
||||
// uses a variable is only checked for the name, as the variable may hold
|
||||
// anything.
|
||||
func (v *validator) opcode(f *File, n *Node, name, value string, _ bool) {
|
||||
if !v.inHeader {
|
||||
v.add(f, n, SeverityInfo, "opcode.no-header", fmt.Sprintf("%s is before any header", name))
|
||||
@@ -132,7 +148,8 @@ func (v *validator) opcode(f *File, n *Node, name, value string, _ bool) {
|
||||
v.value(f, n, name, value, info)
|
||||
}
|
||||
|
||||
// value checks a value against what the format says about its opcode.
|
||||
// value checks the value of an opcode against its type, range and options, and
|
||||
// notes it, if it is a number, to check the header's ranges at its end.
|
||||
func (v *validator) value(f *File, n *Node, name, value string, info OpcodeInfo) {
|
||||
switch info.Type {
|
||||
case TypeInteger, TypeFloat:
|
||||
@@ -172,7 +189,7 @@ func (v *validator) value(f *File, n *Node, name, value string, info OpcodeInfo)
|
||||
}
|
||||
}
|
||||
|
||||
// bounds describes the range an opcode's value may take.
|
||||
// bounds describes the range an opcode's value may take, as text.
|
||||
func bounds(info OpcodeInfo) string {
|
||||
lo, hi := "-∞", "∞"
|
||||
if info.Min != nil {
|
||||
@@ -184,18 +201,19 @@ func bounds(info OpcodeInfo) string {
|
||||
return lo + " to " + hi
|
||||
}
|
||||
|
||||
// undefined reports the variables in text that haven't been defined.
|
||||
// undefined reports the variables in text of the node n that haven't been
|
||||
// defined.
|
||||
func (v *validator) undefined(f *File, n *Node, text string) {
|
||||
for _, run := range v.defs.undefined(text) {
|
||||
v.add(f, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
||||
}
|
||||
}
|
||||
|
||||
// driveRE reads the drive of a Windows path.
|
||||
// driveRE reads the drive at the start of a Windows path.
|
||||
var driveRE = regexp.MustCompile(`^[A-Za-z]:`)
|
||||
|
||||
// outsideFolder reports whether a sample path is absolute or leaves the
|
||||
// folder it is relative to.
|
||||
// outsideFolder reports whether a path is absolute, or leaves the folder that
|
||||
// it is relative to by going up.
|
||||
func outsideFolder(p string) bool {
|
||||
p = strings.ReplaceAll(strings.TrimSpace(p), `\`, "/")
|
||||
if strings.HasPrefix(p, "/") || driveRE.MatchString(p) {
|
||||
|
||||
Reference in New Issue
Block a user