diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/tools/scaladoc/ScalaSearch.java | 5 | ||||
-rw-r--r-- | sources/scala/tools/scalai/Compiler.java | 3 | ||||
-rw-r--r-- | sources/scala/tools/util/Position.java | 4 | ||||
-rw-r--r-- | sources/scala/tools/util/SourceFile.java | 53 | ||||
-rw-r--r-- | sources/scalac/Global.java | 45 | ||||
-rw-r--r-- | sources/scalac/symtab/SourceCompleter.java | 8 |
6 files changed, 62 insertions, 56 deletions
diff --git a/sources/scala/tools/scaladoc/ScalaSearch.java b/sources/scala/tools/scaladoc/ScalaSearch.java index d9f13b901c..634f3a023d 100644 --- a/sources/scala/tools/scaladoc/ScalaSearch.java +++ b/sources/scala/tools/scaladoc/ScalaSearch.java @@ -20,7 +20,6 @@ import java.util.HashSet; import ch.epfl.lamp.util.Pair; -import scala.tools.util.ByteArrayFile; import scala.tools.util.SourceFile; import scalac.Global; @@ -538,9 +537,7 @@ public class ScalaSearch { // Rem: we use a dummy extends clause, otherwise the compiler // complains. queryCounter = queryCounter + 1; - byte[] bytes = unitString.getBytes(); - ByteArrayFile file = new ByteArrayFile("tmp.scala", bytes); - SourceFile sourceFile = new SourceFile(file); + SourceFile sourceFile = global.getSourceFile("tmp.scala", unitString); 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 82def8d387..97e0c6cb29 100644 --- a/sources/scala/tools/scalai/Compiler.java +++ b/sources/scala/tools/scalai/Compiler.java @@ -21,7 +21,6 @@ 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; @@ -64,7 +63,7 @@ public class Compiler { this.any_methods = new HashMap(); this.sources = new HashMap(); - SourceFile compiled = new SourceFile(new VirtualFile("<<compiled code>>")); + SourceFile compiled = global.getSourceFile("<<compiled code>>", ""); environment.insertFunction(definitions.STRING_PLUS, Function.StringPlus); // !!! // !!! ANY_PLUS_STRING is commented out in definitions diff --git a/sources/scala/tools/util/Position.java b/sources/scala/tools/util/Position.java index a3034c8c0c..1e431b19a0 100644 --- a/sources/scala/tools/util/Position.java +++ b/sources/scala/tools/util/Position.java @@ -99,8 +99,8 @@ public class Position { // Public Constructors /** Initializes a new instance. */ - public Position(String filename) { - this(new SourceFile(new VirtualFile(filename))); + public Position(String sourcename) { + this(new SourceFile(sourcename, new byte[0])); } /** Initializes a new instance. */ diff --git a/sources/scala/tools/util/SourceFile.java b/sources/scala/tools/util/SourceFile.java index 5e12ae319d..f3a17c1d86 100644 --- a/sources/scala/tools/util/SourceFile.java +++ b/sources/scala/tools/util/SourceFile.java @@ -8,10 +8,6 @@ package scala.tools.util; -import java.io.File; -import java.io.InputStream; -import java.io.FileInputStream; -import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; @@ -34,8 +30,8 @@ public class SourceFile { /** The underlying file */ private final AbstractFile file; - /** The content of this source file or null if unavailable */ - private final byte[] bytes; + /** The content of this source file */ + private final byte[] content; /** The encoding of this source file or null if unspecified */ private String encoding; @@ -49,16 +45,15 @@ public class SourceFile { //######################################################################## // Public Constructors - /** Initializes this instance with the specified file. */ - public SourceFile(VirtualFile file) { - this.file = file; - this.bytes = normalize(file.read()); + /** Initializes this instance with given name and content. */ + public SourceFile(String sourcename, byte[] content) { + this(new ByteArrayFile(sourcename, content), content); } - /** Initializes this instance with the specified file. */ - public SourceFile(AbstractFile file) throws IOException { + /** Initializes this instance with given file and content. */ + public SourceFile(AbstractFile file, byte[] content) { this.file = file; - this.bytes = normalize(file.read()); + this.content = normalize(content); } //######################################################################## @@ -69,9 +64,9 @@ public class SourceFile { return file; } - /** Returns the content of the file. */ + /** Returns the content of this source file. */ public byte[] getContent() { - return bytes; + return content; } /** Sets the encoding of the file. */ @@ -90,24 +85,24 @@ public class SourceFile { /** Returns the specified line. */ public String getLine(int line) { int index = lineNumber <= line ? nextIndex : (lineNumber = 0); - for (; index < bytes.length && lineNumber < line; lineNumber++) { + for (; index < content.length && lineNumber < line; lineNumber++) { lineStart = index; - for (; index < bytes.length; index++) { - if (bytes[index] == CR) break; - if (bytes[index] == LF) break; - if (bytes[index] == FF) break; + for (; index < content.length; index++) { + if (content[index] == CR) break; + if (content[index] == LF) break; + if (content[index] == FF) break; } lineLength = index - lineStart; - if (index < bytes.length) + if (index < content.length) index++; - if (index < bytes.length) - if (bytes[index - 1] == CR && bytes[index] == LF) index++; + if (index < content.length) + if (content[index - 1] == CR && content[index] == LF) index++; } nextIndex = index; try { return encoding != null ? - new String(bytes, lineStart, lineLength, encoding) : - new String(bytes, lineStart, lineLength); + new String(content, lineStart, lineLength, encoding) : + new String(content, lineStart, lineLength); } catch (UnsupportedEncodingException exception) { throw new Error(exception); // !!! use ApplicationError } @@ -125,10 +120,10 @@ public class SourceFile { private static byte[] normalize(byte[] input) { if (input.length > 0 && input[input.length - 1] == SU) return input; - byte[] bytes = new byte[input.length + 1]; - System.arraycopy(input, 0, bytes, 0, input.length); - bytes[input.length] = SU; - return bytes; + byte[] content = new byte[input.length + 1]; + System.arraycopy(input, 0, content, 0, input.length); + content[input.length] = SU; + return content; } //######################################################################## diff --git a/sources/scalac/Global.java b/sources/scalac/Global.java index c6885d6447..718c5c919b 100644 --- a/sources/scalac/Global.java +++ b/sources/scalac/Global.java @@ -19,7 +19,6 @@ 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; @@ -256,6 +255,32 @@ public abstract class Global { currentPhase = currentPhase.prev; } + /** Creates a virtual source file with given name and content. */ + public SourceFile getSourceFile(String sourcename, String content) { + return new SourceFile(sourcename, content.getBytes()); + } + + /** Reads and returns the source file in file with given name. */ + public SourceFile getSourceFile(String filename) throws IOException { + return getSourceFile(AbstractFile.open(null, filename)); + } + + /** Reads and returns the source file in given abstract file. */ + public SourceFile getSourceFile(AbstractFile file) throws IOException { + if (!file.exists()) throw new FileNotFoundException( + "source file '" + file.getPath() + "' could not be found"); + byte[] content = file.read(); + return new SourceFile(file, content); + } + + /** Reads and returns the source file of given clasz. */ + public SourceFile getSourceFile(Symbol clasz) throws IOException { + assert clasz.isClass() && clasz.isStatic(): Debug.show(clasz); + AbstractFile file = classPath.openFile( + SourceRepresentation.externalizeFileName(clasz, ".scala")); + return getSourceFile(file); + } + /** the top-level compilation process */ public void compile(String[] files, boolean console) { @@ -263,17 +288,10 @@ public abstract class Global { // parse files List units = new ArrayList(files.length); for (int i = 0; i < files.length; i++) { - 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"); + try { + units.add(new Unit(this, getSourceFile(files[i]), console)); + } catch (IOException exception) { + error(exception.getMessage()); } } this.units = (Unit[])units.toArray(new Unit[units.size()]); @@ -289,8 +307,7 @@ public abstract class Global { */ public void compile(String filename, String input, boolean console) { reporter.resetCounters(); - ByteArrayFile file = new ByteArrayFile(filename, input.getBytes()); - SourceFile source = new SourceFile(file); + SourceFile source = getSourceFile(filename, input); units = new Unit[]{new Unit(this, source, console)}; compile(); } diff --git a/sources/scalac/symtab/SourceCompleter.java b/sources/scalac/symtab/SourceCompleter.java index cb44daeea8..9fd3ebb468 100644 --- a/sources/scalac/symtab/SourceCompleter.java +++ b/sources/scalac/symtab/SourceCompleter.java @@ -34,17 +34,15 @@ public class SourceCompleter extends SymbolLoader { /** complete class symbol c by loading the unit */ public String doComplete(Symbol clasz) throws IOException { - 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); + SourceFile source = global.getSourceFile(clasz); + Unit unit = new Unit(global, source, false, mixinOnly); Phase phase = global.currentPhase; global.currentPhase = global.PHASE.PARSER.phase(); global.PHASE.PARSER.phase().apply(new Unit[] {unit}); global.currentPhase = global.PHASE.ANALYZER.phase(); ((AnalyzerPhase)global.PHASE.ANALYZER.phase()).lateEnter(global, unit, clasz); global.currentPhase = phase; - return "source file '" + file + "'"; + return "source file '" + source.getFile() + "'"; } } |