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:
@@ -6,9 +6,9 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Header is a header of a file, with the opcodes that follow it up to the
|
||||
// next header in the same file. Opcodes that an #include after it brings in
|
||||
// belong to the included file's own headers.
|
||||
// Header is a header of a file, with the opcodes that follow it in the same
|
||||
// file up to the next header. The nodes of a file that an #include in that
|
||||
// range reads are the nodes of that file, and not of the header.
|
||||
type Header struct {
|
||||
// File is the file the header is in.
|
||||
File *File
|
||||
@@ -16,12 +16,12 @@ type Header struct {
|
||||
Node *Node
|
||||
}
|
||||
|
||||
// Name returns the header's name without brackets, as written.
|
||||
// Name returns the header's name without its brackets, as written.
|
||||
func (h *Header) Name() string { return h.Node.Name }
|
||||
|
||||
// indexOf returns where a node is in the file, or -1. A node remembers where it
|
||||
// was, which stays close to where it is after edits, so the search starts
|
||||
// there.
|
||||
// indexOf returns where a node is in the file's nodes, or -1 if it isn't. A
|
||||
// node remembers where it was, and edits move it only a short way from there,
|
||||
// so the search starts there and goes outwards.
|
||||
func (f *File) indexOf(n *Node) int {
|
||||
at := func(i int) bool { return i >= 0 && i < len(f.Nodes) && f.Nodes[i] == n }
|
||||
for d := 0; ; d++ {
|
||||
@@ -39,8 +39,8 @@ func (f *File) indexOf(n *Node) int {
|
||||
}
|
||||
}
|
||||
|
||||
// span returns the range of the file's nodes after the header's own, up to the
|
||||
// next header.
|
||||
// span returns the range of the file's nodes that follow the header's own, up
|
||||
// to the next header or the end of the file.
|
||||
func (h *Header) span() (start, end int) {
|
||||
start = h.File.indexOf(h.Node) + 1
|
||||
end = start
|
||||
@@ -62,8 +62,8 @@ func (h *Header) Opcodes() []*Node {
|
||||
return out
|
||||
}
|
||||
|
||||
// Get returns the header's last node for an opcode, which is the one that
|
||||
// takes effect.
|
||||
// Get returns the header's last node for an opcode, which is the one that takes
|
||||
// effect, and whether it has one.
|
||||
func (h *Header) Get(name string) (*Node, bool) {
|
||||
start, end := h.span()
|
||||
for i := end - 1; i >= start; i-- {
|
||||
@@ -74,9 +74,9 @@ func (h *Header) Get(name string) (*Node, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// checkOpcode returns an error if name=value wouldn't be read back as one
|
||||
// opcode with that value, as when the value holds a line break, a comment, or
|
||||
// another opcode.
|
||||
// checkOpcode returns an error wrapping [ErrInvalid] if name=value wouldn't be
|
||||
// read back as one opcode with that value, as when the value holds a line
|
||||
// break, a comment or another opcode.
|
||||
func checkOpcode(name, value string) error {
|
||||
text := name + "=" + value
|
||||
nodes := lex(text)
|
||||
@@ -87,8 +87,8 @@ func checkOpcode(name, value string) error {
|
||||
return fmt.Errorf("%w: %s=%q can't be written as one opcode", ErrInvalid, name, value)
|
||||
}
|
||||
|
||||
// checkDefine returns an error if the text of a #define of name with value
|
||||
// wouldn't be read back as that directive.
|
||||
// checkDefine returns an error wrapping [ErrInvalid] if a #define of the
|
||||
// variable name with the value wouldn't be read back as that directive.
|
||||
func checkDefine(name, value string) error {
|
||||
nodes := lex("#define " + name + " " + value)
|
||||
if len(nodes) > 0 && nodes[0].Kind == KindDefine && nodes[0].Name == name && nodes[0].Value == strings.TrimSpace(value) && onlySpace(nodes[1:]) {
|
||||
@@ -107,11 +107,12 @@ func onlySpace(nodes []*Node) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Set gives the opcode a value, or returns an error wrapping [ErrInvalid], and
|
||||
// changes nothing, if the value can't be written as the value of one opcode. An
|
||||
// opcode the header already has keeps its place and the text around it, and
|
||||
// takes the value as written even if it was a variable. A new one goes after the
|
||||
// header's last opcode, and its comment, laid out as that opcode is.
|
||||
// Set gives the opcode a value. It returns an error wrapping [ErrInvalid], and
|
||||
// changes nothing, if the name and value can't be written as one opcode. An
|
||||
// opcode that the header has keeps its place and the text around it, and takes
|
||||
// the value as written, even if the value was a variable. A new opcode goes
|
||||
// after the header's last, and after any comment on that opcode's line, laid
|
||||
// out as that opcode is.
|
||||
func (h *Header) Set(name, value string) error {
|
||||
value = strings.TrimSpace(value)
|
||||
if err := checkOpcode(name, value); err != nil {
|
||||
@@ -134,8 +135,8 @@ func (h *Header) Set(name, value string) error {
|
||||
at := start
|
||||
var sep string
|
||||
if last < 0 {
|
||||
// With no opcode to follow, the new one goes after the header, on its own
|
||||
// line if the header is.
|
||||
// With no opcode to follow, the new one goes after the header, on a line
|
||||
// of its own if the header has one.
|
||||
sep = " "
|
||||
if start < len(h.File.Nodes) && h.File.Nodes[start].Kind == KindSpace && strings.Contains(h.File.Nodes[start].Raw, "\n") {
|
||||
sep = h.File.Nodes[start].Raw
|
||||
@@ -150,8 +151,9 @@ func (h *Header) Set(name, value string) error {
|
||||
} else if h.File.hasLineBreak() {
|
||||
sep = "\n"
|
||||
}
|
||||
// The new opcode goes after what ends the last one's line. A line comment
|
||||
// runs to the end of the line, so the opcode then needs a line of its own.
|
||||
// The new opcode goes after the comments that end the last one's line. A
|
||||
// line comment runs to the end of its line, so the opcode then goes on a
|
||||
// line of its own. A block comment that never ends can't be passed.
|
||||
for {
|
||||
k := at
|
||||
for k < end && h.File.Nodes[k].Kind == KindSpace && !strings.Contains(h.File.Nodes[k].Raw, "\n") {
|
||||
@@ -169,7 +171,7 @@ func (h *Header) Set(name, value string) error {
|
||||
inserted := append([]*Node{{Kind: KindSpace, Raw: sep}}, nodes...)
|
||||
// A value runs to the end of its line unless a header, opcode, comment or
|
||||
// directive ends it, so text after the new opcode on its line would become
|
||||
// part of the value. It goes on a line of its own.
|
||||
// part of the value. A line break after the opcode keeps it out.
|
||||
k := at
|
||||
for k < len(h.File.Nodes) && h.File.Nodes[k].Kind == KindSpace && !strings.Contains(h.File.Nodes[k].Raw, "\n") {
|
||||
k++
|
||||
@@ -181,13 +183,13 @@ func (h *Header) Set(name, value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// unterminated reports whether a node is a block comment that never ends, which
|
||||
// unterminated reports whether n is a block comment that never ends, and so
|
||||
// takes in all that follows it.
|
||||
func unterminated(n *Node) bool {
|
||||
return n.Kind == KindComment && strings.HasPrefix(n.Raw, "/*") && (len(n.Raw) < 4 || !strings.HasSuffix(n.Raw, "*/"))
|
||||
}
|
||||
|
||||
// hasLineBreak reports whether the file has a line break.
|
||||
// hasLineBreak reports whether the file has a line break anywhere.
|
||||
func (f *File) hasLineBreak() bool {
|
||||
for _, n := range f.Nodes {
|
||||
if n.Kind == KindSpace && strings.Contains(n.Raw, "\n") {
|
||||
@@ -197,8 +199,9 @@ func (f *File) hasLineBreak() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Delete removes the opcode, every time the header has it, with the space
|
||||
// before it.
|
||||
// Delete removes the opcode, each time the header has it, with the space that
|
||||
// separated it from what came before, and leaves the line breaks around other
|
||||
// text as they are.
|
||||
func (h *Header) Delete(name string) {
|
||||
for {
|
||||
n, ok := h.Get(name)
|
||||
@@ -214,9 +217,9 @@ func (h *Header) Delete(name string) {
|
||||
case isSpace(i-1, false):
|
||||
from = i - 1
|
||||
case isSpace(i-1, true):
|
||||
// The line break before the opcode goes with it only if what follows
|
||||
// ends the line, as the opcode's own line then goes. Otherwise the
|
||||
// text after it stays on the line, without the space that separated it.
|
||||
// The line break before the opcode goes with it only if the opcode ends
|
||||
// its line, so that its whole line goes. Otherwise the text after it
|
||||
// keeps its line, and the space that separated it from the opcode goes.
|
||||
if i+1 >= len(h.File.Nodes) || isSpace(i+1, true) || h.File.Nodes[i+1].Kind == KindHeader {
|
||||
from = i - 1
|
||||
} else if isSpace(i+1, false) {
|
||||
@@ -228,7 +231,7 @@ func (h *Header) Delete(name string) {
|
||||
}
|
||||
}
|
||||
|
||||
// insert adds nodes at an index.
|
||||
// insert adds nodes to the file at the index.
|
||||
func (f *File) insert(at int, nodes ...*Node) {
|
||||
f.Nodes = slices.Insert(f.Nodes, at, nodes...)
|
||||
for i, n := range nodes {
|
||||
@@ -237,9 +240,9 @@ func (f *File) insert(at int, nodes ...*Node) {
|
||||
f.dirty = true
|
||||
}
|
||||
|
||||
// Position returns where a node of the file starts, with the line and the
|
||||
// column, in bytes, from 1. Edits move what follows them, which is worked out
|
||||
// when this is next called.
|
||||
// Position returns the line and the column, both from 1, where a node of the
|
||||
// file starts. The column is in bytes. Edits move the nodes after them, which
|
||||
// Position works out when it is next called, and not at each edit.
|
||||
func (f *File) Position(n *Node) (line, col int) {
|
||||
if f.dirty {
|
||||
f.reposition()
|
||||
@@ -247,8 +250,8 @@ func (f *File) Position(n *Node) (line, col int) {
|
||||
return n.line, n.col
|
||||
}
|
||||
|
||||
// reposition sets the line, column and index of each node from the text
|
||||
// before it.
|
||||
// reposition sets the line, column and index of each node from the text before
|
||||
// it.
|
||||
func (f *File) reposition() {
|
||||
line, col := 1, 1
|
||||
for i, n := range f.Nodes {
|
||||
@@ -266,7 +269,7 @@ func (f *File) reposition() {
|
||||
}
|
||||
|
||||
// Headers returns the headers of every file of the document, in the order the
|
||||
// files are read.
|
||||
// files are read, as [Document.Files] returns them.
|
||||
func (d *Document) Headers() []*Header {
|
||||
var out []*Header
|
||||
for _, f := range d.Files() {
|
||||
@@ -279,10 +282,10 @@ func (d *Document) Headers() []*Header {
|
||||
return out
|
||||
}
|
||||
|
||||
// AddHeader adds an empty header on a line of its own after another header and
|
||||
// its opcodes, or at the end of the main file if after is nil, and returns it.
|
||||
// It returns an error wrapping [ErrInvalid] if the name isn't one a header can
|
||||
// have.
|
||||
// AddHeader adds an empty header, on a line of its own, after the header after
|
||||
// and its opcodes, or at the end of the main file if after is nil, and returns
|
||||
// it. It returns an error wrapping [ErrInvalid] if name isn't one that a header
|
||||
// can have, or if the header would fall inside a block comment that never ends.
|
||||
func (d *Document) AddHeader(name string, after *Header) (*Header, error) {
|
||||
if m := headerRE.FindString("<" + name + ">"); m != "<"+name+">" {
|
||||
return nil, fmt.Errorf("%w: <%s> isn't a header", ErrInvalid, name)
|
||||
@@ -294,7 +297,8 @@ func (d *Document) AddHeader(name string, after *Header) (*Header, error) {
|
||||
_, at = after.span()
|
||||
}
|
||||
if at > 0 {
|
||||
// A header after a block comment that never ends would be inside it.
|
||||
// A header after a block comment that never ends would be inside the
|
||||
// comment.
|
||||
if c := f.Nodes[at-1]; unterminated(c) {
|
||||
return nil, fmt.Errorf("%w: a comment that doesn't end is where <%s> would go", ErrInvalid, name)
|
||||
}
|
||||
@@ -315,6 +319,7 @@ func (d *Document) Defines() []*Node { return d.nodes(KindDefine) }
|
||||
// Includes returns the #include nodes of every file, in order.
|
||||
func (d *Document) Includes() []*Node { return d.nodes(KindInclude) }
|
||||
|
||||
// nodes returns the nodes of a kind in every file, in order.
|
||||
func (d *Document) nodes(kind NodeKind) []*Node {
|
||||
var out []*Node
|
||||
for _, f := range d.Files() {
|
||||
@@ -327,12 +332,12 @@ func (d *Document) nodes(kind NodeKind) []*Node {
|
||||
return out
|
||||
}
|
||||
|
||||
// SetDefine gives a variable a new value, by changing its first #define, so
|
||||
// that each opcode that uses it changes with it. Setting an opcode that uses
|
||||
// the variable with [Header.Set] replaces the variable there instead. A
|
||||
// SetDefine gives a variable a new value by changing its first #define, so
|
||||
// that each opcode that uses the variable changes with it. [Header.Set] on an
|
||||
// opcode that uses the variable replaces the variable in that opcode instead. A
|
||||
// variable with no #define gets one at the start of the main file. The name
|
||||
// includes its $. It returns an error wrapping [ErrInvalid], and changes
|
||||
// nothing, if the name or value can't be written as a #define.
|
||||
// includes its $. SetDefine returns an error wrapping [ErrInvalid], and changes
|
||||
// nothing, if the name and value can't be written as a #define.
|
||||
func (d *Document) SetDefine(name, value string) error {
|
||||
value = strings.TrimSpace(value)
|
||||
if err := checkDefine(name, value); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user