summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/doc/DocGenerator.scala
blob: d1d719497a4e4d99c8b8e9ea90a29d12dc5a785e (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package scala.tools.nsc.doc;

import scala.tools.nsc._;
import java.io.File;
import scala.tools.nsc.models._;
import scala.collection.immutable._;
import scala.xml._;

abstract class DocGenerator extends Models {
  import global._;

  def dquote(str : String) = Text("\"" + str + "\"");
  val header = <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
               <meta name="generator" content="scaladoc (1.4.0.4)"/>
               <link rel="stylesheet" type="text/css" href="style.css"/>
               <script type="text/javascript" src="script.js"></script>;


  def page = <HTML></HTML>;


  val emptyMap = ListMap.Empty[Kind,TreeSet[HasTree]];
  def process(units : Iterator[CompilationUnit], outdir : String) : Unit = {
    val outdir0 = new File(outdir);
    if (!outdir0.exists()) outdir0.mkdir();
    var members = emptyMap;
    var topLevel = ListMap.Empty[ModuleSymbol,ListMap[Kind,TreeSet[HasTree]]];
    for (val unit <- units) {
      val sourceMod = new SourceMod(unit);
      for (val mmbr <- sourceMod.members) mmbr.tree match {
      case cdef:  ImplDef =>
        assert(cdef.symbol.owner != NoSymbol);
        val sym = cdef.symbol.owner.asInstanceOf[ModuleSymbol];
        if (!topLevel.contains(sym)) topLevel = topLevel.update(sym, emptyMap);
        topLevel = topLevel.update(sym, organize0(mmbr, topLevel(sym)));
      case _ => throw new Error("unknown: " + mmbr.tree + " " + mmbr.tree.getClass());
      }
    }
    var packages = new TreeMap[String,ModuleSymbol];
    for (val top <- topLevel.elements)
      packages = packages.insert(top._1.fullNameString, top._1);


    val packageFrame = <HTML>
      <HEAD><TITLE>List of modules</TITLE>
            {header}
      </HEAD>
      <body>
      <div class="doctitle-larger">
        Scala<br/>2.0
      </div>
      <a href="module-page.html" target="classesFrame">All objects and classes</a><p/>
      <b>Modules</b>
      <table class="list">
      <tr>
      <td style="white-space:nowrap;">
      { {
       for (val top <- packages.elements)
         yield urlFor0(top._2, "classesFrame");
      } }
      </td></tr></table></body></HTML>;


    System.out.println("PACKAGE_FRAME");
    System.out.println(packageFrame);


    // output HTML for each package.
    for (val top <- topLevel.elements) {
      val sym = top._1;
      val members = top._2;
      val index = {process(members, sym)};


      System.out.println("SAVE-TO: " + sym);
      System.out.println(index);
    }
  }

  def nameFor0(sym : Symbol) : NodeSeq = Text(sym.fullNameString);
  def urlFor0(sym : Symbol, target : String) : NodeSeq =
		<a href={urlFor1(sym)} target={("\"" + target + "\"")}>{nameFor0(sym)}</a><br/>;

  def urlFor2(sym : Symbol) : String = {
    if (sym == sym.toplevelClass) "";
    else {
      val rest = urlFor2(sym.owner);
      val rest0 = if (rest.equals("")) rest else rest + ".";
      rest0 + sym.nameString;
    }
  }

  def urlFor1(sym : Symbol) : String = sym match {
    case _ : ModuleSymbol =>
      ("\"" + sym.fullNameString('/') + "/module-page.html\"");
    case csym : ClassSymbol =>
      if (csym == csym.toplevelClass)
         "\"" + csym.fullNameString('/') + "-class-page.html\"";
      else    urlFor1(sym.toplevelClass) + "#" + urlFor2(sym);
    case _ => urlFor1(sym.toplevelClass) + "#" + urlFor2(sym);
  }





  def process(members : ListMap[Kind,TreeSet[HasTree]], sym : Symbol) : NodeSeq = {
    for (val kind <- KINDS; members.contains(kind)) yield process(kind, members(kind), sym);



    urlFor0(sym, "classesFrame");

  };

  def organize(c : Composite, map0 : ListMap[Kind,TreeSet[HasTree]]) = {
    var map = map0;
    for (val mmbr <- c.members.toList) map = organize0(mmbr, map);
    map;
  }
  def organize0(mmbr : HasTree, map0 : ListMap[Kind,TreeSet[HasTree]]) = {
    var map = map0;
    if (!map.contains(mmbr.kind))
      map = map.update(mmbr.kind, new TreeSet[HasTree]);
    map = map.update(mmbr.kind, map(mmbr.kind) + mmbr);
    map;
  }


  def process(kind : Kind, set : TreeSet[HasTree], sym : Symbol) : Node = {
    val ret =
    <table cellpadding="3" class="member">
    <tr>
      <td colspan="2" class="title">
      { { labelFor(kind) + " Summary"; } }
      </td>
    </tr>
    { { for (val mmbr <- set.toList) yield process(mmbr, sym); } }
    </table>;
    ret;
  }
  def stringsFor(mods : Modifiers) = {
    var modString : List[String] = Nil;
    if (mods . isPrivate  ) modString = "private"   :: modString;
    if (mods . isProtected) modString = "protected" :: modString;
    if (mods . isOverride ) modString = "override"  :: modString;
    if (mods . isAbstract ) modString = "abstract"  :: modString;
    if (mods . isCase     ) modString = "case"      :: modString;
    if (mods . isSealed   ) modString = "sealed"    :: modString;
    if (mods . isFinal    ) modString = "final"    :: modString;
    if (mods . isMixin    ) modString = "mixin"    :: modString;
    modString;
  }


  def targetFor(ht : HasTree) : String = {
    // compute a URL.
    "Foo";
  }
  def forSymbol(symbol : Symbol, tpe : Type) : NodeSeq =
    Text(symbol.nameString);


  def ifT (cond : Boolean, nodes : NodeSeq) = if (cond) nodes else NodeSeq.Empty;
  def ifT (tree : Tree, nodes : NodeSeq, before : Boolean) =
    if (tree != EmptyTree) {
      if (before) nodes.concat(forTree(tree));
      else forTree(tree).concat(nodes);
    } else NodeSeq.Empty;

  def forTree(tree : Tree) : NodeSeq = tree match {
    case vdef : ValDef =>
      Text(vdef.symbol.name.toString()).concat(Text(" : ")).concat(forTree(vdef.tpt));
    case id  : Ident  => forSymbol(id.symbol, id.tpe);
    case sel : Select => forTree(sel.qualifier).concat(forSymbol(sel.symbol, sel.tpe));
    case tree : AbsTypeDef =>
      ifT(tree.lo, Text(" <: "), true).
        concat(forSymbol(tree.symbol, tree.tpe)).concat(ifT(tree.hi, Text(" <: "), false));
    case EmptyTree => NodeSeq.Empty;
    case _ => Text("XX=" + tree.toString());
  }
  def forTrees(trees : List[Tree]) : NodeSeq = {
    if (trees.isEmpty) NodeSeq.Empty;
    else {
      val head = forTree(trees.head);
      head.concat(if (trees.tail.isEmpty) NodeSeq.Empty else Text(", ")).concat(forTrees(trees.tail));
    }
  }

  def surround(open : String, close : String, node : NodeSeq) : NodeSeq =
    Text(open).concat(node).concat(Text(close));

  def typesFor(ht : HasTree) : NodeSeq = {
    val tparams = ht.tree match {
      case cdef : ClassDef => cdef.tparams;
      case ddef : DefDef   => ddef.tparams;
      case adef : AliasTypeDef => adef.tparams;
      case _ => Nil;
    }
    if (tparams.isEmpty) Text("");
    else surround("[", "]", forTrees(tparams));
  }
  def  argsFor(ht : HasTree) : NodeSeq = ht.tree match {
    case ddef : DefDef =>
      val nodes = for (val vparams <- ddef.vparamss)
        yield surround("(", ")", forTrees(vparams));
      nodes.flatMap(x => x.asList);
    case _ => NodeSeq.Empty;
  }
  def urlFor(ht : HasTree) : String =
    "\"unknown\"";

  def  nameFor(ht : HasTree) : NodeSeq =
    <A href={ urlFor(ht) } target="_self"> { forSymbol(ht.tree.symbol, null) } </A>;

  def resultFor(ht : HasTree) : NodeSeq = ht.tree match {
    case vdef : ValOrDefDef =>
      Text(" : ").concat(forTree(vdef.tpt));
    case cdef : ImplDef =>
      if (cdef.impl.parents.isEmpty) NodeSeq.Empty;
      else Text("extends ").concat(forTrees(cdef.impl.parents));
    case _ => NodeSeq.Empty;
  }

  def comment(comment : String, isShort : Boolean) : NodeSeq = {
    var ret : List[Node] = Nil;
    if (comment != null) {
      // strip out any stars.
      var comment0 = comment.trim();
      assert(comment0.startsWith(JDOC_START));
      comment0 = comment0.substring(JDOC_START.length());
      assert(comment0.endsWith(JDOC_END));
      comment0 = comment0.substring(0, comment0.length() - JDOC_END.length());
      var idx = 0;
      while (idx != -1) {
        idx = comment0.indexOf('*', idx);
        if (idx != -1)
          comment0 = comment0.substring(0, idx) +
            comment0.substring(idx + 1, comment0.length());
      }
      val tokenizer = new java.util.StringTokenizer(comment0, "@");
      val body = tokenizer.nextToken();

      var attributes : List[String] = Nil;
      if (!isShort) while (tokenizer.hasMoreElements())
        attributes = attributes ::: (tokenizer.nextToken() :: Nil);
      val node = <BR> { Text(comment) } </BR>;
      val nodes = { { for (val a <- attributes) yield <BR> { Text(a) } </BR>; } };
      val nodes0 : NodeSeq = node :: nodes;
      nodes0;
    } else NodeSeq.Empty;
  };


  def process(ht : HasTree, sym : Symbol) : scala.xml.Node = {
    val comment0 = if (comments.contains(ht.tree.symbol))
      comments(ht.tree.symbol) else null;


    val index =
    <tr>
      <td valign="top" class="modifiers">
        &nbsp;
        <code>{ {
          (for (val mod <- stringsFor(ht.mods)) yield Text(mod + " "));
        } }</code>
      </td>
      <td class="signature">
        <code>
        { labelFor(ht.kind).toLowerCase() }
        { nameFor(ht) }
        { typesFor(ht) }
        {  argsFor(ht) }
        {resultFor(ht) }
        </code>
        { comment(comment0, true) }
      </td>
    </tr>;

    val body = <X></X>;






    index;
  }


/*
  val index0 = <HTML>
  <HEAD><TITLE>API Documentation</TITLE></HEAD>
  <FRAMESET cols="20%,80%" title="" onLoad="top.loadFrames()">
  <FRAMESET rows="30%,70%" title="" onLoad="top.loadFrames()">
  <FRAME src="overview-frame.html" name="packageListFrame" title="All Packages">
  <FRAME src="allclasses-frame.html" name="packageFrame" title="All classes and interfaces (except non-static nested types)">
  </FRAMESET>
  <FRAME src="overview-summary.html" name="classFrame" title="Package, class and interface descriptions" scrolling="yes">
  </FRAMESET>
  </HTML>;
*/

  def index = {
    val index0 = <HTML></HTML>;
  }

  private val JDOC_START = "/**";
  private val JDOC_END   = "*/";
}