Commit 2c2e2c5d authored by Russ Cox's avatar Russ Cox

more comment work.

got rid of regexps.
primary bug fix is that // inside /* */ do not get stripped anymore,
so that the text inside

/*
int a;
// int b;
int c;
*/

is

int a;
// int b;
int c;

before, the "int b;" line was being uncommented too.

R=gri
DELTA=65  (13 added, 42 deleted, 10 changed)
OCL=35334
CL=35404
parent dc093494
...@@ -9,36 +9,12 @@ package doc ...@@ -9,36 +9,12 @@ package doc
import ( import (
"go/ast"; "go/ast";
"io"; "io";
"once";
"regexp";
"strings"; "strings";
"template"; // for htmlEscape "template"; // for htmlEscape
) )
// Comment extraction // 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]*$");
}
// CommentText returns the text of comment, // CommentText returns the text of comment,
// with the comment markers - //, /*, and */ - removed. // with the comment markers - //, /*, and */ - removed.
func CommentText(comment *ast.CommentGroup) string { func CommentText(comment *ast.CommentGroup) string {
...@@ -50,39 +26,34 @@ func CommentText(comment *ast.CommentGroup) string { ...@@ -50,39 +26,34 @@ func CommentText(comment *ast.CommentGroup) string {
comments[i] = string(c.Text); comments[i] = string(c.Text);
} }
once.Do(setupRegexps);
lines := make([]string, 0, 20); lines := make([]string, 0, 20);
for _, c := range comments { for _, c := range comments {
// split on newlines // Remove comment markers.
cl := strings.Split(c, "\n", 0); // The parser has given us exactly the comment text.
switch n := len(c); {
// walk lines, stripping comment markers case n >= 4 && c[0:2] == "/*" && c[n-2:n] == "*/":
w := 0; c = c[2:n-2];
for _, l := range cl { case n >= 2 && c[0:2] == "//":
// remove /* and */ lines c = c[2:n];
if comment_junk.MatchString(l) { // Remove leading space after //, if there is one.
continue; if len(c) > 0 && c[0] == ' ' {
c = c[1:len(c)];
} }
}
// strip trailing white space // Split on newlines.
m := trailing_whitespace.ExecuteString(l); cl := strings.Split(c, "\n", 0);
if len(m) > 0 {
l = l[0 : m[1]];
}
// strip leading comment markers // Walk lines, stripping trailing white space and adding to list.
m = comment_markers.ExecuteString(l); for _, l := range cl {
if len(m) > 0 { // Strip trailing white space
l = l[m[1] : len(l)]; m := len(l);
for m > 0 && (l[m-1] == ' ' || l[m-1] == '\n' || l[m-1] == '\t' || l[m-1] == '\r') {
m--;
} }
l = l[0 : m];
cl[w] = l; // Add to list.
w++;
}
cl = cl[0:w];
// Add this comment to total list.
for _, l := range cl {
n := len(lines); n := len(lines);
if n+1 >= cap(lines) { if n+1 >= cap(lines) {
newlines := make([]string, n, 2*cap(lines)); newlines := make([]string, n, 2*cap(lines));
......
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