summaryrefslogtreecommitdiff
path: root/sources/scalac/ast/printer/HTMLTreePrinter.java
blob: 8dbd34f2a0c4bcda92b89529f498513448365144 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
**                                                                      **
\*                                                                      */

// $Id$

package scalac.ast.printer;

import scalac.Unit;
import scalac.symtab.Symbol;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.Math;
import java.util.HashMap;

/**
 * HTML pretty printer for Scala abstract syntax trees.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class HTMLTreePrinter extends TextTreePrinter {
    protected int outSectionLevel = 1;
    protected boolean started = false;

    public HTMLTreePrinter(OutputStream stream) {
        super(stream);
    }

    public void begin() {
        assert !started;

        super.begin();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<html>");
        out.println("<head>");
        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
        out.println("<link rel=\"stylesheet\" href=\"scala.css\" type=\"text/css\">");
        out.println("<title>Scala tree</title>");
        out.println("</head>");
        out.println("<body>");

        started = true;
    }

    public void end() {
        assert started;

        out.println("</body>");
        out.println("</html>");
        super.end();

        started = false;
    }

    public void beginSection(int level, String title) {
        outSectionLevel = Math.min(level, 4);
        beginSection1(outSectionLevel, title);
    }

    protected void beginSection1(int level, String title) {
        if (level == 1)
            out.println("<hr/>");
        String tag = "h" + level;
        startTag(tag);
        print(Text.Simple(title));
        endTag(tag);
    }

    protected void startTag(String tag) {
        out.print('<'); out.print(tag); out.print('>');
    }

    protected void startTag(String tag, String attr1, String val1) {
        out.print('<');
        out.print(tag);
        out.print(' ');
        out.print(attr1);
        out.print("=\"");
        out.print(val1);
        out.print("\">");
    }

    protected void endTag(String tag) {
        out.print("</"); out.print(tag); out.print(">");
    }

    protected void startSpan(String cls) {
        startTag("span", "class", cls);
    }

    protected void endSpan() {
        endTag("span");
    }

    protected void printString(String str) {
        StringBuffer buf = null;
        int strLen = str.length();

        for (int i = 0; i < strLen; ++i) {
            String entity;
            char c = str.charAt(i);
            switch (c) {
            case '<': entity = "lt"; break;
            case '>': entity = "gt"; break;
            case '&': entity = "amp"; break;
            default:  entity = null; break;
            }
            if (entity != null) {
                out.print('&');
                out.print(entity);
                out.print(';');
            } else
                out.print(c);
        }
    }

    protected static HashMap/*<Symbol,Integer>*/ symAnchors = new HashMap();
    protected String symbolAnchor(Symbol sym, SymbolUsage usage) {
        Integer anchorId = (Integer)symAnchors.get(sym);
        if (anchorId == null) {
            anchorId = new Integer(symAnchors.size());
            symAnchors.put(sym, anchorId);
        }
        if (usage == SymbolUsage.Definition)
            return anchorId.toString();
        else
            return "#" + anchorId.toString();
    }

    protected void print(Text text) {
        switch (text) {
        case Keyword(String name):
            startSpan("kw");
            printString(name);
            endSpan();
            break;
        case Literal(String str):
            startSpan("lit");
            printString(str);
            endSpan();
            break;
        case Identifier(Symbol symbol, String name, SymbolUsage usage):
            boolean defined = (usage == SymbolUsage.Definition);
            if (defined) startSpan("idDef");
            if (symbol != null) {
                String attr = (defined ? "name" : "href");
                startTag("a", attr, symbolAnchor(symbol, usage));
            }
            printString(name);
            if (symbol != null)
                endTag("a");
            if (defined) endSpan();
            break;
        default:
            super.print(text);
        }
    }

    protected void printUnitHeader(Unit unit) {
        beginSection1(outSectionLevel + 1, unit.source.toString());
        startTag("pre");
    }

    protected void printUnitFooter(Unit unit) {
        endTag("pre");
    }
}