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:
@@ -9,36 +9,40 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Resolver returns the contents of an included file. The path is relative to
|
||||
// the folder of the main file, with forward slashes.
|
||||
// Resolver returns the contents of an included file. The path it is given is
|
||||
// relative to the folder of the main file, with forward slashes. It may hold ..
|
||||
// elements or be absolute, as the text of an instrument decides, so a Resolver
|
||||
// that reads from a disk needs to keep to the instrument's folder.
|
||||
type Resolver func(path string) ([]byte, error)
|
||||
|
||||
// IncludeMode is what [Parse] does with an #include.
|
||||
// IncludeMode is what [Parse] does with each #include.
|
||||
type IncludeMode int
|
||||
|
||||
// The include modes.
|
||||
const (
|
||||
// IncludesResolve reads each include. One that can't be read is reported
|
||||
// as a [Problem] and left unresolved.
|
||||
// IncludesResolve reads each include. An include that can't be read is
|
||||
// reported as a [Problem] and left unresolved.
|
||||
IncludesResolve IncludeMode = iota
|
||||
// IncludesKeep reads no includes, and works out their paths only.
|
||||
// IncludesKeep reads no includes, and only works out their paths.
|
||||
IncludesKeep
|
||||
// IncludesFail reads each include, and returns [ErrUnresolved] for one
|
||||
// that can't be read.
|
||||
// IncludesFail reads each include, and makes [Parse] return
|
||||
// [ErrUnresolved] for one that can't be read.
|
||||
IncludesFail
|
||||
)
|
||||
|
||||
// Limits bound what an instrument may make Parse do.
|
||||
// Limits bound the work that the text of an instrument can make [Parse] do.
|
||||
type Limits struct {
|
||||
// MaxDepth is how deeply #include may nest. The main file is at depth 0.
|
||||
MaxDepth int
|
||||
// MaxBytes is how many bytes all the files together may hold.
|
||||
// MaxBytes is how many bytes all the files together may hold, counting
|
||||
// what their variables expand to as well.
|
||||
MaxBytes int
|
||||
}
|
||||
|
||||
// DefaultLimits are the limits [Parse] uses unless [WithLimits] says otherwise.
|
||||
// DefaultLimits are the limits [Parse] uses unless [WithLimits] sets others.
|
||||
var DefaultLimits = Limits{MaxDepth: 16, MaxBytes: 16 << 20}
|
||||
|
||||
// parseConfig is what the [ParseOption]s set.
|
||||
type parseConfig struct {
|
||||
resolver Resolver
|
||||
includes IncludeMode
|
||||
@@ -48,7 +52,8 @@ type parseConfig struct {
|
||||
// ParseOption changes how [Parse] works.
|
||||
type ParseOption func(*parseConfig)
|
||||
|
||||
// WithResolver sets how includes are read. Without one, includes aren't read.
|
||||
// WithResolver sets how includes are read. Without a resolver, includes aren't
|
||||
// read.
|
||||
func WithResolver(r Resolver) ParseOption {
|
||||
return func(c *parseConfig) { c.resolver = r }
|
||||
}
|
||||
@@ -59,7 +64,7 @@ func WithIncludes(m IncludeMode) ParseOption {
|
||||
return func(c *parseConfig) { c.includes = m }
|
||||
}
|
||||
|
||||
// WithLimits sets the limits. A field that is 0 uses its default.
|
||||
// WithLimits sets the limits. A field that is 0 keeps its default.
|
||||
func WithLimits(l Limits) ParseOption {
|
||||
return func(c *parseConfig) {
|
||||
if l.MaxDepth > 0 {
|
||||
@@ -71,7 +76,11 @@ func WithLimits(l Limits) ParseOption {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse reads an instrument from r, and its includes with the resolver.
|
||||
// Parse reads an instrument from r, and its includes with the resolver. It
|
||||
// returns an error wrapping [ErrInvalid] for an instrument that is past the
|
||||
// limits or that includes itself, and one wrapping [ErrUnresolved] for an
|
||||
// include that can't be read if [IncludesFail] is set. Any other text can be
|
||||
// read, and what in it isn't SFZ is for [Document.Validate] to report.
|
||||
func Parse(r io.Reader, opts ...ParseOption) (*Document, error) {
|
||||
p := newParser(opts)
|
||||
data, err := p.read(r)
|
||||
@@ -82,8 +91,8 @@ func Parse(r io.Reader, opts ...ParseOption) (*Document, error) {
|
||||
}
|
||||
|
||||
// ParseFile reads the instrument whose main file is name in fsys, and its
|
||||
// includes from the folder of the main file. [WithResolver] overrides how
|
||||
// includes are read.
|
||||
// includes from the folder of the main file, which fsys keeps them to.
|
||||
// [WithResolver] overrides how includes are read.
|
||||
func ParseFile(fsys fs.FS, name string, opts ...ParseOption) (*Document, error) {
|
||||
dir := path.Dir(name)
|
||||
read := func(p string) ([]byte, error) { return fs.ReadFile(fsys, path.Join(dir, p)) }
|
||||
@@ -100,10 +109,11 @@ func ParseFile(fsys fs.FS, name string, opts ...ParseOption) (*Document, error)
|
||||
return p.document(path.Base(name), data)
|
||||
}
|
||||
|
||||
// Includes returns the files that an SFZ file includes directly, with the
|
||||
// variables it defines itself substituted, in the order they first appear. The
|
||||
// paths are relative to the folder of the main file. Includes that an
|
||||
// included file makes aren't followed.
|
||||
// Includes returns the files that an SFZ file includes directly, in the order
|
||||
// they first appear, each once. A variable that the file defines itself is
|
||||
// replaced by its value. The paths are relative to the folder of the main file.
|
||||
// Includes that an included file makes aren't followed. It returns nil if text
|
||||
// is past the limits.
|
||||
func Includes(text []byte) []string {
|
||||
doc, err := newParser([]ParseOption{WithIncludes(IncludesKeep)}).document("", text)
|
||||
if err != nil {
|
||||
@@ -126,10 +136,14 @@ type parser struct {
|
||||
defines defines
|
||||
// including holds the files being read, to refuse an include loop.
|
||||
including []string
|
||||
bytes int
|
||||
problems []Problem
|
||||
// bytes is how many bytes the files read so far, and what their variables
|
||||
// expand to, come to.
|
||||
bytes int
|
||||
// problems are the problems found so far.
|
||||
problems []Problem
|
||||
}
|
||||
|
||||
// newParser returns a parser with the options set.
|
||||
func newParser(opts []ParseOption) *parser {
|
||||
p := &parser{parseConfig: parseConfig{limits: DefaultLimits}, defines: defines{}}
|
||||
for _, o := range opts {
|
||||
@@ -138,8 +152,8 @@ func newParser(opts []ParseOption) *parser {
|
||||
return p
|
||||
}
|
||||
|
||||
// read returns the text of the main file, and refuses one that is longer than
|
||||
// the limit without reading the rest of it.
|
||||
// read returns the text of the main file. It refuses one that is longer than the
|
||||
// limit without reading the rest of it.
|
||||
func (p *parser) read(r io.Reader) ([]byte, error) {
|
||||
data, err := io.ReadAll(io.LimitReader(r, int64(p.limits.MaxBytes)+1))
|
||||
if err != nil {
|
||||
@@ -151,13 +165,14 @@ func (p *parser) read(r io.Reader) ([]byte, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// tooLong returns the error for an instrument that is past the limit on bytes.
|
||||
func (p *parser) tooLong() error {
|
||||
return fmt.Errorf("%w: the instrument, with what its variables expand to, is longer than %d bytes", ErrInvalid, p.limits.MaxBytes)
|
||||
}
|
||||
|
||||
// expand replaces the variables in text, counting the result against the
|
||||
// limit on bytes, so that variables that hold variables can't make text
|
||||
// that grows faster than the text that makes it.
|
||||
// expand replaces the variables in text and counts the result against the
|
||||
// limit on bytes. The count stops variables that hold variables from making
|
||||
// text that grows much faster than the text that makes it.
|
||||
func (p *parser) expand(text string) (string, error) {
|
||||
out, ok := expand(p.defines, text, p.limits.MaxBytes-p.bytes)
|
||||
if !ok {
|
||||
@@ -176,7 +191,9 @@ func (p *parser) document(name string, data []byte) (*Document, error) {
|
||||
return &Document{Root: root, problems: p.problems}, nil
|
||||
}
|
||||
|
||||
// file lexes a file and reads the directives in it, in order.
|
||||
// file lexes a file, and reads the directives in it in order: each #define sets
|
||||
// a variable and each #include reads the file it names, at depth levels of
|
||||
// include below the main file.
|
||||
func (p *parser) file(name string, data []byte, depth int) (*File, error) {
|
||||
if depth > p.limits.MaxDepth {
|
||||
return nil, fmt.Errorf("%w: includes nest more than %d deep at %s", ErrInvalid, p.limits.MaxDepth, name)
|
||||
@@ -203,7 +220,8 @@ func (p *parser) file(name string, data []byte, depth int) (*File, error) {
|
||||
}
|
||||
p.defines.set(n.Name, value)
|
||||
case KindOpcode, KindText:
|
||||
// Flattening and writing expand these again, so what they make counts.
|
||||
// Flattening and writing expand these again, so what they make counts
|
||||
// against the limit now.
|
||||
if strings.Contains(n.Raw, "$") {
|
||||
if _, err := p.expand(n.Raw); err != nil {
|
||||
return nil, err
|
||||
@@ -238,25 +256,26 @@ func (p *parser) file(name string, data []byte, depth int) (*File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// variableRE finds a $ and the run of name characters after it. Only the start
|
||||
// of the run may be the name of a variable (see substitute).
|
||||
// variableRE finds a $ and the run of name characters after it. The name of a
|
||||
// variable is the start of the run (see substitute).
|
||||
var variableRE = regexp.MustCompile(`\$[A-Za-z0-9_]+`)
|
||||
|
||||
// maxVariableName is the most bytes a variable's name may have, with its $.
|
||||
// maxVariableName is the most bytes that the name of a variable may have, with
|
||||
// its $. It keeps the search for the end of a name short.
|
||||
const maxVariableName = 129
|
||||
|
||||
// substitute replaces the variables defined so far in text. A variable's name
|
||||
// ends at the shortest run of characters that is a defined name, so in
|
||||
// $MIC_$ART the underscore is text between $MIC and $ART, as in ARIA. A
|
||||
// variable that isn't defined is left as it is.
|
||||
// variable that isn't defined stays as it is.
|
||||
func substitute(defines map[string]string, text string) string {
|
||||
out, _ := expand(defines, text, -1)
|
||||
return out
|
||||
}
|
||||
|
||||
// expand is substitute with a limit on the length of the result, which it
|
||||
// reports as false if text would make more than that. A negative limit is
|
||||
// none.
|
||||
// expand is substitute with a limit on the length of the result. It reports
|
||||
// false, and returns nothing, if text would make more than that. A negative
|
||||
// limit is no limit.
|
||||
func expand(defines map[string]string, text string, limit int) (string, bool) {
|
||||
if !strings.Contains(text, "$") {
|
||||
return text, limit < 0 || len(text) <= limit
|
||||
|
||||
Reference in New Issue
Block a user