summaryrefslogtreecommitdiff
path: root/sources/scala/tools/scaladoc/Tag.java
blob: 4e710310017b839f658f203824168b485f093ba7 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.scaladoc;

import java.util.regex.*;

import ch.epfl.lamp.util.Pair;

import scalac.ast.Tree;
import scalac.symtab.Symbol;

import scaladoc.*;

/**
 * Documentation tag.
 */
public class Tag {

    /**
     * Symbol whose comment contains the tag.
     */
    public final Symbol holder;

    /**
     * Name of the tag.
     */
    public final String name;

    /**
     * Value of the tag.
     */
    public final String text;

    /**
     * Constructor
     *
     * @param name
     * @param text
     */
    public Tag(Symbol holder, String name, String text) {
        this.holder = holder;
        this.name = name;
        this.text = text;
    }

    /** String representation.
     */
    public String toString() {
	return "Tag(" + name + ", " + text + ")";
    }

    /**
     * Is this tag a param tag ?
     */
    public boolean isParam() {
	return name.equals("@param");
    }

    /**
     * Is this tag an exception tag ?
     */
    public boolean isException() {
	return name.equals("@param") || name.equals("@exception");
    }

    /**
     * Is this tag a reference tag ?
     */
    public boolean isReference() {
	return name.equals("@see") || name.equals("@link");
    }

    /**
     * Is this tag a text tag ?
     */
    public boolean isText() {
	return name.equals("@text");
    }

    /**
     * Splits a string containing spaces into two parts.
     *
     * @param text
     */
    public static Pair split(String text) {
	int length = text.length();
	int endFst = length;
	int startSnd = length;
	int current = 0;
	while (current < length && startSnd >= length) {
	    char c = text.charAt(current);
	    if (Character.isWhitespace(c) && endFst == length)
		endFst = current;
	    else if (!Character.isWhitespace(c) && endFst != length)
		startSnd = current;
	    current++;
	}
	return new Pair(text.substring(0, endFst),
			text.substring(startSnd, length));
    }

    /**
     * Kind of a reference tag.
     */
    public static class RefKind {

	/** Bad reference. */
	public case Bad(String ref);

	/** Reference to an URL. */
	public case Url(String ref);

	/** String literal reference. */
	public case Literal(String ref);

	/** Reference to a scala entity. */
	public case Scala(String container, String member, String label);
    }

    /**
     * Parses a reference tag.
     *
     * @param tag
     */
    protected static RefKind parseReference(Tag tag) {
	if (tag.text.startsWith("\""))
	    return RefKind.Literal(tag.text);
	else if (tag.text.startsWith("<"))
	    return RefKind.Url(tag.text);
	else {
	    Pattern p = Pattern.compile("(([^#\\s]*)(#([^\\s]+))?)\\s*(.+)?");
	    Matcher m = p.matcher(tag.text);
	    if (m.find()) {
		String container = m.group(2) == null ? "" : m.group(2);
		String member = m.group(3);
		String label =  m.group(4) == null ? "" : m.group(4);
		return RefKind.Scala(container, member, label);
	    } else {
		System.err.println("Warning: Fail to parse see tag:" + tag + " in " + tag.holder);
		return RefKind.Bad(tag.text);
	    }
	}
    }

}