[e-cvs] cvs commit: e/src/jsrc/org/erights/e/elib/tables Twine.java

markm@eros.cs.jhu.edu markm@eros.cs.jhu.edu
Thu, 6 Sep 2001 01:49:57 -0400


markm       01/09/06 01:49:57

  Modified:    src/jsrc/org/erights/e/elib/base ClassDesc.java
               src/jsrc/org/erights/e/elib/tables Twine.java
  Log:
  before reformat

Revision  Changes    Path
1.22      +64 -64    e/src/jsrc/org/erights/e/elib/base/ClassDesc.java

Index: ClassDesc.java
===================================================================
RCS file: /cvs/e/src/jsrc/org/erights/e/elib/base/ClassDesc.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- ClassDesc.java	2001/08/29 00:20:17	1.21
+++ ClassDesc.java	2001/09/06 05:49:57	1.22
@@ -18,7 +18,7 @@
 The Initial Developer of the Original Code is Mark S. Miller.
 Copyright (C) 1999 Mark S. Miller. All Rights Reserved.
 
-Contributor(s): ______________________________________. 
+Contributor(s): ______________________________________.
 */
 
 import java.math.BigInteger;
@@ -43,31 +43,31 @@
 import org.erights.e.meta.java.lang.InterfaceGuardSugar;
 
 /**
- * Describes the E-type of instances of a Java class (and its subclasses).  
- * This is the Java type as modified by the E sugar mechanism, and as seen 
+ * Describes the E-type of instances of a Java class (and its subclasses).
+ * This is the Java type as modified by the E sugar mechanism, and as seen
  * through E.
- * 
+ *
  * @author <a href="mailto:markm@erights.org">Mark S. Miller</a>
  */
 public class ClassDesc extends TypeDesc {
-        
+
     /**
-     * Maps from the fully qualified class name of a non-primitive to the 
+     * Maps from the fully qualified class name of a non-primitive to the
      * fqname of the GuardSugar class providing its coerce method. <p>
-     * 
-     * When a Class is used as a type (coerced to a ClassDesc), it might have 
-     * per-Class coercion behavior.  Such behavior would be in a GuardSugar 
-     * class that implements ValueGuard.  This table maps from the fqnames of 
-     * Classes to the fqnames of their corresponding GuardSugar class.  The 
+     *
+     * When a Class is used as a type (coerced to a ClassDesc), it might have
+     * per-Class coercion behavior.  Such behavior would be in a GuardSugar
+     * class that implements ValueGuard.  This table maps from the fqnames of
+     * Classes to the fqnames of their corresponding GuardSugar class.  The
      * ClassDesc that a Class is an instance of this GuardSugar. <p>
      *
-     * For a given Class, if a direct match isn't found, we check that 
-     * class's supertypes (both extends & implements).  If a unique 
-     * best match is found we take that.  Otherwise we throw an Exception.  
+     * For a given Class, if a direct match isn't found, we check that
+     * class's supertypes (both extends & implements).  If a unique
+     * best match is found we take that.  Otherwise we throw an Exception.
      * (XXX double check that we actually do so.) <p>
      *
-     * Arrays make use of ArrayGuardSugar, interfaces make use of 
-     * InterfaceGuardSugar by default, and fully unmatched types make use of 
+     * Arrays make use of ArrayGuardSugar, interfaces make use of
+     * InterfaceGuardSugar by default, and fully unmatched types make use of
      * ClassDesc itself.
      */
     static private String[][] GuardSugarings = {
@@ -86,7 +86,7 @@
           "org.erights.e.meta.java.lang.LongGuardSugar" },
         { "java.math.BigInteger",
           "org.erights.e.meta.java.math.BigIntegerGuardSugar" },
-          
+
         { "java.lang.Float",
           "org.erights.e.meta.java.lang.FloatGuardSugar" },
         { "java.lang.Double",
@@ -97,31 +97,31 @@
 
         { "org.erights.e.elib.base.TypeDesc",
           "org.erights.e.meta.org.erights.e.elib.base.TypeDescGuardSugar" },
-        
+
         { "org.erights.e.elib.ref.Ref",
           "org.erights.e.meta.org.erights.e.elib.ref.RefGuardSugar" },
-          
+
         { "org.erights.e.elib.slot.SlotGuard",
           "org.erights.e.meta.org.erights.e.elib.slot.SlotGuardGuardSugar" },
         { "org.erights.e.elib.slot.ValueGuard",
           "org.erights.e.meta.org.erights.e.elib.slot.ValueGuardGuardSugar" },
-          
+
         { "org.erights.e.elib.tables.EList",
           "org.erights.e.meta.org.erights.e.elib.tables.EListGuardSugar" }
     };
-    
-    /** 
+
+    /**
      * The above list of pairs turned into a ConstMap.
      */
-    static private ConstMap TheGuardSugars = 
+    static private ConstMap TheGuardSugars =
         ConstMap.fromPairs(GuardSugarings);
 
     /**
-     * 
+     *
      */
-    static private FlexMap GuardCache = 
+    static private FlexMap GuardCache =
         FlexMap.fromTypes(Class.class, Class.class);
-    
+
     /**
      * @param clazz A non-primitive class
      * @return A kind of ClassDesc.
@@ -155,7 +155,7 @@
                 supers = new Class[ifaces.length +1];
                 System.arraycopy(ifaces, 0, supers, 1, ifaces.length);
                 supers[0] = optSuper;
-            }            
+            }
             FlexMap superSugarSet = FlexMap.fromTypes(Class.class, Void.TYPE);
             for (int i = 0; i < supers.length; i++) {
                 Class superSugar = GetGuard(supers[i]);
@@ -193,18 +193,18 @@
     }
 
     /**
-     * A Mapping from classes, primitive or not, to ClassDescs (instances of 
+     * A Mapping from classes, primitive or not, to ClassDescs (instances of
      * ClassDesc or a GuardSugar subclass of ClassDesc).
      */
-    static private FlexMap DescCache = 
+    static private FlexMap DescCache =
         FlexMap.fromTypes(Class.class, ClassDesc.class);
-        
+
     /**
      * Returns the E-object that a Java Class object promotes to. <p>
      *
-     * This provides two services: 1) A descriptions of the messages that may 
-     * be sent to members of this type. 2) ValueGuard (coerce/2) behavior and 
-     * SlotGuard (makeSlot/2) behavior, enabling a Class to be used as a 
+     * This provides two services: 1) A descriptions of the messages that may
+     * be sent to members of this type. 2) ValueGuard (coerce/2) behavior and
+     * SlotGuard (makeSlot/2) behavior, enabling a Class to be used as a
      * Guard.
      */
     static public ClassDesc make(Class clazz) {
@@ -219,15 +219,15 @@
      *
      */
     static private final Class[] ArgTypes = { Class.class };
-    
+
     /**
      * Returns a description of the messages that may
      * be sent to members of this type.
      */
-    static private ClassDesc privateMake(Class clazz) 
+    static private ClassDesc privateMake(Class clazz)
     throws ClassNotFoundException,
-           IllegalAccessException, 
-           InstantiationException, 
+           IllegalAccessException,
+           InstantiationException,
            InvocationTargetException,
            NoSuchMethodException {
         ClassDesc result;
@@ -238,7 +238,7 @@
                 return result;
             }
             if (clazz.isPrimitive()) {
-                //This should only happen once, and clazz must be one of the 
+                //This should only happen once, and clazz must be one of the
                 //nine primitive TYPEs below
                 DescCache.put(Boolean.TYPE,     privateMake(Boolean.class));
                 DescCache.put(Character.TYPE,   privateMake(Character.class));
@@ -261,7 +261,7 @@
         }
         return result;
     }
-    
+
     /**
      *
      */
@@ -272,7 +272,7 @@
         script.protocol(null, mTypeList);
         return mTypeList.snapshot();
     }
