summaryrefslogtreecommitdiff
path: root/sources/scala/tools/scaladoc/Comment.java
blob: fc76d8438e491ee718e15f5aa6626d48999ee744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.scaladoc;

import java.util.*;
import java.util.regex.*;
import ch.epfl.lamp.util.Pair;
import scalac.symtab.Symbol;

/**
 * Class <code>Comment</code> contains all information in comment part.
 * It allows users to get first sentence of this comment, get comment
 * for different tags...
 */
public class Comment {

    /**
     * Holder of the comment.
     */
    public final Symbol holder;

    /**
     * Raw text of the comment.
     */
    public final String rawText;

    /**
     * Text minus any tags.
     */
    public String text;

    /**
     * Tags minus text.
     */
    public Tag[] tags;

    /**
     * Constructor.
     *
     * @param holder
     * @param rawText
     */
    public Comment(Symbol holder, String rawText) {
	this.holder = holder;
	this.rawText = cleanComment(rawText);
	parseComment();
    }

    /**
     * Returns true if this comment is empty.
     */
    public boolean isEmpty() {
	return "".equals(rawText);
    }

    /**
     * Removes the leading white spaces and asterixes.
     *
     * @param comment
     */
    public static String cleanComment(String s) {
	if (s == null)
	    return "";
        s = s.substring(3, s.length() - 2);
        StringBuffer buf = new StringBuffer();
        String regexp = "^([ \\t]*)([\\*]*)(.*)$";
        Pattern p = Pattern.compile(regexp,
                                    Pattern.MULTILINE);
        Matcher m = p.matcher(s);

        while (m.find()) {
            if (m.group(2).length() == 0)
                buf.append(m.group(1) +
                           m.group(2) +
                           m.group(3));
            else
                buf.append(m.group(3));
            buf.append("\n");
        }
        return buf.toString();
    }

    /**
     * Parses the comment string separating the description
     * text from tags.
     */
    protected void parseComment() {
        String regexp = "\n[ ]*@|\\A[ ]*@";
        String[] parts = rawText.split(regexp);
	if (parts.length == 0) {
	    text = "";
	    tags = new Tag[0];
	}
	else {
	    int startTag;
	    if (parts[0].startsWith("@")) {
		text = "";
		startTag = 0;
	    } else {
		text = parts[0];
		startTag = 1;
	    }
	    List tagList = new LinkedList();
	    for(int i = startTag; i < parts.length; i++) {
		Pair fields = Tag.split(parts[i]);
		String name = (String) fields.fst;
		String description = (String) fields.snd;
		tagList.add(new Tag(holder, "@" + name, description));
	    }
	    tags = (Tag[]) tagList.toArray(new Tag[tagList.size()]);
	}
    }

    /**
     * Returns an array of tags with text and inline.
     *
     * @param holder
     * @param s
     * @see          See Tags for a Doc comment.
     */
    public static Tag[] makeTags(Symbol holder, String s) {
	final List tagList = new LinkedList();
	Pattern p = Pattern.compile("\\{@([^\\}]*)\\}");
	Matcher m = p.matcher(s);

	int start = 0;
	while (m.find()) {
	    String txt = s.substring(start, m.start());
	    if (!txt.equals(""))
		tagList.add(new Tag(holder, "@text", txt));
	    Pair fields = Tag.split(m.group(1));
	    String name = (String) fields.fst;
	    String description = (String) fields.snd;
	    tagList.add(new Tag(holder, "@" + name, description));
	    start = m.end();
	}
	String txt = s.substring(start, s.length());
	if (!txt.equals(""))
	    tagList.add(new Tag(holder, "@text", txt));
	return (Tag[]) tagList.toArray(new Tag[tagList.size()]);
    }

    /**
     * Returns the first sentence of this comment.
     */
    public String firstSentence() {
	Pattern p = Pattern.compile("\\.(\\s)");
	Matcher m = p.matcher(text);
	if (m.find()) {
	    return text.substring(0, m.start(1));;
	} else
	    return text;
    }

}