Rewrite the code comments for consistent tone and accuracy
Comments only: the tokens of every Go file are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+53
-29
@@ -2,10 +2,11 @@ package sfz
|
||||
|
||||
import "strings"
|
||||
|
||||
// Opcodes are the opcodes set at one point of an instrument, by name.
|
||||
// Opcodes are the opcodes set at one point of an instrument, by name, with
|
||||
// their values as written and any variables in them replaced.
|
||||
type Opcodes map[string]string
|
||||
|
||||
// with returns the opcodes of o with those of more on top.
|
||||
// with returns a copy of the opcodes of o, with those of more set over them.
|
||||
func (o Opcodes) with(more Opcodes) Opcodes {
|
||||
out := make(Opcodes, len(o)+len(more))
|
||||
for k, v := range o {
|
||||
@@ -19,14 +20,16 @@ func (o Opcodes) with(more Opcodes) Opcodes {
|
||||
|
||||
// FlatRegion is a <region> with the opcodes it inherits.
|
||||
type FlatRegion struct {
|
||||
// Opcodes are the region's own and those of the headers above it, and the
|
||||
// <control> settings that apply to it.
|
||||
// Opcodes are the region's own opcodes, those of the headers above it, and
|
||||
// the <control> settings that apply to it. They are Inherited with Own set
|
||||
// over them.
|
||||
Opcodes Opcodes
|
||||
// File and Line are where the <region> header is.
|
||||
// File is the path of the file that has the <region> header, as in
|
||||
// [File.Path], and Line is the line the header is on.
|
||||
File string
|
||||
Line int
|
||||
// Own holds the opcodes of the region's own header, and Inherited those that
|
||||
// it inherits, which Opcodes is the two of together.
|
||||
// Own are the opcodes that follow the region's header, and Inherited are
|
||||
// those that the region takes from the headers above it.
|
||||
Own, Inherited Opcodes
|
||||
// Header is the <region> header, or nil if a variable made it, so that it
|
||||
// has no node of its own.
|
||||
@@ -34,21 +37,25 @@ type FlatRegion struct {
|
||||
}
|
||||
|
||||
// Flat is an instrument with its directives expanded and its headers resolved.
|
||||
// It has no comments and no layout.
|
||||
type Flat struct {
|
||||
// Control holds the opcodes of the <control> headers.
|
||||
// Control holds the opcodes of the <control> headers, the later setting of
|
||||
// an opcode replacing the earlier.
|
||||
Control Opcodes
|
||||
// Curves holds the opcodes of each <curve> header.
|
||||
// Curves holds the opcodes of each <curve> header, in order.
|
||||
Curves []Opcodes
|
||||
// Regions are the regions, in order.
|
||||
// Regions are the <region> headers, with what they inherit, in order.
|
||||
Regions []FlatRegion
|
||||
// Headers counts the headers that aren't <control>, <curve>, <global>,
|
||||
// <master>, <group> or <region>, by name.
|
||||
// Headers counts, by lowercase name, the headers that aren't <control>,
|
||||
// <curve>, <global>, <master>, <group> or <region>.
|
||||
Headers map[string]int
|
||||
}
|
||||
|
||||
// level is how far down the header hierarchy a header is.
|
||||
// level is how far down the header hierarchy a header is, where the opcodes of
|
||||
// each level are set over those of the levels before it.
|
||||
type level int
|
||||
|
||||
// The levels of the headers that regions inherit from.
|
||||
const (
|
||||
levelGlobal level = iota
|
||||
levelMaster
|
||||
@@ -65,33 +72,38 @@ var headerLevels = map[string]level{
|
||||
}
|
||||
|
||||
// controlSettings are the <control> opcodes that apply to the regions after
|
||||
// them.
|
||||
// them, as opposed to those that name controllers or set their values.
|
||||
var controlSettings = []string{"default_path", "note_offset", "octave_offset"}
|
||||
|
||||
// target is where the opcodes after a header go.
|
||||
type target struct {
|
||||
// ops are the opcodes set so far.
|
||||
ops Opcodes
|
||||
// region is whether the header is a <region>, which becomes a region when
|
||||
// the next header starts or the text ends, and line is where it is.
|
||||
// the next header starts or the text ends.
|
||||
region bool
|
||||
// line, file and header are where the <region> header is, and its node, if a
|
||||
// variable didn't make it.
|
||||
line int
|
||||
file string
|
||||
header *Header
|
||||
}
|
||||
|
||||
// flattener reads a document's nodes in order.
|
||||
// flattener is the [visitor] that builds a [Flat] from a document's nodes.
|
||||
type flattener struct {
|
||||
flat *Flat
|
||||
// levels are the opcodes set at each level.
|
||||
// levels are the opcodes set at each level by the headers read so far.
|
||||
levels [levelRegion + 1]Opcodes
|
||||
into *target
|
||||
// into is where the opcodes that come next go, or nil before any header.
|
||||
into *target
|
||||
}
|
||||
|
||||
// Flatten returns the instrument with each #include read where it stands,
|
||||
// each variable replaced by its value as of where it is used, and each
|
||||
// region given the opcodes of the <global>, <master> and <group> above it. A
|
||||
// new header clears the ones below it, so a <group> starts with no group
|
||||
// opcodes. Comments are dropped. An include that wasn't read is skipped.
|
||||
// Flatten returns the instrument with each #include read where it stands, each
|
||||
// variable replaced by its value as of where it is used, and each region given
|
||||
// the opcodes of the <global>, <master> and <group> above it. A header clears
|
||||
// the opcodes of the levels below it, so a <group> starts with no group
|
||||
// opcodes of its own. Comments are dropped, and an include that wasn't read is
|
||||
// skipped.
|
||||
func (d *Document) Flatten() *Flat {
|
||||
f := &flattener{flat: &Flat{Control: Opcodes{}, Headers: map[string]int{}}}
|
||||
for i := range f.levels {
|
||||
@@ -102,14 +114,22 @@ func (d *Document) Flatten() *Flat {
|
||||
return f.flat
|
||||
}
|
||||
|
||||
func (f *flattener) define(*File, *Node) {}
|
||||
func (f *flattener) include(*File, *Node) {}
|
||||
func (f *flattener) variables(*File, *Node) {}
|
||||
// define does nothing, as [walk] sets variables itself.
|
||||
func (f *flattener) define(*File, *Node) {}
|
||||
|
||||
// include does nothing, as [walk] reads included files itself.
|
||||
func (f *flattener) include(*File, *Node) {}
|
||||
|
||||
// variables does nothing, as [walk] replaces variables itself.
|
||||
func (f *flattener) variables(*File, *Node) {}
|
||||
|
||||
// text does nothing, as a flattened instrument leaves out text that isn't SFZ.
|
||||
func (f *flattener) text(*File, *Node, string) {}
|
||||
|
||||
// header starts the header, which is on the line of n even if a variable made
|
||||
// it.
|
||||
func (f *flattener) header(file *File, n *Node, name string, variable bool) {
|
||||
// A header that a variable made has no node of its own, so it takes the line
|
||||
// of the node that made it.
|
||||
// A header that a variable made has no node of its own to edit.
|
||||
line, _ := file.Position(n)
|
||||
var own *Header
|
||||
if !variable {
|
||||
@@ -118,10 +138,11 @@ func (f *flattener) header(file *File, n *Node, name string, variable bool) {
|
||||
f.startHeader(name, line, file, own)
|
||||
}
|
||||
|
||||
// opcode sets the opcode in the header being read.
|
||||
func (f *flattener) opcode(_ *File, _ *Node, name, value string, _ bool) { f.setOpcode(name, value) }
|
||||
|
||||
// closeRegion adds the region being read, if any. A region takes the <control>
|
||||
// settings made before it, which later <control> headers don't change.
|
||||
// settings made before it, and later <control> headers don't change them.
|
||||
func (f *flattener) closeRegion() {
|
||||
if f.into == nil || !f.into.region {
|
||||
return
|
||||
@@ -139,6 +160,8 @@ func (f *flattener) closeRegion() {
|
||||
})
|
||||
}
|
||||
|
||||
// startHeader ends the header being read and starts the one called header,
|
||||
// which is on a line of file, and is own if it has a node of its own.
|
||||
func (f *flattener) startHeader(header string, line int, file *File, own *Header) {
|
||||
f.closeRegion()
|
||||
name := strings.ToLower(header)
|
||||
@@ -169,6 +192,7 @@ func (f *flattener) startHeader(header string, line int, file *File, own *Header
|
||||
}
|
||||
}
|
||||
|
||||
// setOpcode sets an opcode in the header being read.
|
||||
func (f *flattener) setOpcode(name, value string) {
|
||||
if f.into == nil {
|
||||
// Opcodes before any header apply to every region.
|
||||
|
||||
Reference in New Issue
Block a user