-    
+
     /**
      * For use by subclasses.  XXX should move to a more general place.
      */
@@ -281,7 +281,7 @@
             throw new RuntimeException(msg);
         }
     }
-    
+
     /**
      *
      */
@@ -292,20 +292,20 @@
         Long.class,     Long.TYPE,
         BigInteger.class
     };
-    
+
     /**
      *
      */
-    static private final ConstMap IntegralTypes = 
+    static private final ConstMap IntegralTypes =
         ConstList.fromArray(IntegralTypeList).asKeys();
-        
+
     /**
      *
      */
     static public boolean isIntegral(Class clazz) {
         return IntegralTypes.maps(clazz);
     }
-    
+
     /**
      *
      */
@@ -313,25 +313,25 @@
         Float.class,    Float.TYPE,
         Double.class,   Double.TYPE
     };
-    
+
     /**
      *
      */
-    static private final ConstMap FloatingTypes = 
+    static private final ConstMap FloatingTypes =
         ConstList.fromArray(FloatingTypeList).asKeys();
-        
+
     /**
      *
      */
     static public boolean isFloating(Class clazz) {
         return FloatingTypes.maps(clazz);
     }
-    
+
     /**
      *
      */
     public final Class myClass;
-    
+
     /**
      *
      */
@@ -342,10 +342,10 @@
               mTypes(clazz));
         myClass = clazz;
     }
