Commit 99128fc7 authored by Russ Cox's avatar Russ Cox

move pretty/comment.go into go/doc.

extract comment text code out of go/doc/doc.go into comment.go.
no code changes, just rearrangement.

first step so i can write tests.

R=gri
DELTA=633  (318 added, 301 deleted, 14 changed)
OCL=29269
CL=29293
parent 9c6fd4c1
...@@ -40,16 +40,23 @@ coverage: packages ...@@ -40,16 +40,23 @@ coverage: packages
$(AS) $*.s $(AS) $*.s
O1=\ O1=\
comment.$O\
O2=\
doc.$O\ doc.$O\
phases: a1 phases: a1 a2
_obj$D/doc.a: phases _obj$D/doc.a: phases
a1: $(O1) a1: $(O1)
$(AR) grc _obj$D/doc.a doc.$O $(AR) grc _obj$D/doc.a comment.$O
rm -f $(O1) rm -f $(O1)
a2: $(O2)
$(AR) grc _obj$D/doc.a doc.$O
rm -f $(O2)
newpkg: clean newpkg: clean
mkdir -p _obj$D mkdir -p _obj$D
...@@ -57,6 +64,7 @@ newpkg: clean ...@@ -57,6 +64,7 @@ newpkg: clean
$(O1): newpkg $(O1): newpkg
$(O2): a1 $(O2): a1
$(O3): a2
nuke: clean nuke: clean
rm -f $(GOROOT)/pkg$D/doc.a rm -f $(GOROOT)/pkg$D/doc.a
......
...@@ -2,16 +2,111 @@ ...@@ -2,16 +2,111 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Godoc comment -> HTML formatting // Godoc comment extraction and comment -> HTML formatting.
package comment package doc
import ( import (
"fmt"; "fmt";
"io"; "io";
"template"; "once";
"regexp";
"strings";
"template"; // for htmlEscape
) )
// Comment extraction
var (
comment_markers *regexp.Regexp;
trailing_whitespace *regexp.Regexp;
comment_junk *regexp.Regexp;
)
func makeRex(s string) *regexp.Regexp {
re, err := regexp.Compile(s);
if err != nil {
panic("MakeRegexp ", s, " ", err.String());
}
return re;
}
// TODO(rsc): Cannot use var initialization for regexps,
// because Regexp constructor needs threads.
func setupRegexps() {
comment_markers = makeRex("^/(/|\\*) ?");
trailing_whitespace = makeRex("[ \t\r]+$");
comment_junk = makeRex("^[ \t]*(/\\*|\\*/)[ \t]*$");
}
// Aggregate comment text, without comment markers.
func commentText(comments []string) string {
once.Do(setupRegexps);
lines := make([]string, 0, 20);
for i, c := range comments {
// split on newlines
cl := strings.Split(c, "\n");
// walk lines, stripping comment markers
w := 0;
for j, l := range cl {
// remove /* and */ lines
if comment_junk.Match(l) {
continue;
}
// strip trailing white space
m := trailing_whitespace.Execute(l);
if len(m) > 0 {
l = l[0 : m[1]];
}
// strip leading comment markers
m = comment_markers.Execute(l);
if len(m) > 0 {
l = l[m[1] : len(l)];
}
// throw away leading blank lines
if w == 0 && l == "" {
continue;
}
cl[w] = l;
w++;
}
// throw away trailing blank lines
for w > 0 && cl[w-1] == "" {
w--;
}
cl = cl[0 : w];
// add this comment to total list
// TODO: maybe separate with a single blank line
// if there is already a comment and len(cl) > 0?
for j, l := range cl {
n := len(lines);
if n+1 >= cap(lines) {
newlines := make([]string, n, 2*cap(lines));
for k := range newlines {
newlines[k] = lines[k];
}
lines = newlines;
}
lines = lines[0 : n+1];
lines[n] = l;
}
}
// add final "" entry to get trailing newline.
// loop always leaves room for one more.
n := len(lines);
lines = lines[0 : n+1];
return strings.Join(lines, "\n");
}
// Split bytes into lines. // Split bytes into lines.
func split(text []byte) [][]byte { func split(text []byte) [][]byte {
// count lines // count lines
......
...@@ -8,9 +8,9 @@ import ( ...@@ -8,9 +8,9 @@ import (
"container/vector"; "container/vector";
"fmt"; "fmt";
"go/ast"; "go/ast";
"go/doc";
"go/token"; "go/token";
"io"; "io";
"once";
"regexp"; "regexp";
"sort"; "sort";
"strings"; "strings";
...@@ -203,99 +203,14 @@ func (doc *DocReader) AddProgram(prog *ast.Program) { ...@@ -203,99 +203,14 @@ func (doc *DocReader) AddProgram(prog *ast.Program) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Conversion to external representation // Conversion to external representation
func makeRex(s string) *regexp.Regexp { func astComment(comments ast.Comments) string {
re, err := regexp.Compile(s); text := make([]string, len(comments));
if err != nil {
panic("MakeRegexp ", s, " ", err.String());
}
return re;
}
var (
comment_markers *regexp.Regexp;
trailing_whitespace *regexp.Regexp;
comment_junk *regexp.Regexp;
)
// TODO(rsc): Cannot use var initialization for regexps,
// because Regexp constructor needs threads.
func setupRegexps() {
comment_markers = makeRex("^/(/|\\*) ?");
trailing_whitespace = makeRex("[ \t\r]+$");
comment_junk = makeRex("^[ \t]*(/\\*|\\*/)[ \t]*$");
}
// Aggregate comment text, without comment markers.
func comment(comments ast.Comments) string {
once.Do(setupRegexps);
lines := make([]string, 0, 20);
for i, c := range comments { for i, c := range comments {
// split on newlines text[i] = string(c.Text);
cl := strings.Split(string(c.Text), "\n");
// walk lines, stripping comment markers
w := 0;
for j, l := range cl {
// remove /* and */ lines
if comment_junk.Match(l) {
continue;
}
// strip trailing white space
m := trailing_whitespace.Execute(l);
if len(m) > 0 {
l = l[0 : m[1]];
}
// strip leading comment markers
m = comment_markers.Execute(l);
if len(m) > 0 {
l = l[m[1] : len(l)];
} }
return commentText(text);
// throw away leading blank lines
if w == 0 && l == "" {
continue;
}
cl[w] = l;
w++;
}
// throw away trailing blank lines
for w > 0 && cl[w-1] == "" {
w--;
}
cl = cl[0 : w];
// add this comment to total list
// TODO: maybe separate with a single blank line
// if there is already a comment and len(cl) > 0?
for j, l := range cl {
n := len(lines);
if n+1 >= cap(lines) {
newlines := make([]string, n, 2*cap(lines));
for k := range newlines {
newlines[k] = lines[k];
}
lines = newlines;
}
lines = lines[0 : n+1];
lines[n] = l;
}
}
// add final "" entry to get trailing newline.
// loop always leaves room for one more.
n := len(lines);
lines = lines[0 : n+1];
return strings.Join(lines, "\n");
} }
// ValueDoc is the documentation for a group of declared // ValueDoc is the documentation for a group of declared
// values, either vars or consts. // values, either vars or consts.
// //
...@@ -341,7 +256,7 @@ func makeValueDocs(v *vector.Vector) []*ValueDoc { ...@@ -341,7 +256,7 @@ func makeValueDocs(v *vector.Vector) []*ValueDoc {
d := make([]*ValueDoc, v.Len()); d := make([]*ValueDoc, v.Len());
for i := range d { for i := range d {
decl := v.At(i).(*ast.GenDecl); decl := v.At(i).(*ast.GenDecl);
d[i] = &ValueDoc{comment(decl.Doc), decl, i}; d[i] = &ValueDoc{astComment(decl.Doc), decl, i};
} }
sort.Sort(sortValueDoc(d)); sort.Sort(sortValueDoc(d));
return d; return d;
...@@ -369,7 +284,7 @@ func makeFuncDocs(m map[string] *ast.FuncDecl) []*FuncDoc { ...@@ -369,7 +284,7 @@ func makeFuncDocs(m map[string] *ast.FuncDecl) []*FuncDoc {
i := 0; i := 0;
for name, f := range m { for name, f := range m {
doc := new(FuncDoc); doc := new(FuncDoc);
doc.Doc = comment(f.Doc); doc.Doc = astComment(f.Doc);
if f.Recv != nil { if f.Recv != nil {
doc.Recv = f.Recv.Type; doc.Recv = f.Recv.Type;
} }
...@@ -418,7 +333,7 @@ func makeTypeDocs(m map[string] *typeDoc) []*TypeDoc { ...@@ -418,7 +333,7 @@ func makeTypeDocs(m map[string] *typeDoc) []*TypeDoc {
for name, old := range m { for name, old := range m {
typespec := old.decl.Specs[0].(*ast.TypeSpec); typespec := old.decl.Specs[0].(*ast.TypeSpec);
t := new(TypeDoc); t := new(TypeDoc);
t.Doc = comment(typespec.Doc); t.Doc = astComment(typespec.Doc);
t.Type = typespec; t.Type = typespec;
t.Factories = makeFuncDocs(old.factories); t.Factories = makeFuncDocs(old.factories);
t.Methods = makeFuncDocs(old.methods); t.Methods = makeFuncDocs(old.methods);
...@@ -451,7 +366,7 @@ func (doc *DocReader) Doc() *PackageDoc { ...@@ -451,7 +366,7 @@ func (doc *DocReader) Doc() *PackageDoc {
p := new(PackageDoc); p := new(PackageDoc);
p.PackageName = doc.name; p.PackageName = doc.name;
p.ImportPath = doc.path; p.ImportPath = doc.path;
p.Doc = comment(doc.doc); p.Doc = astComment(doc.doc);
p.Consts = makeValueDocs(doc.consts); p.Consts = makeValueDocs(doc.consts);
p.Vars = makeValueDocs(doc.vars); p.Vars = makeValueDocs(doc.vars);
p.Types = makeTypeDocs(doc.types); p.Types = makeTypeDocs(doc.types);
......
...@@ -30,7 +30,7 @@ install: pretty godoc untab ...@@ -30,7 +30,7 @@ install: pretty godoc untab
clean: clean:
rm -f pretty untab godoc *.6 *.a 6.out *~ rm -f pretty untab godoc *.6 *.a 6.out *~
godoc.6: astprinter.6 comment.6 godoc.6: astprinter.6
pretty.6: astprinter.6 format.6 pretty.6: astprinter.6 format.6
......
...@@ -47,7 +47,6 @@ import ( ...@@ -47,7 +47,6 @@ import (
"time"; "time";
"astprinter"; "astprinter";
"comment";
) )
...@@ -258,7 +257,7 @@ func htmlFmt(w io.Writer, x interface{}, format string) { ...@@ -258,7 +257,7 @@ func htmlFmt(w io.Writer, x interface{}, format string) {
// Template formatter for "html-comment" format. // Template formatter for "html-comment" format.
func htmlCommentFmt(w io.Writer, x interface{}, format string) { func htmlCommentFmt(w io.Writer, x interface{}, format string) {
comment.ToHtml(w, toText(x)); doc.ToHtml(w, toText(x));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment