Add editing, writing and building documents

Header.Get, Set and Delete edit opcodes in place and keep the text around
them. Write and WriteFiles take WriteOptions to inline includes, expand
variables and set line endings. Build makes a document from a typed
Instrument, with options to split regions into included files and to
hoist repeated values into #define directives.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 17:41:34 +00:00
co-authored by Claude Sonnet 5.5
parent d124690ecf
commit 3d2ab78a32
7 changed files with 1154 additions and 1 deletions
+8 -1
View File
@@ -25,13 +25,20 @@ The library knows every opcode and header in the format, from the data behind [s
The registry is generated: replace `internal/gen/syntax.yml` and run `go generate ./...`, then `go test -update`.
## Editing and writing
- `Document.Headers` returns the headers of every file. `Header.Get`, `Set` and `Delete` change an opcode and keep the comments and spacing around it; a new opcode goes after the header's last and is laid out like it. `Document.AddHeader` adds a header.
- `Header.Set` on an opcode that uses a variable writes the value in its place. `Document.SetDefine` changes the variable, and so each opcode that uses it.
- `Document.Write` and `WriteFiles` take `WriteOptions`: `InlineIncludes` makes one file, `ExpandVariables` replaces variables with their values, and `LineEnding` sets the line break. The zero value writes each file as it was read.
- `Build` makes a document from an `Instrument`, with `BuildOptions`: `SplitFiles` puts regions in included files, `Variables` hoists repeated values into `#define`s and the samples' folder into `default_path`, and `Indent` sets the indent. Reading the result gives the instrument back, except for opcodes the typed model doesn't hold.
## Includes
`Parse` reads includes with the resolver from `WithResolver`; `ParseFile` reads them from the folder of the main file. An include that can't be read is a `Problem` from `Validate`, and `WithIncludes(sfz.IncludesFail)` makes it an error. Includes nest at most 16 deep, and an instrument may hold 16 MB of text; `WithLimits` changes both.
## Status
The document, flattening, typed model, opcode registry and validation are in. Not yet in: editing a document, and building one from the typed model.
The document, flattening, typed model, opcode registry, validation, editing, writing and building are in.
## Licence