/
Source.java
66 lines (58 loc) · 1.59 KB
/
Source.java
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
/*
* This file is part of the Jikes RVM project (http://jikesrvm.org).
*
* This file is licensed to You under the Common Public License (CPL);
* You may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.opensource.org/licenses/cpl1.0.php
*
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership.
*/
package org.mmtk.harness.lang.parser;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Provide access to script source for error messages etc.
*/
public class Source {
private final String filename;
private final ArrayList<String> lines = new ArrayList<String>();
private boolean isInitialized = false;
/**
* Create a source object for the named file.
* @param filename
*/
public Source(String filename) {
this.filename = filename;
}
private void readSource(String filename) {
try {
BufferedReader source = new BufferedReader(new FileReader(filename));
for (String line = source.readLine(); line != null; line = source.readLine()) {
lines.add(line);
}
source.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void initialize() {
if (!isInitialized) {
isInitialized = true;
readSource(filename);
}
}
/**
* Return the given source line (numbered from 1).
* @param line
* @return
*/
public String getLine(int line) {
initialize();
return lines.get(line-1);
}
}