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:
@@ -22,33 +22,39 @@ var (
|
||||
breakRE = regexp.MustCompile(`<[A-Za-z_]+>|\s[A-Za-z0-9_$]+=|//|/\*|\s#(?:define|include)\b|\n`)
|
||||
)
|
||||
|
||||
// byteOrderMark is the UTF-8 mark some editors put at the start of a file. It
|
||||
// is read as space.
|
||||
// byteOrderMark is the UTF-8 mark that some editors put at the start of a file.
|
||||
// The lexer reads it as space.
|
||||
const byteOrderMark = "\ufeff"
|
||||
|
||||
// atLineStart reports whether a directive may start at offset i: at the start
|
||||
// of the text, after the byte order mark, or after whitespace.
|
||||
// atLineStart reports whether a directive may start at offset i of src, which
|
||||
// is so at the start of the text, after the byte order mark, and after
|
||||
// whitespace.
|
||||
func atLineStart(src string, i int) bool {
|
||||
return i == 0 || isSpace(src[i-1]) || i == len(byteOrderMark) && strings.HasPrefix(src, byteOrderMark)
|
||||
}
|
||||
|
||||
// isSpace reports whether c is whitespace.
|
||||
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' }
|
||||
|
||||
// isNameChar reports whether c may be in the name of an opcode or a variable.
|
||||
func isNameChar(c byte) bool {
|
||||
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_' || c == '$'
|
||||
}
|
||||
|
||||
// lexer cuts text into nodes and keeps count of the position.
|
||||
type lexer struct {
|
||||
// nodes are the nodes cut so far.
|
||||
nodes []*Node
|
||||
line int
|
||||
// line is the line the next node starts on, from 1.
|
||||
line int
|
||||
// lineStart is the offset where the current line starts.
|
||||
lineStart int
|
||||
// pos is the offset the next node starts at.
|
||||
pos int
|
||||
}
|
||||
|
||||
// add appends a node for the text from the current position to end.
|
||||
// add gives n the text from the current position to end, and appends it, or
|
||||
// extends the node before it if both are text.
|
||||
func (l *lexer) add(src string, end int, n *Node) {
|
||||
n.Raw = src[l.pos:end]
|
||||
n.line, n.col = l.line, l.pos-l.lineStart+1
|
||||
@@ -75,7 +81,9 @@ func (l *lexer) advance(src string, end int) {
|
||||
l.pos = end
|
||||
}
|
||||
|
||||
// lex cuts text into nodes whose raw text, joined, is the text.
|
||||
// lex cuts text into nodes whose raw text, joined, is the text. Text that
|
||||
// isn't a space, comment, header, opcode or directive is kept as text nodes,
|
||||
// so that any text can be lexed.
|
||||
func lex(src string) []*Node {
|
||||
l := &lexer{line: 1}
|
||||
for l.pos < len(src) {
|
||||
@@ -83,6 +91,7 @@ func lex(src string) []*Node {
|
||||
rest := src[i:]
|
||||
switch {
|
||||
case isSpace(src[i]) || i == 0 && strings.HasPrefix(src, byteOrderMark):
|
||||
// A byte order mark and the whitespace after it make one space node.
|
||||
j := i
|
||||
if i == 0 && strings.HasPrefix(src, byteOrderMark) {
|
||||
j = len(byteOrderMark)
|
||||
@@ -107,7 +116,9 @@ func lex(src string) []*Node {
|
||||
m := headerRE.FindStringSubmatch(rest)
|
||||
l.add(src, i+len(m[0]), &Node{Kind: KindHeader, Name: m[1]})
|
||||
case src[i] == '#' && atLineStart(src, i) && l.directive(src):
|
||||
// The directive is added by the call that tests for it.
|
||||
case opcodeRE.MatchString(rest):
|
||||
// A value runs to the next break, or to the end of the text.
|
||||
m := opcodeRE.FindStringSubmatch(rest)
|
||||
valueStart := i + len(m[0])
|
||||
end := len(src)
|
||||
@@ -135,8 +146,8 @@ func lex(src string) []*Node {
|
||||
return l.nodes
|
||||
}
|
||||
|
||||
// directive adds a #define or #include at the current position, and reports
|
||||
// whether it was one that can be read.
|
||||
// directive adds the #define or #include at the current position, and reports
|
||||
// whether there was one that can be read. Otherwise it adds nothing.
|
||||
func (l *lexer) directive(src string) bool {
|
||||
rest := src[l.pos:]
|
||||
if m := defineRE.FindStringSubmatch(rest); m != nil {
|
||||
@@ -159,5 +170,5 @@ func (l *lexer) directive(src string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// commentRE finds the start of a comment.
|
||||
// commentRE finds the start of a comment, which ends the value of a #define.
|
||||
var commentRE = regexp.MustCompile(`//|/\*`)
|
||||
|
||||
Reference in New Issue
Block a user