198 lines
5.4 KiB
Go
198 lines
5.4 KiB
Go
package sfz
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"slices"
|
||
|
|
"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.
|
||
|
|
func (d *Document) Validate() []Problem {
|
||
|
|
v := &validator{defines: map[string]string{}}
|
||
|
|
v.problems = append(v.problems, d.problems...)
|
||
|
|
v.file(d.Root)
|
||
|
|
v.endHeader()
|
||
|
|
return v.problems
|
||
|
|
}
|
||
|
|
|
||
|
|
// opcodeAt is an opcode read in a header.
|
||
|
|
type opcodeAt struct {
|
||
|
|
value float64
|
||
|
|
at *Node
|
||
|
|
file string
|
||
|
|
}
|
||
|
|
|
||
|
|
type validator struct {
|
||
|
|
defines map[string]string
|
||
|
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *validator) add(file string, n *Node, sev Severity, code, msg string) {
|
||
|
|
v.problems = append(v.problems, Problem{File: file, Line: n.Line, Col: n.Col, Severity: sev, Code: code, Msg: msg})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *validator) file(f *File) {
|
||
|
|
for _, n := range f.Nodes {
|
||
|
|
switch n.Kind {
|
||
|
|
case KindDefine:
|
||
|
|
v.undefined(f, n, n.Value)
|
||
|
|
v.defines[n.Name] = strings.TrimSpace(substitute(v.defines, n.Value))
|
||
|
|
case KindInclude:
|
||
|
|
v.undefined(f, n, n.Value)
|
||
|
|
if n.File != nil {
|
||
|
|
v.file(n.File)
|
||
|
|
}
|
||
|
|
case KindHeader:
|
||
|
|
v.header(f, n, n.Name)
|
||
|
|
case KindOpcode, KindText:
|
||
|
|
v.undefined(f, n, n.Raw)
|
||
|
|
text := n.Raw
|
||
|
|
if strings.Contains(text, "$") {
|
||
|
|
// A variable's value can hold opcodes and headers, so the
|
||
|
|
// text it makes is read again.
|
||
|
|
text = substitute(v.defines, text)
|
||
|
|
}
|
||
|
|
v.read(f, n, text)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// read checks the headers and opcodes that text holds, which stands where n
|
||
|
|
// does.
|
||
|
|
func (v *validator) read(f *File, n *Node, text string) {
|
||
|
|
for _, m := range lex(text) {
|
||
|
|
switch m.Kind {
|
||
|
|
case KindHeader:
|
||
|
|
v.header(f, n, m.Name)
|
||
|
|
case KindOpcode:
|
||
|
|
v.opcode(f, n, m.Name, m.Value)
|
||
|
|
case KindText:
|
||
|
|
if strings.Contains(m.Raw, "$") {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
v.add(f.Path, n, SeverityWarning, "text.unreadable", fmt.Sprintf("can't read %q", strings.TrimSpace(m.Raw)))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *validator) header(f *File, n *Node, name string) {
|
||
|
|
v.endHeader()
|
||
|
|
v.inHeader = true
|
||
|
|
v.numbers = map[string]opcodeAt{}
|
||
|
|
if _, ok := LookupHeader(name); !ok {
|
||
|
|
v.add(f.Path, n, SeverityInfo, "header.unknown", fmt.Sprintf("<%s> isn't a header of the format", name))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// endHeader checks the ranges of the header that has just ended: a lo opcode
|
||
|
|
// above its hi opcode.
|
||
|
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *validator) opcode(f *File, n *Node, name, value string) {
|
||
|
|
if !v.inHeader {
|
||
|
|
v.add(f.Path, n, SeverityInfo, "opcode.no-header", fmt.Sprintf("%s is before any header", name))
|
||
|
|
v.inHeader = true
|
||
|
|
v.numbers = map[string]opcodeAt{}
|
||
|
|
}
|
||
|
|
if strings.Contains(name, "$") {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
info, ok := Lookup(name)
|
||
|
|
if !ok {
|
||
|
|
v.add(f.Path, n, SeverityInfo, "opcode.unknown", fmt.Sprintf("%s isn't an opcode of the format", name))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if info.Deprecated {
|
||
|
|
v.add(f.Path, n, SeverityInfo, "opcode.deprecated", fmt.Sprintf("%s is deprecated", name))
|
||
|
|
}
|
||
|
|
if strings.Contains(value, "$") {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
v.value(f, n, name, value, info)
|
||
|
|
}
|
||
|
|
|
||
|
|
// value checks a value against what the format says about its opcode.
|
||
|
|
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 {
|
||
|
|
v.add(f.Path, n, SeverityWarning, "value.type", fmt.Sprintf("%s=%s isn't a number", name, value))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if info.Type == TypeInteger && !info.Note && x != float64(int64(x)) {
|
||
|
|
v.add(f.Path, n, SeverityInfo, "value.type", fmt.Sprintf("%s=%s isn't a whole number", name, value))
|
||
|
|
}
|
||
|
|
if info.Min != nil && x < *info.Min || info.Max != nil && x > *info.Max {
|
||
|
|
v.add(f.Path, n, SeverityWarning, "value.range", fmt.Sprintf("%s=%s is outside %s", name, value, bounds(info)))
|
||
|
|
}
|
||
|
|
v.numbers[name] = opcodeAt{value: x, at: n, file: f.Path}
|
||
|
|
case TypeString:
|
||
|
|
if len(info.Options) == 0 {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
for _, o := range info.Options {
|
||
|
|
if strings.EqualFold(o, strings.TrimSpace(value)) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
v.add(f.Path, n, SeverityWarning, "value.option", fmt.Sprintf("%s=%s isn't one of %s", name, value, strings.Join(info.Options, ", ")))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// bounds describes the range an opcode's value may take.
|
||
|
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
// undefined reports the variables in text that haven't been defined.
|
||
|
|
func (v *validator) undefined(f *File, n *Node, text string) {
|
||
|
|
for _, run := range variableRE.FindAllString(text, -1) {
|
||
|
|
if substitute(v.defines, run) == run {
|
||
|
|
v.add(f.Path, n, SeverityWarning, "define.undefined", fmt.Sprintf("%s isn't defined", run))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|