-    
+
     /**
-     * Shortens, handles null, handles a trivial match ({@link 
-     * Class#isInstance(Object)}), and otherwise delegates to {@link 
+     * Shortens, handles null, handles a trivial match ({@link
+     * Class#isInstance(Object)}), and otherwise delegates to {@link
      * #subCoerce(Object, OneArgFunc)}
      */
     public Object coerce(Object specimen, OneArgFunc optEjector) {
@@ -353,7 +353,7 @@
         specimen = Ref.resolution(specimen);
         if (null == specimen) {
             throw Thrower.toEject(
-                optEjector, 
+                optEjector,
                 new NullPointerException("must be a " +
                                          getName() +
                                          " rather than null"));
@@ -366,13 +366,13 @@
     }
 
     /**
-     * 'specimen' is assumed to already be shortened, not be null, and already 
+     * 'specimen' is assumed to already be shortened, not be null, and already
      * known not to be an instance of myClass.
      * <p>
-     * Used by {@link #coerce(Object, OneArgFunc)} and overridden by 
-     * subclasses to provide the type-specific part of the coercion 
-     * behavior.  The default implementation here just ejects according to 
-     * optEjector, but subclasses should override to provide other coercions. 
+     * Used by {@link #coerce(Object, OneArgFunc)} and overridden by
+     * subclasses to provide the type-specific part of the coercion
+     * behavior.  The default implementation here just ejects according to
+     * optEjector, but subclasses should override to provide other coercions.
      * <p>
      * Although this is public, it's not intended to be invokable from E.
      */
@@ -382,7 +382,7 @@
         //It's cool even if it's -1
         specTypeName = specTypeName.substring(dot + 1);
         throw Thrower.toEject(
-            optEjector, 
+            optEjector,
             new ClassCastException(specTypeName +
                                    " doesn't coerce to a " +
                                    getName()));
@@ -396,21 +396,21 @@
     /**
      * Cause "myClass[5]" to return a 5 element ConstList of myClass. <p>
      *
-     * Rather weird kludge.  Result is that, in E, "myClass[5]" doesn't get 
-     * the fifth (or sixth) of anything, but rather produces a 5 element 
+     * Rather weird kludge.  Result is that, in E, "myClass[5]" doesn't get
+     * the fifth (or sixth) of anything, but rather produces a 5 element
      * array of type myClass.
      */
     public Object get(int index) {
         return EList.newArray(myClass, index);
     }
-    
+
     /**
      * Cause "myClass[]" to return the type "list of myClass". <p>
      *
      * At the Java level, it's actually the type "Array of myClass".
      */
     public Object get() {
-        //XXX this is a horrible way to do this, but I couldn't find any 
+        //XXX this is a horrible way to do this, but I couldn't find any
         //simpler way.
         return Array.newInstance(myClass, 0).getClass();
     }



1.18      +36 -0     e/src/jsrc/org/erights/e/elib/tables/Twine.java

Index: Twine.java
===================================================================
RCS file: /cvs/e/src/jsrc/org/erights/e/elib/tables/Twine.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- Twine.java	2001/09/04 10:52:19	1.17
+++ Twine.java	2001/09/06 05:49:57	1.18
@@ -108,6 +108,42 @@
     }
 
     /**
+     * startLine defaults to 1 and startCol defaults to 0
+     */
+    public Twine asFrom(String url) {
+        return asFrom(url, 1, 0);
+    }
+
+    /**
+     * Returns a new Twine on the same underlying bare String as this one,
+     * but marked as being contiguous text starting at the position
+     * described by the argument.
+     */
+    public Twine asFrom(String url, int startLine, int startCol) {
+        FlexList parts = FlexList.fromType(AtomicTwine.class);
+        String str = bare();
+        int len = str.length();
+        int i = 0; //beginning of next line
+        int j;     //last char of next line
+        for (; i < len; i = j+1) {
+            j = str.indexOf('\n', i);
+            if (-1 == j) {
+                j = len-1; //the last char isn't necessarily a '\n'
+                //This also means the next i will be len, and so we'll exit
+                //the loop.
+            }
+            int endCol = startCol + j - i;
+            SourceSpan span = new SourceSpan(url, true,
+                                             startLine, startCol,
+                                             startLine, endCol);
+            parts.push(Twine.fromString(str.substring(i, j+1), span));
+            startLine++;
+            startCol = 0;
+        }
+        return Twine.fromParts(parts.snapshot());
+    }
+
+    /**
      * @return A list of AtomicTwines.  An empty Twine returns the empty
      *         list.
      */