5// Read source files and RFC and errata files, and cross-link them.
7// todo: also cross-reference typescript and possibly other files. switch from go parser to just reading the source as text.
24 log.Println("usage: link ../*.go ../*/*.go")
44 dstrfc string // e.g. "5322" or "6376-eid4810"
45 comment string // e.g. "todo" or "todo spec"
48 // RFC-file to RFC-line to references to list of file+line (possibly RFCs).
49 rfcLineSources := map[string]map[int][]ref{}
51 // Source-file to source-line to references of RFCs.
52 sourceLineRFCs := map[string]map[int][]ref{}
54 re := regexp.MustCompile(`((../)*)rfc/([0-9]{4,5})(-eid([1-9][0-9]*))?(:([1-9][0-9]*))?`)
56 addRef := func(m map[string]map[int][]ref, rfc string, lineno int, r ref) {
59 lineRefs = map[int][]ref{}
62 lineRefs[lineno] = append(lineRefs[lineno], r)
65 // Parse all .go files on the cli, assumed to be relative to current dir.
66 fset := token.NewFileSet()
67 for _, arg := range args {
68 f, err := parser.ParseFile(fset, arg, nil, parser.ParseComments|parser.SkipObjectResolution)
70 log.Fatalf("parse file %q: %s", arg, err)
72 for _, cg := range f.Comments {
73 for _, c := range cg.List {
74 lines := strings.Split(c.Text, "\n")
75 for i, line := range lines {
76 matches := re.FindAllStringSubmatch(line, -1)
77 if len(matches) == 0 {
82 if strings.HasPrefix(line, "// todo") {
83 s, _, have := strings.Cut(strings.TrimPrefix(line, "// "), ":")
92 srclineno := fset.Position(c.Pos()).Line + i
93 dir := filepath.Dir(srcpath)
94 for _, m := range matches {
99 if eid != "" && lineStr != "" {
100 log.Fatalf("%s:%d: cannot reference both errata (eid %q) to specified line number", srcpath, srclineno, eid)
104 v, err := strconv.ParseInt(lineStr, 10, 32)
106 log.Fatalf("%s:%d: bad linenumber %q: %v", srcpath, srclineno, lineStr, err)
116 dstpath := filepath.Join(dir, pre+"rfc", rfc)
117 if _, err := os.Stat(dstpath); err != nil {
118 log.Fatalf("%s:%d: references %s: %v", srcpath, srclineno, dstpath, err)
120 r := ref{srcpath, srclineno, dstpath, dstlineno, true, rfc, comment}
121 addRef(sourceLineRFCs, r.srcpath, r.srclineno, r)
122 addRef(rfcLineSources, r.dstrfc, r.dstlineno, ref{r.dstrfc, r.dstlineno, r.srcpath, r.srclineno, false, "", comment})
129 files, err := os.ReadDir(".")
131 log.Fatalf("readdir: %v", err)
133 for _, de := range files {
136 iserrata := isErrata(name)
137 if !isrfc && !iserrata {
140 oldBuf, err := os.ReadFile(name)
142 log.Fatalf("readdir: %v", err)
144 old := string(oldBuf)
146 lineRefs := rfcLineSources[name]
147 lines := strings.Split(old, "\n")
148 if len(lines) > 0 && lines[len(lines)-1] == "" {
149 lines = lines[:len(lines)-1]
151 for i, line := range lines {
152 if !(iserrata && i > 0) && len(line) > 80 {
153 line = strings.TrimRight(line[:80], " ")
155 refs := lineRefs[i+1]
157 line = fmt.Sprintf("%-80s", line)
159 // Lookup source files for rfc:line, so we can cross-link the rfcs.
160 done := map[string]bool{}
161 for _, r := range refs {
162 for _, xr := range sourceLineRFCs[r.dstpath][r.dstlineno] {
163 sref := fmt.Sprintf(" %s:%d", xr.dstrfc, xr.dstlineno)
164 if xr.dstrfc == name && xr.dstlineno == i+1 || done[sref] {
172 // Add link from rfc to source code.
173 for _, r := range refs {
178 line += fmt.Sprintf(" %s%s:%d", comment, r.dstpath, r.dstlineno)
185 if !bytes.Equal(oldBuf, newBuf) {
186 if err := os.WriteFile(name, newBuf, 0660); err != nil {
187 log.Printf("writefile %q: %s", name, err)
194func isRFC(name string) bool {
195 if len(name) < 4 || len(name) > 5 {
198 for _, c := range name {
199 if c < '0' || c > '9' {
206func isErrata(name string) bool {
207 t := strings.Split(name, "-")
208 return len(t) == 2 && isRFC(t[0]) && strings.HasPrefix(t[1], "eid")