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:
2026-12-01 19:08:00 +00:00
co-authored by Claude Sonnet 5.5
parent 41f752e1c1
commit 158661eefe
27 changed files with 716 additions and 437 deletions
+37 -29
View File
@@ -9,7 +9,7 @@ import (
"strings"
)
// LineEnding is the line break a written document uses.
// LineEnding is the line break that a written document uses.
type LineEnding string
// The line endings.
@@ -23,30 +23,32 @@ const (
)
// WriteOptions change how a document is written. The zero value writes each
// file as it was read.
// file as it was read, with the line breaks it had.
type WriteOptions struct {
// InlineIncludes replaces each #include that was read with the text of its
// file, so the main file stands alone.
InlineIncludes bool
// ExpandVariables replaces each variable with its value, as of where it is
// used, and leaves out the #define directives. Written to several files,
// a file that is included more than once, and so would be expanded
// differently each time, is an error.
// used, and leaves out the #define directives. A file that is included more
// than once, with its variables set differently each time, can't be written
// as a file of its own, as it would need to be written differently each
// time, and [Document.WriteFiles] returns an error for it.
ExpandVariables bool
// LineEnding sets the line break of the text written.
// LineEnding is the line break of the text written. The zero value is
// [KeepLineEndings].
LineEnding LineEnding
}
// FileWriter writes a file of a document. The path is that of the file, with
// forward slashes, in the folder of the main file.
// FileWriter writes the data of a file of a document. The path is that of the
// file, with forward slashes, in the folder of the path given to
// [Document.WriteFiles].
type FileWriter func(path string, data []byte) error
// Write writes the document as one file to w. A document that has includes
// that were read can be written as one file only with InlineIncludes, or else
// the files they hold would be dropped; Write returns [ErrInvalid] then. With
// ExpandVariables, the included files would also need variables that the main
// file no longer defines, so it needs InlineIncludes as well. Use
// [Document.WriteFiles] to write the files separately.
// Write writes the document as one file to w. A document with includes that
// were read can be written as one file only with InlineIncludes, as the files
// they hold would be lost otherwise, and Write returns an error wrapping
// [ErrInvalid] for it. Use [Document.WriteFiles] to write the files
// separately, with ExpandVariables or not.
func (d *Document) Write(w io.Writer, opts WriteOptions) error {
if d.hasIncludes() && !opts.InlineIncludes {
return fmt.Errorf("%w: the document has includes, which need InlineIncludes, or WriteFiles", ErrInvalid)
@@ -56,7 +58,7 @@ func (d *Document) Write(w io.Writer, opts WriteOptions) error {
return err
}
// hasIncludes reports whether the document read any include.
// hasIncludes reports whether the main file has an include that was read.
func (d *Document) hasIncludes() bool {
for _, n := range d.Root.Nodes {
if n.Kind == KindInclude && n.File != nil {
@@ -66,10 +68,11 @@ func (d *Document) hasIncludes() bool {
return false
}
// WriteFiles writes the main file with the path root, and each file it
// includes at its own path, in the folder of root, using fw. It writes nothing
// and returns [ErrInvalid] if an include's path is absolute or leaves the
// folder.
// WriteFiles writes the main file with the path root, and each file it includes
// at its own path in the folder of root, using fw. It writes nothing, and
// returns an error wrapping [ErrInvalid], for an include whose path is absolute
// or leaves the folder, and for a file that is included more than once and
// would be written differently each time.
func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) error {
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}, collect: true}
text := r.file(d.Root)
@@ -98,18 +101,21 @@ func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) err
return nil
}
// renderer writes files as the options say.
// renderer writes the files of a document as the options say.
type renderer struct {
opts WriteOptions
opts WriteOptions
// defines are the variables defined so far, when variables are expanded.
defines defines
// collect is whether the included files that stand apart are kept in
// outputs, as WriteFiles writes them.
collect bool
// outputs are the included files that stand apart, by path.
outputs map[string]string
err error
// err is the first error found.
err error
}
// lineEndings returns text with the line breaks that the options give it.
func (r *renderer) lineEndings(text string) string {
if r.opts.LineEnding == KeepLineEndings {
return text
@@ -118,14 +124,15 @@ func (r *renderer) lineEndings(text string) string {
return strings.ReplaceAll(text, "\n", string(r.opts.LineEnding))
}
// file returns the text of a file as it is written.
// file returns the text of a file as it is written, with the files it includes
// written into it or kept for [Document.WriteFiles], as the options say.
func (r *renderer) file(f *File) string {
var b strings.Builder
// dropBreak is whether the next line break is cut, as the directive it
// followed is.
// dropBreak is whether the next line break is cut, as the directive that it
// ends is cut, or its text has been written in its place.
dropBreak := false
// gap is whether an inlined file ended without whitespace, so that what
// follows needs some.
// follows needs some to stay apart from it.
gap := false
for _, n := range f.Nodes {
raw := n.Raw
@@ -148,7 +155,8 @@ func (r *renderer) file(f *File) string {
b.WriteByte(' ')
}
b.WriteString(inner)
// The include's own line break would leave an empty line.
// The include's own line break would leave an empty line after a file
// that ends with one.
dropBreak = strings.HasSuffix(inner, "\n")
gap = inner != "" && !isSpace(inner[len(inner)-1])
continue
@@ -177,8 +185,8 @@ func (r *renderer) file(f *File) string {
return b.String()
}
// output keeps the text of an included file, and reports a file that comes out
// differently each time it is included.
// output keeps the text of an included file, and sets err if the file has
// already been kept with other text.
func (r *renderer) output(n *Node, text string) {
if prev, ok := r.outputs[n.Resolved]; ok && prev != text && r.err == nil {
r.err = fmt.Errorf("%w: %s is included more than once and is written differently each time", ErrInvalid, n.Resolved)