summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpaltherr <paltherr@epfl.ch>2004-03-21 04:31:47 +0000
committerpaltherr <paltherr@epfl.ch>2004-03-21 04:31:47 +0000
commitca196dd13c1008176b162d006838767e8b4d83b7 (patch)
tree813851163262df10d74c2f332647bcfaffc30a54
parent92763237f37452a9325e94f63ad5b7faa4ac04ca (diff)
downloadscala-ca196dd13c1008176b162d006838767e8b4d83b7.tar.gz
scala-ca196dd13c1008176b162d006838767e8b4d83b7.tar.bz2
scala-ca196dd13c1008176b162d006838767e8b4d83b7.zip
- Replaced file String by an AbstractFile in So...
- Replaced file String by an AbstractFile in SourceFile
-rw-r--r--sources/scala/tools/scalac/ast/parser/Scanner.scala2
-rw-r--r--sources/scala/tools/scalac/typechecker/Analyzer.scala3
-rw-r--r--sources/scala/tools/scaladoc/ScalaSearch.java9
-rw-r--r--sources/scala/tools/scalai/Compiler.java3
-rw-r--r--sources/scala/tools/scalai/Evaluator.java2
-rw-r--r--sources/scala/tools/util/Position.java68
-rw-r--r--sources/scala/tools/util/SourceFile.java84
-rw-r--r--sources/scalac/Global.java23
-rw-r--r--sources/scalac/backend/jvm/GenJVM.java2
-rw-r--r--sources/scalac/symtab/SourceCompleter.java4
-rw-r--r--sources/scalac/util/ClassPath.java13
-rw-r--r--sources/scalac/util/Reporter.java17
12 files changed, 104 insertions, 126 deletions
diff --git a/sources/scala/tools/scalac/ast/parser/Scanner.scala b/sources/scala/tools/scalac/ast/parser/Scanner.scala
index cef6bfd641..022bc8a082 100644
--- a/sources/scala/tools/scalac/ast/parser/Scanner.scala
+++ b/sources/scala/tools/scalac/ast/parser/Scanner.scala
@@ -62,7 +62,7 @@ class Scanner(_unit: Unit) extends TokenData {
/** the input buffer:
*/
- var buf: Array[byte] = unit.source.bytes();
+ var buf: Array[byte] = unit.source.getContent();
var bp: int = -1;
/** the current character
diff --git a/sources/scala/tools/scalac/typechecker/Analyzer.scala b/sources/scala/tools/scalac/typechecker/Analyzer.scala
index bdc8a7ef3a..14899ac096 100644
--- a/sources/scala/tools/scalac/typechecker/Analyzer.scala
+++ b/sources/scala/tools/scalac/typechecker/Analyzer.scala
@@ -112,7 +112,8 @@ class Analyzer(global: scalac_Global, descr: AnalyzerPhase) extends Transformer(
if (clasz.isExternal()) {
try {
val filename = SourceRepresentation.externalizeFileName(clasz, ".scala");
- val file = global.classPath.openJavaFile(filename);
+ val file = global.classPath.openFile(filename);
+ if (!file.exists()) throw new java.io.FileNotFoundException(file.getPath());
new SourceCompleter(global).complete(clasz);
} catch {
case exception: java.io.IOException =>
diff --git a/sources/scala/tools/scaladoc/ScalaSearch.java b/sources/scala/tools/scaladoc/ScalaSearch.java
index 3b53c99659..d9f13b901c 100644
--- a/sources/scala/tools/scaladoc/ScalaSearch.java
+++ b/sources/scala/tools/scaladoc/ScalaSearch.java
@@ -20,6 +20,7 @@ import java.util.HashSet;
import ch.epfl.lamp.util.Pair;
+import scala.tools.util.ByteArrayFile;
import scala.tools.util.SourceFile;
import scalac.Global;
@@ -537,11 +538,9 @@ public class ScalaSearch {
// Rem: we use a dummy extends clause, otherwise the compiler
// complains.
queryCounter = queryCounter + 1;
- InputStream in = new ByteArrayInputStream(unitString.getBytes());
- SourceFile sourceFile = null;
- try {
- sourceFile = new SourceFile("tmp.scala", in);
- } catch(IOException e) { }
+ byte[] bytes = unitString.getBytes();
+ ByteArrayFile file = new ByteArrayFile("tmp.scala", bytes);
+ SourceFile sourceFile = new SourceFile(file);
Unit tmpUnit = new Unit(global, sourceFile, false);
tmpUnit.body = new Parser$class(tmpUnit).parse();
//TreePrinter treePrinter = new TextTreePrinter(System.out);
diff --git a/sources/scala/tools/scalai/Compiler.java b/sources/scala/tools/scalai/Compiler.java
index 4393561d14..82def8d387 100644
--- a/sources/scala/tools/scalai/Compiler.java
+++ b/sources/scala/tools/scalai/Compiler.java
@@ -21,6 +21,7 @@ import java.util.Iterator;
import scala.tools.util.Position;
import scala.tools.util.SourceFile;
+import scala.tools.util.VirtualFile;
import scalac.Unit;
import scalac.Global;
@@ -63,7 +64,7 @@ public class Compiler {
this.any_methods = new HashMap();
this.sources = new HashMap();
- SourceFile compiled = new SourceFile("<<compiled code>>", new byte[0]);
+ SourceFile compiled = new SourceFile(new VirtualFile("<<compiled code>>"));
environment.insertFunction(definitions.STRING_PLUS, Function.StringPlus); // !!!
// !!! ANY_PLUS_STRING is commented out in definitions
diff --git a/sources/scala/tools/scalai/Evaluator.java b/sources/scala/tools/scalai/Evaluator.java
index 841976c06e..0728b4d92e 100644
--- a/sources/scala/tools/scalai/Evaluator.java
+++ b/sources/scala/tools/scalai/Evaluator.java
@@ -139,7 +139,7 @@ public class Evaluator {
buffer.append('.');
buffer.append(stack.symbol.nameString());
buffer.append('(');
- buffer.append(stack.source.getShortName());
+ buffer.append(stack.source.getFile().getName());
int line = Position.line(stack.pos);
if (line != 0) buffer.append(':').append(line);
buffer.append(")");
diff --git a/sources/scala/tools/util/Position.java b/sources/scala/tools/util/Position.java
index 9afdc78e7c..a3034c8c0c 100644
--- a/sources/scala/tools/util/Position.java
+++ b/sources/scala/tools/util/Position.java
@@ -86,8 +86,8 @@ public class Position {
//########################################################################
// Private Fields
- /** The position's file */
- private final SourceFile file;
+ /** The position's source file */
+ private final SourceFile source;
/** The position's line number */
private final int line;
@@ -99,26 +99,26 @@ public class Position {
// Public Constructors
/** Initializes a new instance. */
- public Position(String source) {
- this(new SourceFile(source, new byte[0]));
+ public Position(String filename) {
+ this(new SourceFile(new VirtualFile(filename)));
}
/** Initializes a new instance. */
- public Position(SourceFile file) {
- this(file, 0, 0);
+ public Position(SourceFile source) {
+ this(source, 0, 0);
}
/** Initializes a new instance. */
- public Position(SourceFile file, int position) {
- this(file, line(position), column(position));
+ public Position(SourceFile source, int position) {
+ this(source, line(position), column(position));
}
/** Initializes a new instance. */
- public Position(SourceFile file, int line, int column) {
- this.file = file;
+ public Position(SourceFile source, int line, int column) {
+ this.source = source;
this.line = line;
this.column = column;
- assert file != null;
+ assert source != null;
assert line >= 0 : line;
assert line == 0 ? column == 0 : column >= 0 : line + "," + column;
}
@@ -126,29 +126,49 @@ public class Position {
//########################################################################
// Public Methods
- /** Returns the file of this position. */
- public SourceFile file() {
- return file;
+ /** Returns the underlying abstract file's name. */
+ public String getName() {
+ return source.getFile().getName();
}
- /** Returns the line number of this position. */
- public int line() {
+ /** Returns the underlying abstract file's path position. */
+ public String getPath() {
+ return source.getFile().getPath();
+ }
+
+ /** Returns the underlying abstract file. */
+ public AbstractFile getAbstractFile() {
+ return source.getFile();
+ }
+
+ /** Returns the source file. */
+ public SourceFile getSourceFile() {
+ return source;
+ }
+
+ /** Returns the line content or null if it's unavailable. */
+ public String getLineContent() {
+ return line == 0 ? null : source.getLine(line);
+ }
+
+ /** Returns the line number. */
+ public int getLineNumber() {
return line;
}
- /** Returns the column number of this position. */
- public int column() {
+ /** Returns the column number. */
+ public int getColumnNumber() {
return column;
}
- /** Returns an int encoding the line and column of this position. */
- public int encodedLineColumn() {
+ /** Returns the line and column numbers encoded in an int. */
+ public int getLineAndColumnNumber() {
return encode(line, column);
}
/** Returns a string representation of this position. */
public String toString() {
- StringBuffer buffer = new StringBuffer(file.name());
+ StringBuffer buffer = new StringBuffer(source.getFile().getPath());
if (line > 0) buffer.append(":").append(line);
if (line > 0 && column > 0) buffer.append(":").append(column);
return buffer.toString();
@@ -156,14 +176,16 @@ public class Position {
/** Returns the hash code of this position. */
public int hashCode() {
- return file.hashCode() ^ encodedLineColumn();
+ return source.hashCode() ^ getLineAndColumnNumber();
}
/** Returns true iff the given object represents the same position. */
public boolean equals(Object object) {
if (!(object instanceof Position)) return false;
Position that = (Position)object;
- return file == that.file && line == that.line && column == that.column;
+ return this.source == that.source
+ && this.line == that.line
+ && this.column == that.column;
}
//########################################################################
diff --git a/sources/scala/tools/util/SourceFile.java b/sources/scala/tools/util/SourceFile.java
index 894a45ba46..5e12ae319d 100644
--- a/sources/scala/tools/util/SourceFile.java
+++ b/sources/scala/tools/util/SourceFile.java
@@ -16,8 +16,7 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
-/** This class represents a single source file.
- */
+/** This class represents a single source file. */
public class SourceFile {
//########################################################################
@@ -32,10 +31,10 @@ public class SourceFile {
//########################################################################
// Private Fields
- /** The name of this source file */
- private final String name;
+ /** The underlying file */
+ private final AbstractFile file;
- /** The content of source this file */
+ /** The content of this source file or null if unavailable */
private final byte[] bytes;
/** The encoding of this source file or null if unspecified */
@@ -50,37 +49,28 @@ public class SourceFile {
//########################################################################
// Public Constructors
- /** Initializes a new instance. */
- public SourceFile(String name) throws IOException {
- this(new File(name));
+ /** Initializes this instance with the specified file. */
+ public SourceFile(VirtualFile file) {
+ this.file = file;
+ this.bytes = normalize(file.read());
}
- /** Initializes a new instance. */
- public SourceFile(File name) throws IOException {
- this(name.toString(), read(name));
- }
-
- /** Initializes a new instance. */
- public SourceFile(String name, InputStream input) throws IOException {
- this(name, read(name, input));
- }
-
- /** Initializes a new instance. */
- public SourceFile(String name, byte[] bytes) {
- this.name = name;
- this.bytes = normalize(bytes);
+ /** Initializes this instance with the specified file. */
+ public SourceFile(AbstractFile file) throws IOException {
+ this.file = file;
+ this.bytes = normalize(file.read());
}
//########################################################################
// Public Methods
- /** Returns the name of this source file. */
- public String name() {
- return name;
+ /** Returns the underlying file. */
+ public AbstractFile getFile() {
+ return file;
}
- /** Returns the content of this source file. */
- public byte[] bytes() {
+ /** Returns the content of the file. */
+ public byte[] getContent() {
return bytes;
}
@@ -89,12 +79,6 @@ public class SourceFile {
this.encoding = encoding;
}
- /** Returns the short name (name without path) of this source file. */
- public String getShortName() {
- int start = name.lastIndexOf(File.separatorChar);
- return start < 0 ? name : name.substring(start + 1);
- }
-
/**
* Returns an instance of Position representing the given line and
* column of this source file.
@@ -103,7 +87,7 @@ public class SourceFile {
return new Position(this, line, column);
}
- /** Returns the content of the given line. */
+ /** Returns the specified line. */
public String getLine(int line) {
int index = lineNumber <= line ? nextIndex : (lineNumber = 0);
for (; index < bytes.length && lineNumber < line; lineNumber++) {
@@ -129,39 +113,13 @@ public class SourceFile {
}
}
- /** Returns the name of this source file. */
+ /** Returns the path of the underlying file. */
public String toString() {
- return name;
+ return file.getPath();
}
//########################################################################
- // Private Methods
-
- /** Reads the file and returns its content as a byte array.
- */
- private static byte[] read(File file) throws IOException {
- InputStream input = new FileInputStream(file);
- try {
- return read(file.toString(), input);
- } finally {
- input.close();
- }
- }
-
- /** Reads the InputStream and returns its content as a byte array.
- */
- private static byte[] read(String name, InputStream input) throws IOException {
- try {
- byte[] bytes = new byte[input.available() + 1];
- int numread = input.read(bytes);
- if ((numread >= 0) && (numread != bytes.length - 1))
- throw new IOException();
- bytes[bytes.length - 1] = SU;
- return bytes;
- } catch (IOException exception) {
- throw new IOException("cannot read '" + name + "'");
- }
- }
+ // Private Functions
/** Ensures that the last byte of the array is SU. */
private static byte[] normalize(byte[] input) {
diff --git a/sources/scalac/Global.java b/sources/scalac/Global.java
index af9d69092f..c6885d6447 100644
--- a/sources/scalac/Global.java
+++ b/sources/scalac/Global.java
@@ -18,6 +18,8 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
+import scala.tools.util.AbstractFile;
+import scala.tools.util.ByteArrayFile;
import scala.tools.util.Position;
import scala.tools.util.SourceFile;
@@ -261,13 +263,17 @@ public abstract class Global {
// parse files
List units = new ArrayList(files.length);
for (int i = 0; i < files.length; i++) {
- String file = files[i];
- try {
- units.add(new Unit(this, new SourceFile(file), console));
- } catch (FileNotFoundException e) {
- error("file " + file + " not found");
- } catch (IOException e) {
- error(e.getMessage());
+ String filename = files[i];
+ AbstractFile file = AbstractFile.open(null, filename);
+ if (file.exists()) {
+ try {
+ SourceFile source = new SourceFile(file);
+ units.add(new Unit(this, source, console));
+ } catch (IOException exception) {
+ error(exception.getMessage());
+ }
+ } else {
+ error("file '" + filename + "' not found");
}
}
this.units = (Unit[])units.toArray(new Unit[units.size()]);
@@ -283,7 +289,8 @@ public abstract class Global {
*/
public void compile(String filename, String input, boolean console) {
reporter.resetCounters();
- SourceFile source = new SourceFile(filename, input.getBytes());
+ ByteArrayFile file = new ByteArrayFile(filename, input.getBytes());
+ SourceFile source = new SourceFile(file);
units = new Unit[]{new Unit(this, source, console)};
compile();
}
diff --git a/sources/scalac/backend/jvm/GenJVM.java b/sources/scalac/backend/jvm/GenJVM.java
index 66f9ecca2b..f16ce8d85b 100644
--- a/sources/scalac/backend/jvm/GenJVM.java
+++ b/sources/scalac/backend/jvm/GenJVM.java
@@ -122,7 +122,7 @@ class GenJVM {
public void translate(Unit unit) {
try {
for (int i = 0; i < unit.body.length; ++i)
- gen(Context.EMPTY.withSourceFileName(unit.source.getShortName()),
+ gen(Context.EMPTY.withSourceFileName(unit.source.getFile().getName()),
unit.body[i]);
} catch (JCode.OffsetTooBigException e) {
throw global.fail(e);
diff --git a/sources/scalac/symtab/SourceCompleter.java b/sources/scalac/symtab/SourceCompleter.java
index fad727cfac..cb44daeea8 100644
--- a/sources/scalac/symtab/SourceCompleter.java
+++ b/sources/scalac/symtab/SourceCompleter.java
@@ -8,6 +8,7 @@
package scalac.symtab;
+import scala.tools.util.AbstractFile;
import scala.tools.util.SourceFile;
import scalac.*;
@@ -33,8 +34,9 @@ public class SourceCompleter extends SymbolLoader {
/** complete class symbol c by loading the unit
*/
public String doComplete(Symbol clasz) throws IOException {
- File file = global.classPath.openJavaFile(
+ AbstractFile file = global.classPath.openFile(
SourceRepresentation.externalizeFileName(clasz, ".scala"));
+ if (!file.exists()) throw new FileNotFoundException(file.getPath());
Unit unit = new Unit(global, new SourceFile(file), false, mixinOnly);
Phase phase = global.currentPhase;
global.currentPhase = global.PHASE.PARSER.phase();
diff --git a/sources/scalac/util/ClassPath.java b/sources/scalac/util/ClassPath.java
index 6e68be350a..1745141079 100644
--- a/sources/scalac/util/ClassPath.java
+++ b/sources/scalac/util/ClassPath.java
@@ -153,19 +153,6 @@ public class ClassPath {
"' not found in classpath");
}
- public java.io.File openJavaFile(String name) throws FileNotFoundException {
- if (printSearch)
- System.out.println("looking for " + name);
- for (int i = 0; i < root.length; i++) {
- if (printSearch)
- System.out.println(" in " + root[i]);
- java.io.File f = new File(root[i], name);
- if (f.exists()) return f;
- }
- throw new FileNotFoundException("file '" + name +
- "' not found in classpath");
- }
-
public String[] components() {
return root;
}
diff --git a/sources/scalac/util/Reporter.java b/sources/scalac/util/Reporter.java
index d765ec98e1..70206f46b8 100644
--- a/sources/scalac/util/Reporter.java
+++ b/sources/scalac/util/Reporter.java
@@ -152,12 +152,12 @@ public class Reporter {
public void printMessage(Position position, String message) {
if (position != null) {
message = " " + message;
- if (position.line() != 0)
- message = position.line() + ":" + message;
+ if (position.getLineNumber() != 0)
+ message = position.getLineNumber() + ":" + message;
if (shortname)
- message = position.file().getShortName() + ":" + message;
+ message = position.getName() + ":" + message;
else
- message = position.file().name() + ":" + message;
+ message = position.getPath() + ":" + message;
}
printMessage(message);
printSourceLine(position);
@@ -183,14 +183,15 @@ public class Reporter {
/** Prints the source line of the given position. */
public void printSourceLine(Position position) {
- if (position == null || position.line() == 0) return;
- printMessage(position.file().getLine(position.line()));
+ String line = position == null ? null : position.getLineContent();
+ if (line == null) return;
+ printMessage(line);
printColumnMarker(position);
}
/** Prints the column marker of the given position. */
public void printColumnMarker(Position position) {
- int column = position == null ? 0 : position.column();
+ int column = position == null ? 0 : position.getColumnNumber();
StringBuffer buffer = new StringBuffer(column);
for (int i = 1; i < column; i++) buffer.append(' ');
if (column > 0) buffer.append('^');
@@ -228,7 +229,7 @@ public class Reporter {
/** Logs a position and returns true if it was already logged. */
private boolean testAndLog(Position position) {
if (position == null) return false;
- if (position.column() == 0) return false;
+ if (position.getColumnNumber() == 0) return false;
if (positions.contains(position)) return true;
positions.add(position);
return false;