Bound parsing work and fix writer, lexer and keyswitch bugs

- Count variable expansions against MaxBytes and stop substituting past it, so
  nested #defines can't grow exponentially. Read at most MaxBytes from a reader.
- Lex in linear time: end an opcode value at the next line break with the same
  regexp, and merge text nodes by slicing. Cap variable names at 128 bytes.
- Read a byte order mark as space so a directive after it is recognised.
- Refuse in WriteFiles an include path that is absolute or leaves the folder, and
  report include.outside and sample.outside from Validate.
- Give a lone sw_lokey or sw_hikey a one-key range.
- Sum controller modulations in a fixed order so Instrument is deterministic.
- Stop samplePrefix looping on a sample at the root.
- Validate without lexing plain opcodes again, and cache registry lookups.
- Add regression tests and FuzzWrite, FuzzEdit and FuzzBuild.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:12:16 +00:00
co-authored by Claude Sonnet 5.5
parent 3d2ab78a32
commit cf54471778
8 changed files with 499 additions and 54 deletions
+23 -13
View File
@@ -12,15 +12,25 @@ var (
// 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]*)`)
defineRE = regexp.MustCompile(`^#define[ \t]+(\$[A-Za-z0-9_]{1,128})[ \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`)
// opcode, a comment, a directive after whitespace, or the end of the line,
// so that a value can hold spaces, as a sample's path can.
breakRE = regexp.MustCompile(`<[A-Za-z_]+>|[A-Za-z0-9_$]+=|//|/\*|\s#(?:define|include)\b|\n`)
)
// byteOrderMark is the UTF-8 mark some editors put at the start of a file. It
// is read as space.
const byteOrderMark = "\ufeff"
// atLineStart reports whether a directive may start at offset i: at the start
// of the text, after the byte order mark, or after whitespace.
func atLineStart(src string, i int) bool {
return i == 0 || isSpace(src[i-1]) || i == len(byteOrderMark) && strings.HasPrefix(src, byteOrderMark)
}
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' }
func isNameChar(c byte) bool {
@@ -43,7 +53,8 @@ func (l *lexer) add(src string, end int, n *Node) {
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
// Nodes tile the text, so the two are one run of it.
last.Raw = src[l.pos-len(last.Raw) : end]
l.advance(src, end)
return
}
@@ -70,8 +81,11 @@ func lex(src string) []*Node {
i := l.pos
rest := src[i:]
switch {
case isSpace(src[i]):
case isSpace(src[i]) || i == 0 && strings.HasPrefix(src, byteOrderMark):
j := i
if i == 0 && strings.HasPrefix(src, byteOrderMark) {
j = len(byteOrderMark)
}
for j < len(src) && isSpace(src[j]) {
j++
}
@@ -91,16 +105,12 @@ func lex(src string) []*Node {
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 src[i] == '#' && atLineStart(src, i) && 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 := len(src)
if loc := breakRE.FindStringIndex(src[valueStart:]); loc != nil {
end = valueStart + loc[0]
}
for end > valueStart && isSpace(src[end-1]) {