2026-12-01 17:38:10 +00:00
|
|
|
package sfz
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-12-01 18:12:16 +00:00
|
|
|
"io/fs"
|
|
|
|
|
"path"
|
|
|
|
|
"regexp"
|
2026-12-01 17:38:10 +00:00
|
|
|
"slices"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// 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.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (d *Document) Validate() []Problem {
|
2026-12-01 18:20:35 +00:00
|
|
|
defs := defines{}
|
|
|
|
|
v := &validator{defs: defs, known: map[string]known{}}
|
2026-12-01 17:38:10 +00:00
|
|
|
v.problems = append(v.problems, d.problems...)
|
2026-12-01 18:20:35 +00:00
|
|
|
walk(d.Root, defs, v)
|
2026-12-01 17:38:10 +00:00
|
|
|
v.endHeader()
|
|
|
|
|
return v.problems
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// opcodeAt is the numeric value of an opcode of a header, with where it is.
|
2026-12-01 17:38:10 +00:00
|
|
|
type opcodeAt struct {
|
|
|
|
|
value float64
|
|
|
|
|
at *Node
|
2026-12-01 18:27:17 +00:00
|
|
|
file *File
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// known is the result of a [Lookup], kept so it needn't be made again.
|
2026-12-01 18:12:16 +00:00
|
|
|
type known struct {
|
|
|
|
|
info OpcodeInfo
|
|
|
|
|
ok bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// validator is the [visitor] that finds the problems in a document's nodes.
|
2026-12-01 17:38:10 +00:00
|
|
|
type validator struct {
|
2026-12-01 18:20:35 +00:00
|
|
|
// defs are the variables defined so far, which [walk] sets.
|
|
|
|
|
defs defines
|
2026-12-01 18:12:16 +00:00
|
|
|
// known holds the lookups made, as a document names the same few opcodes
|
|
|
|
|
// many times.
|
2026-12-01 19:08:00 +00:00
|
|
|
known map[string]known
|
|
|
|
|
// problems are those found so far.
|
2026-12-01 17:38:10 +00:00
|
|
|
problems []Problem
|
|
|
|
|
// inHeader is whether a header has been read.
|
|
|
|
|
inHeader bool
|
|
|
|
|
// numbers are the numeric values of the opcodes of the header being read,
|
|
|
|
|
// by name.
|
|
|
|
|
numbers map[string]opcodeAt
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// add records a problem at the node n of the file f.
|
2026-12-01 18:27:17 +00:00
|
|
|
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})
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// define reports the undefined variables that a #define's value uses.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (v *validator) define(f *File, n *Node) { v.undefined(f, n, n.Value) }
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// include reports the undefined variables that an #include's path uses, and a
|
|
|
|
|
// path that leaves the folder of the main file.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (v *validator) include(f *File, n *Node) {
|
|
|
|
|
v.undefined(f, n, n.Value)
|
|
|
|
|
if n.Resolved != "" && !fs.ValidPath(n.Resolved) {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityWarning, "include.outside", fmt.Sprintf("%s is outside the folder of the main file", n.Resolved))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// variables reports the undefined variables that a node uses.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (v *validator) variables(f *File, n *Node) { v.undefined(f, n, n.Raw) }
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// text reports text that isn't SFZ. Text that holds a variable is left, as the
|
|
|
|
|
// variable may be undefined.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (v *validator) text(f *File, n *Node, raw string) {
|
|
|
|
|
if strings.Contains(raw, "$") {
|
|
|
|
|
return
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(raw)))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// header ends the header before it, and checks that the format has the header.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (v *validator) header(f *File, n *Node, name string, _ bool) {
|
2026-12-01 17:38:10 +00:00
|
|
|
v.endHeader()
|
|
|
|
|
v.inHeader = true
|
|
|
|
|
v.numbers = map[string]opcodeAt{}
|
|
|
|
|
if _, ok := LookupHeader(name); !ok {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityInfo, "header.unknown", fmt.Sprintf("<%s> isn't a header of the format", name))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// endHeader checks the header that has just ended, for an opcode that starts
|
|
|
|
|
// with lo and is above the one that is its hi.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (v *validator) endHeader() {
|
|
|
|
|
names := make([]string, 0, len(v.numbers))
|
|
|
|
|
for name := range v.numbers {
|
|
|
|
|
names = append(names, name)
|
|
|
|
|
}
|
|
|
|
|
slices.Sort(names)
|
|
|
|
|
for _, name := range names {
|
|
|
|
|
rest, ok := strings.CutPrefix(name, "lo")
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
hi, ok := v.numbers["hi"+rest]
|
|
|
|
|
if lo := v.numbers[name]; ok && lo.value > hi.value {
|
|
|
|
|
v.add(lo.file, lo.at, SeverityWarning, "range.inverted", fmt.Sprintf("%s is above hi%s", name, rest))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
v.numbers = nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// 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.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (v *validator) opcode(f *File, n *Node, name, value string, _ bool) {
|
2026-12-01 17:38:10 +00:00
|
|
|
if !v.inHeader {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityInfo, "opcode.no-header", fmt.Sprintf("%s is before any header", name))
|
2026-12-01 17:38:10 +00:00
|
|
|
v.inHeader = true
|
|
|
|
|
v.numbers = map[string]opcodeAt{}
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(name, "$") {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-12-01 18:12:16 +00:00
|
|
|
k, seen := v.known[name]
|
|
|
|
|
if !seen {
|
|
|
|
|
k.info, k.ok = Lookup(name)
|
|
|
|
|
v.known[name] = k
|
|
|
|
|
}
|
|
|
|
|
info, ok := k.info, k.ok
|
2026-12-01 17:38:10 +00:00
|
|
|
if !ok {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityInfo, "opcode.unknown", fmt.Sprintf("%s isn't an opcode of the format", name))
|
2026-12-01 17:38:10 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if info.Deprecated {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityInfo, "opcode.deprecated", fmt.Sprintf("%s is deprecated", name))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
if strings.Contains(value, "$") {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-12-01 18:12:16 +00:00
|
|
|
if (name == "sample" || name == "default_path") && outsideFolder(value) {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityInfo, "sample.outside", fmt.Sprintf("%s=%s is outside the folder of the main file", name, value))
|
2026-12-01 18:12:16 +00:00
|
|
|
}
|
2026-12-01 17:38:10 +00:00
|
|
|
v.value(f, n, name, value, info)
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// 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.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (v *validator) value(f *File, n *Node, name, value string, info OpcodeInfo) {
|
|
|
|
|
switch info.Type {
|
|
|
|
|
case TypeInteger, TypeFloat:
|
|
|
|
|
var x float64
|
|
|
|
|
var ok bool
|
|
|
|
|
if info.Note {
|
|
|
|
|
var note int
|
|
|
|
|
note, ok = parseNote(value)
|
|
|
|
|
x = float64(note)
|
|
|
|
|
if strings.EqualFold(value, "sample") && name == "pitch_keycenter" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
x, ok = parseNumber(value)
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityWarning, "value.type", fmt.Sprintf("%s=%s isn't a number", name, value))
|
2026-12-01 17:38:10 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if info.Type == TypeInteger && !info.Note && x != float64(int64(x)) {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityInfo, "value.type", fmt.Sprintf("%s=%s isn't a whole number", name, value))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
if info.Min != nil && x < *info.Min || info.Max != nil && x > *info.Max {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityWarning, "value.range", fmt.Sprintf("%s=%s is outside %s", name, value, bounds(info)))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
2026-12-01 18:27:17 +00:00
|
|
|
v.numbers[name] = opcodeAt{value: x, at: n, file: f}
|
2026-12-01 17:38:10 +00:00
|
|
|
case TypeString:
|
|
|
|
|
if len(info.Options) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, o := range info.Options {
|
|
|
|
|
if strings.EqualFold(o, strings.TrimSpace(value)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityWarning, "value.option", fmt.Sprintf("%s=%s isn't one of %s", name, value, strings.Join(info.Options, ", ")))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// bounds describes the range an opcode's value may take, as text.
|
2026-12-01 17:38:10 +00:00
|
|
|
func bounds(info OpcodeInfo) string {
|
|
|
|
|
lo, hi := "-∞", "∞"
|
|
|
|
|
if info.Min != nil {
|
|
|
|
|
lo = fmt.Sprint(*info.Min)
|
|
|
|
|
}
|
|
|
|
|
if info.Max != nil {
|
|
|
|
|
hi = fmt.Sprint(*info.Max)
|
|
|
|
|
}
|
|
|
|
|
return lo + " to " + hi
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// undefined reports the variables in text of the node n that haven't been
|
|
|
|
|
// defined.
|
2026-12-01 17:38:10 +00:00
|
|
|
func (v *validator) undefined(f *File, n *Node, text string) {
|
2026-12-01 18:20:35 +00:00
|
|
|
for _, run := range v.defs.undefined(text) {
|
2026-12-01 18:27:17 +00:00
|
|
|
v.add(f, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
2026-12-01 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-12-01 18:12:16 +00:00
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// driveRE reads the drive at the start of a Windows path.
|
2026-12-01 18:12:16 +00:00
|
|
|
var driveRE = regexp.MustCompile(`^[A-Za-z]:`)
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// outsideFolder reports whether a path is absolute, or leaves the folder that
|
|
|
|
|
// it is relative to by going up.
|
2026-12-01 18:12:16 +00:00
|
|
|
func outsideFolder(p string) bool {
|
|
|
|
|
p = strings.ReplaceAll(strings.TrimSpace(p), `\`, "/")
|
|
|
|
|
if strings.HasPrefix(p, "/") || driveRE.MatchString(p) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
p = path.Clean(p)
|
|
|
|
|
return p == ".." || strings.HasPrefix(p, "../")
|
|
|
|
|
}
|