Add lossless SFZ document, flattening, typed model and opcode registry
Parse keeps a file exactly as written, with #define and #include unexpanded and each include as its own file in a tree. Flatten resolves directives and header inheritance, and Flat.Instrument reads regions into the typed model ported from Zampler's internal/sfz. The opcode registry is generated from the SFZ format's syntax data and drives Lookup, GetAs and Validate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
package sfz
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var (
|
||||
// headerRE reads a header at the start of the text.
|
||||
headerRE = regexp.MustCompile(`^<([A-Za-z_]+)>`)
|
||||
// opcodeRE reads an opcode's name and = at the start of the text.
|
||||
opcodeRE = regexp.MustCompile(`^([A-Za-z0-9_$]+)=`)
|
||||
// defineRE reads a #define's variable and value, up to the end of its line.
|
||||
defineRE = regexp.MustCompile(`^#define[ \t]+(\$[A-Za-z0-9_]+)[ \t]+([^\r\n]*)`)
|
||||
// includeRE reads an #include's quoted path.
|
||||
includeRE = regexp.MustCompile(`^#include[ \t]+"([^"\r\n]*)"`)
|
||||
// breakRE finds where an opcode's value ends: at a header, the next
|
||||
// opcode, a comment, or a directive after whitespace. Without one it runs
|
||||
// to the end of its line, so it can hold spaces, as a sample's path can.
|
||||
breakRE = regexp.MustCompile(`<[A-Za-z_]+>|[A-Za-z0-9_$]+=|//|/\*|\s#(?:define|include)\b`)
|
||||
)
|
||||
|
||||
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' }
|
||||
|
||||
func isNameChar(c byte) bool {
|
||||
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '$'
|
||||
}
|
||||
|
||||
// lexer cuts text into nodes and keeps count of the position.
|
||||
type lexer struct {
|
||||
nodes []*Node
|
||||
line int
|
||||
// lineStart is the offset where the current line starts.
|
||||
lineStart int
|
||||
// pos is the offset the next node starts at.
|
||||
pos int
|
||||
}
|
||||
|
||||
// add appends a node for the text from the current position to end.
|
||||
func (l *lexer) add(src string, end int, n *Node) {
|
||||
n.Raw = src[l.pos:end]
|
||||
n.Line, n.Col = l.line, l.pos-l.lineStart+1
|
||||
if n.Kind == KindText && len(l.nodes) > 0 {
|
||||
if last := l.nodes[len(l.nodes)-1]; last.Kind == KindText {
|
||||
last.Raw += n.Raw
|
||||
l.advance(src, end)
|
||||
return
|
||||
}
|
||||
}
|
||||
l.nodes = append(l.nodes, n)
|
||||
l.advance(src, end)
|
||||
}
|
||||
|
||||
// advance moves the position to end, counting the lines it passes.
|
||||
func (l *lexer) advance(src string, end int) {
|
||||
for i := l.pos; i < end; i++ {
|
||||
if src[i] == '\n' {
|
||||
l.line++
|
||||
l.lineStart = i + 1
|
||||
}
|
||||
}
|
||||
l.pos = end
|
||||
}
|
||||
|
||||
// lex cuts text into nodes whose raw text, joined, is the text.
|
||||
func lex(src string) []*Node {
|
||||
l := &lexer{line: 1}
|
||||
for l.pos < len(src) {
|
||||
i := l.pos
|
||||
rest := src[i:]
|
||||
switch {
|
||||
case isSpace(src[i]):
|
||||
j := i
|
||||
for j < len(src) && isSpace(src[j]) {
|
||||
j++
|
||||
}
|
||||
l.add(src, j, &Node{Kind: KindSpace})
|
||||
case strings.HasPrefix(rest, "//"):
|
||||
j := len(src)
|
||||
if e := strings.IndexByte(rest, '\n'); e >= 0 {
|
||||
j = i + e
|
||||
}
|
||||
l.add(src, j, &Node{Kind: KindComment})
|
||||
case strings.HasPrefix(rest, "/*"):
|
||||
j := len(src)
|
||||
if e := strings.Index(rest[2:], "*/"); e >= 0 {
|
||||
j = i + 2 + e + 2
|
||||
}
|
||||
l.add(src, j, &Node{Kind: KindComment})
|
||||
case src[i] == '<' && headerRE.MatchString(rest):
|
||||
m := headerRE.FindStringSubmatch(rest)
|
||||
l.add(src, i+len(m[0]), &Node{Kind: KindHeader, Name: m[1]})
|
||||
case src[i] == '#' && (i == 0 || isSpace(src[i-1])) && l.directive(src):
|
||||
case opcodeRE.MatchString(rest):
|
||||
m := opcodeRE.FindStringSubmatch(rest)
|
||||
valueStart := i + len(m[0])
|
||||
lineEnd := len(src)
|
||||
if e := strings.IndexByte(src[valueStart:], '\n'); e >= 0 {
|
||||
lineEnd = valueStart + e
|
||||
}
|
||||
end := lineEnd
|
||||
if loc := breakRE.FindStringIndex(src[valueStart:lineEnd]); loc != nil {
|
||||
end = valueStart + loc[0]
|
||||
}
|
||||
for end > valueStart && isSpace(src[end-1]) {
|
||||
end--
|
||||
}
|
||||
l.add(src, end, &Node{Kind: KindOpcode, Name: m[1], Value: strings.TrimSpace(src[valueStart:end])})
|
||||
case isNameChar(src[i]):
|
||||
j := i
|
||||
for j < len(src) && isNameChar(src[j]) {
|
||||
j++
|
||||
}
|
||||
l.add(src, j, &Node{Kind: KindText})
|
||||
default:
|
||||
_, size := utf8.DecodeRuneInString(rest)
|
||||
l.add(src, i+size, &Node{Kind: KindText})
|
||||
}
|
||||
}
|
||||
return l.nodes
|
||||
}
|
||||
|
||||
// directive adds a #define or #include at the current position, and reports
|
||||
// whether it was one that can be read.
|
||||
func (l *lexer) directive(src string) bool {
|
||||
rest := src[l.pos:]
|
||||
if m := defineRE.FindStringSubmatch(rest); m != nil {
|
||||
length := len(m[0])
|
||||
// A comment ends the value.
|
||||
valueStart := length - len(m[2])
|
||||
if loc := commentRE.FindStringIndex(m[2]); loc != nil {
|
||||
length = valueStart + loc[0]
|
||||
}
|
||||
for length > valueStart && isSpace(rest[length-1]) {
|
||||
length--
|
||||
}
|
||||
l.add(src, l.pos+length, &Node{Kind: KindDefine, Name: m[1], Value: strings.TrimSpace(rest[valueStart:length])})
|
||||
return true
|
||||
}
|
||||
if m := includeRE.FindStringSubmatch(rest); m != nil {
|
||||
l.add(src, l.pos+len(m[0]), &Node{Kind: KindInclude, Value: m[1]})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// commentRE finds the start of a comment.
|
||||
var commentRE = regexp.MustCompile(`//|/\*`)
|
||||
Reference in New Issue
Block a user