2026-12-01 18:20:35 +00:00
|
|
|
package sfz
|
|
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// defines are the variables defined so far, by name with their $, with their
|
|
|
|
|
// values as they stand after the variables in them are replaced.
|
2026-12-01 18:20:35 +00:00
|
|
|
type defines map[string]string
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// set gives a variable a value, which has had its own variables replaced
|
2026-12-01 18:20:35 +00:00
|
|
|
// already.
|
|
|
|
|
func (d defines) set(name, value string) { d[name] = strings.TrimSpace(value) }
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// define sets the variable of a #define node, replacing the variables in its
|
|
|
|
|
// value.
|
2026-12-01 18:20:35 +00:00
|
|
|
func (d defines) define(n *Node) { d.set(n.Name, substitute(d, n.Value)) }
|
|
|
|
|
|
|
|
|
|
// undefined returns the variables in text that haven't been defined.
|
|
|
|
|
func (d defines) undefined(text string) []string {
|
|
|
|
|
var out []string
|
|
|
|
|
for _, run := range variableRE.FindAllString(text, -1) {
|
|
|
|
|
if substitute(d, run) == run {
|
|
|
|
|
out = append(out, run)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// visitor is what [walk] tells about the nodes of a document, in order.
|
2026-12-01 18:20:35 +00:00
|
|
|
type visitor interface {
|
2026-12-01 19:08:00 +00:00
|
|
|
// define is called for a #define, before its variable is set.
|
2026-12-01 18:20:35 +00:00
|
|
|
define(f *File, n *Node)
|
2026-12-01 19:08:00 +00:00
|
|
|
// include is called for an #include, before the file it read is walked.
|
2026-12-01 18:20:35 +00:00
|
|
|
include(f *File, n *Node)
|
|
|
|
|
// variables is called for an opcode or text node that uses variables,
|
2026-12-01 19:08:00 +00:00
|
|
|
// before the headers, opcodes and text that it makes are told of.
|
2026-12-01 18:20:35 +00:00
|
|
|
variables(f *File, n *Node)
|
2026-12-01 19:08:00 +00:00
|
|
|
// header is called for a header. Variable is whether the value of a
|
|
|
|
|
// variable made it, in which case n is the node that uses the variable.
|
2026-12-01 18:20:35 +00:00
|
|
|
header(f *File, n *Node, name string, variable bool)
|
2026-12-01 19:08:00 +00:00
|
|
|
// opcode is called for an opcode. Variable is as for header, and n is
|
|
|
|
|
// likewise the node that uses the variable.
|
2026-12-01 18:20:35 +00:00
|
|
|
opcode(f *File, n *Node, name, value string, variable bool)
|
2026-12-01 19:08:00 +00:00
|
|
|
// text is called for text that isn't an opcode or a header, which is n or
|
|
|
|
|
// is made by n.
|
2026-12-01 18:20:35 +00:00
|
|
|
text(f *File, n *Node, raw string)
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-01 19:08:00 +00:00
|
|
|
// walk tells v of the nodes of a file in order, and of the files it includes
|
|
|
|
|
// where the includes stand, with defs holding the variables as they are at each
|
|
|
|
|
// node. A node that uses a variable is lexed again with its variables replaced,
|
|
|
|
|
// because a value can hold opcodes and headers.
|
2026-12-01 18:20:35 +00:00
|
|
|
func walk(f *File, defs defines, v visitor) {
|
|
|
|
|
for _, n := range f.Nodes {
|
|
|
|
|
switch n.Kind {
|
|
|
|
|
case KindDefine:
|
|
|
|
|
v.define(f, n)
|
|
|
|
|
defs.define(n)
|
|
|
|
|
case KindInclude:
|
|
|
|
|
v.include(f, n)
|
|
|
|
|
if n.File != nil {
|
|
|
|
|
walk(n.File, defs, v)
|
|
|
|
|
}
|
|
|
|
|
case KindHeader:
|
|
|
|
|
v.header(f, n, n.Name, false)
|
|
|
|
|
case KindOpcode, KindText:
|
|
|
|
|
if !strings.Contains(n.Raw, "$") {
|
|
|
|
|
if n.Kind == KindOpcode {
|
|
|
|
|
v.opcode(f, n, n.Name, n.Value, false)
|
|
|
|
|
} else {
|
|
|
|
|
v.text(f, n, n.Raw)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
v.variables(f, n)
|
|
|
|
|
for _, m := range lex(substitute(defs, n.Raw)) {
|
|
|
|
|
switch m.Kind {
|
|
|
|
|
case KindHeader:
|
|
|
|
|
v.header(f, n, m.Name, true)
|
|
|
|
|
case KindOpcode:
|
|
|
|
|
v.opcode(f, n, m.Name, m.Value, true)
|
|
|
|
|
case KindText:
|
|
|
|
|
v.text(f, n, m.Raw)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|