changeset 19690:c152a485d747

Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 03 Mar 2015 17:55:10 -0800
parents 9d2ff2e8360d
children 898686c10193 27fe86a6fb49
files graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java
diffstat 1 files changed, 19 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java	Tue Mar 03 17:13:51 2015 -0800
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java	Tue Mar 03 17:55:10 2015 -0800
@@ -124,15 +124,16 @@
     /**
      * Gets the canonical representation of a source file, whose contents have already been read and
      * need not be read again. It is confirmed that the file resolves to a file name, so it can be
-     * indexed by canonical path, but it is not confirmed to be readable.
+     * indexed by canonical path. It is not confirmed that the text supplied agrees with the file's
+     * contents or even whether the file is readable.
      *
      * @param chars textual source code already read from the file
      * @param fileName
      * @return canonical representation of the file's contents.
      * @throws IOException if the file cannot be found
-     * @throws IllegalArgumentException if there is already a Source indexed under this file name
      */
-    public static Source fromFileName(CharSequence chars, String fileName) throws IOException, IllegalArgumentException {
+    public static Source fromFileName(CharSequence chars, String fileName) throws IOException {
+
         final WeakReference<Source> nameRef = filePathToSource.get(fileName);
         Source source = nameRef == null ? null : nameRef.get();
         if (source == null) {
@@ -144,10 +145,9 @@
             if (source == null) {
                 source = new FileSource(file, fileName, path, chars);
                 filePathToSource.put(path, new WeakReference<>(source));
-                return source;
             }
         }
-        throw new IOException("Source already exists for file:" + fileName);
+        return source;
     }
 
     /**
@@ -304,6 +304,13 @@
     }
 
     /**
+     * Gets the number of characters in the source.
+     */
+    public final int getLength() {
+        return checkTextMap().length();
+    }
+
+    /**
      * Returns the complete text of the code.
      */
     public abstract String getCode();
@@ -1136,6 +1143,13 @@
         }
 
         /**
+         * The number of characters in the mapped text.
+         */
+        public int length() {
+            return textLength;
+        }
+
+        /**
          * The number of lines in the text; if characters appear after the final newline, then they
          * also count as a line, even though not newline-terminated.
          */