예제 #1
0
 /**
  * may be called by a callback to retrieve see <a
  * href="doc-files/resyntax.html#rse">submatches</a>. Retrieving submatches must be done before
  * the match is changed in any way. A typical call within an {@link FaAction} looks like
  *
  * <pre>
  *   public void invoke(StringBuilder out, int start, DfaRun r)
  *     throws CallbackException {
  *   {
  *     TextStore ts = r.submatches(out, start);
  *     ...
  *   }</pre>
  *
  * Parameter <code>txt</code> is not changed in any way.
  *
  * @param txt must contain the full match starting at position <code>start</code>. It may contain
  *     more characters.
  * @param start is the position where the full match starts within <code>txt</code>
  * @return a <code>TextStore</code> that contains the whole match as part 0 and submatches as
  *     subsequent parts. The return value is private to <code>this</code> and its contents may
  *     only be used locally in a callback. After returning from the callback, the contents of the
  *     result may soon change.
  */
 public TextStore submatches(StringBuilder txt, int start) {
   readTs.clear();
   // System.out.println("-->"+txt+"<--, `"+txt.substring(start)
   // +"'  "+smd.size);
   readTs.appendPart(txt, start, txt.length());
   smd.analyze(readTs, action);
   return readTs;
 }
예제 #2
0
 /**
  * returns a <code>TextStore</code> which contains submatches, if any, pertaining to the most
  * recent match. The object returned should be treated read-only. Its contents are only valid
  * until the next match operation.
  *
  * @throws IllegalStateException if the most recent application of <code>this</code> did not yield
  *     a match.
  */
 public TextStore submatches() {
   if (a == null || a == DfaRun.EOF) {
     throw new IllegalStateException("no recent match available");
   }
   if (!analyzed) {
     analyzed = true;
     ts.clear();
     ts.appendPart(out, 0, out.length());
     smd.analyze(ts, a);
   }
   return ts;
 }
 // depending on the object in 'client' the strings in 'tests' should
 // probably be changed!! (Needs some thought if additional Splitters
 // are implemented.
 public void testGeneric1() {
   for (int i = 0; i < tests.length; i++) {
     store.clear();
     client.split(store, new StringBuffer(tests[i][0]), 0);
     assertEquals(tests[i].length, store.getNumParts());
     StringBuffer sb = new StringBuffer();
     store.getPart(sb, 0);
     assertEquals(tests[i][0], sb.toString());
     for (int j = 1; j < tests[i].length; j++) {
       sb.setLength(0);
       store.getPart(sb, j);
       assertEquals("loop with j=" + j, tests[i][j], sb.toString());
     }
   }
 }
 public void testBeforeSplit3() {
   StringBuffer sb = new StringBuffer();
   Exception e = null;
   store.getPart(sb, 0);
   assertEquals("", sb.toString());
   //     try {
   //       store.getPart(sb, 0);
   //     } catch( ArrayIndexOutOfBoundsException _e ){
   //       e = _e;
   //     }
   //     assertTrue(e instanceof ArrayIndexOutOfBoundsException);
 }
예제 #5
0
 /**
  * shoves back characters into the input of the <code>DfaRun</code>. This method simply applies
  * {@link TextStore#drain TextStore.drain()} to the input of <code>this</code>. Consequently,
  * <code>start</code> may be negative to indicate a suffix of <code>ts</code> to be pushed back.
  */
 public void unskip(TextStore ts, int start) {
   ts.drain(in, start);
 }
 public void testBeforeSplit1() {
   assertEquals(0, store.getNumParts());
 }
  public void testSubstring() {
    client.split(store, new StringBuffer(testAIn), 3);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 2; i++) {
      int L = testAOut[i].length();

      sb.setLength(0);
      store.getPart(sb, i);
      assertEquals("i=" + i, testAOut[i], sb.toString());
      assertEquals("i=" + i, testAOut[i].length(), store.getPartLen(i));

      sb.setLength(0);
      store.getPart(sb, i, 1, -1);
      assertEquals("i=" + i, testAOut[i].substring(1, L - 1), sb.toString());

      sb.setLength(0);
      store.getPart(sb, i, -3, 0);
      assertEquals(testAOut[i].substring(L - 3, L), sb.toString());

      sb.setLength(0);
      store.getPart(sb, i, 2, 2);
      assertEquals("", sb.toString());

      sb.setLength(0);
      store.getPart(sb, i, 2, 3);
      assertEquals(testAOut[i].substring(2, 3), sb.toString());

      // test left index too small
      sb.setLength(0);
      store.getPart(sb, i, -100, 0);
      assertEquals(testAOut[i], sb.toString());
      //       Exception e = null;
      //       try {
      // 	store.getPart(sb, i, -100, 0);
      // 	System.out.println("*************"+sb);
      //       } catch( Exception _e) {
      // 	e = _e;
      //       }
      //       assertTrue(e instanceof StringIndexOutOfBoundsException);

      // test left index much too large
      sb.setLength(0);
      store.getPart(sb, i, 10000, 0);
      assertEquals("", sb.toString());
      //       e = null;
      //       try {
      // 	store.getPart(sb, i, 10000, 0);
      //       } catch( Exception _e) {
      // 	e = _e;
      //       }
      //       assertTrue(e instanceof StringIndexOutOfBoundsException);

      // test right index too small
      sb.setLength(0);
      store.getPart(sb, i, 0, -10000);
      assertEquals("", sb.toString());
      //       e = null;
      //       try {
      // 	store.getPart(sb, i, 0, -10000);
      //       } catch( Exception _e) {
      // 	e = _e;
      //       }
      //       assertTrue(e instanceof StringIndexOutOfBoundsException);

      // test right index far too large
      sb.setLength(0);
      store.getPart(sb, i, 0, 10000);
      assertEquals(testAOut[i], sb.toString());
      //       e = null;
      //       try {
      // 	store.getPart(sb, i, 0, 10000);
      //       } catch( Exception _e) {
      // 	e = _e;
      //       }
      //       assertTrue(e instanceof StringIndexOutOfBoundsException);

      // out of range part should return empty string
      sb.setLength(0);
      store.getPart(sb, 299, 0, 10000);
      assertEquals("", sb.toString());
      //       e = null;
      //       try {
      // 	store.getPart(sb, 1000, 0, 1);
      //       } catch( Exception _e) {
      // 	e = _e;
      //       }
      //       assertTrue(e instanceof ArrayIndexOutOfBoundsException);

      // yet another one, this time in getPartLen
      assertEquals(0, store.getPartLen(1000));
      //       e = null;
      //       try {
      // 	store.getPartLen(1000);
      //       } catch( Exception _e) {
      // 	e = _e;
      //       }
      //       assertTrue(e instanceof ArrayIndexOutOfBoundsException);

    }
  }