szabivan

Loop node

Feb 12th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1.     /**
  2.      * Handles the repetition count for a greedy Curly. The matchInit
  3.      * is called from the Prolog to save the index of where the group
  4.      * beginning is stored. A zero length group check occurs in the
  5.      * normal match but is skipped in the matchInit.
  6.      */
  7.     static class Loop extends Node {
  8.         Node body;
  9.         int countIndex; // local count index in matcher locals
  10.         int beginIndex; // group beginning index
  11.         int cmin, cmax;
  12.         Loop(int countIndex, int beginIndex) {
  13.             this.countIndex = countIndex;
  14.             this.beginIndex = beginIndex;
  15.         }
  16.         boolean match(Matcher matcher, int i, CharSequence seq) {
  17.             // Avoid infinite loop in zero-length case.
  18.             if (i > matcher.locals[beginIndex]) {
  19.                 int count = matcher.locals[countIndex];
  20.  
  21.                 // This block is for before we reach the minimum
  22.                 // iterations required for the loop to match
  23.                 if (count < cmin) {
  24.                     matcher.locals[countIndex] = count + 1;
  25.                     boolean b = body.match(matcher, i, seq);
  26.                     // If match failed we must backtrack, so
  27.                     // the loop count should NOT be incremented
  28.                     if (!b)
  29.                         matcher.locals[countIndex] = count;
  30.                     // Return success or failure since we are under
  31.                     // minimum
  32.                     return b;
  33.                 }
  34.                 // This block is for after we have the minimum
  35.                 // iterations required for the loop to match
  36.                 if (count < cmax) {
  37.                     matcher.locals[countIndex] = count + 1;
  38.                     boolean b = body.match(matcher, i, seq);
  39.                     // If match failed we must backtrack, so
  40.                     // the loop count should NOT be incremented
  41.                     if (!b)
  42.                         matcher.locals[countIndex] = count;
  43.                     else
  44.                         return true;
  45.                 }
  46.             }
  47.             return next.match(matcher, i, seq);
  48.         }
  49.         boolean matchInit(Matcher matcher, int i, CharSequence seq) {
  50.             int save = matcher.locals[countIndex];
  51.             boolean ret = false;
  52.             if (0 < cmin) {
  53.                 matcher.locals[countIndex] = 1;
  54.                 ret = body.match(matcher, i, seq);
  55.             } else if (0 < cmax) {
  56.                 matcher.locals[countIndex] = 1;
  57.                 ret = body.match(matcher, i, seq);
  58.                 if (ret == false)
  59.                     ret = next.match(matcher, i, seq);
  60.             } else {
  61.                 ret = next.match(matcher, i, seq);
  62.             }
  63.             matcher.locals[countIndex] = save;
  64.             return ret;
  65.         }
  66.         boolean study(TreeInfo info) {
  67.             info.maxValid = false;
  68.             info.deterministic = false;
  69.             return false;
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment