summaryrefslogtreecommitdiff
path: root/sources/scalac/ast/parser/Sourcefile.java
blob: 3ac0a9d4bd7d402994528c80d7287ea5c32d93e6 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
**                                                                      **
** $Id$
\*                                                                      */

package scalac.ast.parser;

import java.io.*;
import java.util.Hashtable;
import scalac.util.AbstractFile;
import scalac.util.Name;
import scalac.util.Position;


/** This class represents a single scala source file. It provides
 *  functionality to read the file and to output error messages on
 *  a given line and column. Error messages are logged to
 *  decouple the time where an error message is issued from the
 *  time where the error messages are displayed.
 *
 *  @author     Matthias Zenger
 *  @version    1.0
 */
public class Sourcefile {

    /** the id management
     */
    public static int numIds = 1;
    public static String[] files = new String[]{"console", null, null, null};
    public static Sourcefile[] sources = new Sourcefile[]{null, null, null, null};
    public int id;

    /** the filename
     */
    public String        filename;
    public String           shortname;
    public String           pathname;

    /** the encoding of the file
     */
    protected String        encoding;

    /** a log of all errors generated so far; used to avoid printing an
     *  error message more than once
     */
    protected Hashtable     recorded = new Hashtable();

    /** the buffer containing the file that is currently translated
     */
    protected byte[]        buf = null;

    /** the last error position
     */
    protected int    lastLine = 0;
    protected int    lastPos = 0;
    protected int    lineEnd = 0;
    protected int    newPos = 0;

    /** constants used for source parsing
     */
    final static byte       LF = 0xA;
    final static byte       FF = 0xC;
    final static byte       CR = 0xD;
    final static byte       SU = 0x1A;

    /** set col to NO_COLUMN, if the printLine method should not mark
     *  the column
     */
    final static int        NO_COLUMN = -1;

    /** number of lines and bytes (not used internally)
     */
    public int              lines;  // set externally
    public int              bytes;

    /** prompt after error?
     */
    public boolean prompt;

    /** constructors
     */
    public Sourcefile(String filename) throws IOException, FileNotFoundException {
        if (filename == null) {
            this.filename = "(sourcefile not available)";
            this.shortname = "?";
            this.pathname = "?";
            buf = new byte[]{SU};
        } else {
            File f = new File(filename);
            this.filename = filename;
            this.shortname = f.getName();
            this.pathname = f.getAbsoluteFile().getParentFile().
                              getCanonicalPath();
            fillBuffer(new FileInputStream(f));
        }
        if (numIds == files.length) {
            String[] newfiles = new String[numIds * 2];
            System.arraycopy(files, 0, newfiles, 0, numIds);
            files = newfiles;
            Sourcefile[] newsources = new Sourcefile[numIds * 2];
            System.arraycopy(sources, 0, newsources, 0, numIds);
            sources = newsources;
        }
        sources[numIds] = this;
        files[id = numIds++] = shortname;
    }

    public Sourcefile(AbstractFile abs) throws IOException, FileNotFoundException {
        if (filename == null) {
            this.filename = "(sourcefile not available)";
            this.shortname = "?";
            this.pathname = "?";
            buf = new byte[]{SU};
        } else {
            this.filename = abs.getPath();
            this.shortname = abs.getName();
            this.pathname = abs.getPath();
            fillBuffer(abs.getInputStream());
        }
        if (numIds == files.length) {
            String[] newfiles = new String[numIds * 2];
            System.arraycopy(files, 0, newfiles, 0, numIds);
            files = newfiles;
            Sourcefile[] newsources = new Sourcefile[numIds * 2];
            System.arraycopy(sources, 0, newsources, 0, numIds);
            sources = newsources;
        }
        sources[numIds] = this;
        files[id = numIds++] = shortname;
    }

    public Sourcefile(byte[] input) {
        if (input == null) {
            this.filename = "(sourcefile not available)";
            this.shortname = "?";
            this.pathname = "?";
            buf = new byte[]{SU};
        } else {
            this.filename = "console";
            this.shortname = "console";
            this.pathname = "console";
            buf = new byte[input.length + 2];
            System.arraycopy(input, 0, buf, 0, input.length);
            buf[input.length] = Scanner.LF;
            buf[input.length + 1] = SU;
        }
        sources[0] = this;
        id = 0;
    }

    /** fill the buffer using the InputStream
     */
    private void fillBuffer(InputStream in) throws IOException {
        try {
            buf = new byte[(bytes = in.available()) + 1];
            if (in.read(buf) != (buf.length - 1))
                throw new IOException();
            in.close();
            buf[buf.length - 1] = SU;
        } catch (IOException e) {
            throw new IOException("cannot read '" + filename + "'");
        }
    }

    /** return filename as a string
     */
    public String toString() {
        return filename;
    }

    /** return filename as a name
     */
    public Name getName() {
        return Name.fromString(filename);
    }

    /** return the shortname without the suffix
     */
    public String getShortnameWithoutSuffix() {
        int idx = shortname.lastIndexOf('.');
        if (idx < 0)
            return shortname;
        else
            return shortname.substring(0, idx);
    }

    /** return the source buffer of this file
     */
    public byte[] getBuffer() {
        return buf;
    }

    /** number of logged entries
     */
    public int logged() {
        return recorded.size();
    }

    /** is there already an entry at position 'pos'
     */
    public boolean isLogged(int pos) {
        return (recorded.get(new Integer(pos)) != null);
    }

    /** enter entry into log table
     */
    public void log(int pos, String message) {
        recorded.put(new Integer(pos), message);
    }

    /** set encoding of the file
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /** return true if there is an entry for this position,
     *  otherwise return false and enter message into log
     */
    public boolean testAndSetLog(int pos, String message) {
        if (!isLogged(pos)) {
            log(pos, message);
            return false;
        }
        return true;
    }

    /** get error message with line from sourcefile
     */
    public String getMessage(int pos, String message) {
        if (pos == Position.NOPOS)
            return filename + ": " + message;
        else {
            int fileId = Position.file(pos);
            String filename = files[fileId];
            int line = Position.line(pos);
            int col = Position.column(pos);
            String main = filename + ":" + line + ": " + message;
            if ((fileId > 0) &&
                (fileId < numIds) &&
                (sources[fileId] != null))
                return main + '\n' + sources[fileId].getLine(line, col);
            else
                return main;
            //else
            //    System.out.println("(source file not available anymore)");
        }
    }

    /** get source line
     */
    public String getLine(int line, int col) {
        int pos = 0;
        if (lastLine > line)
            lastLine = 0;
        else
            pos = newPos;
        while ((pos < buf.length) && (lastLine < line))
        {
            lastPos = pos;
            while ((pos < buf.length) && (buf[pos] != CR) &&
                    (buf[pos] != LF) && (buf[pos] != FF))
                pos++;
            lineEnd = pos;
            if (pos < buf.length)
                pos++;
            if ((pos < buf.length) && (buf[pos-1] == CR) && (buf[pos] == LF))
                pos++;
            lastLine++;
        }
        newPos = pos;
        try
        {
            String  errline = (encoding != null) ?
                                new String(buf, lastPos, lineEnd - lastPos, encoding) :
                                new String(buf, lastPos, lineEnd - lastPos);
            if (col != NO_COLUMN)
            {
                byte[]  ptr = new byte[col];
                for (int i = col - 2; i >= 0; i--)
                    ptr[i] = (byte)' ';
                ptr[col - 1] = (byte)'^';
                return errline + '\n' + new String(ptr);
            } else
                return errline;
        } catch (UnsupportedEncodingException e) {
            throw new InternalError(e.getMessage());
        }
    }

    /** release all sourcefile objects
     */
    public static void flushSources() {
        for (int i = 0; i < sources.length; i++)
            sources[i] = null;
    }
}