1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16   
    17  import org.as2lib.regexp.node.Loop; 
    18  import org.as2lib.regexp.node.TreeInfo;
    19  
    20  /**
    21   * {@code LazyLoop} handles the repetition count for a reluctant Curly. 
    22   * The matchInit is called from the Prolog to save the index of where 
    23   * the group beginning is stored. A zero length group check occurs in the
    24   * normal match but is skipped in the matchInit.
    25   * 
    26   * @author Igor Sadovskiy
    27   */
    28   
    29  class org.as2lib.regexp.node.LazyLoop extends Loop {
    30  	
    31      public function LazyLoop(countIndex:Number, beginIndex:Number) {
    32          super(countIndex, beginIndex);
    33      }
    34      
    35      public function match(matcher:Object, i:Number, seq:String):Boolean {
    36          // Check for zero length group
    37          if (i > matcher.locals[beginIndex]) {
    38              var count:Number = matcher.locals[countIndex];
    39              if (count < cmin) {
    40                  matcher.locals[countIndex] = count + 1;
    41                  var result:Boolean = body.match(matcher, i, seq);
    42                  // If match failed we must backtrack, so
    43                  // the loop count should NOT be incremented
    44                  if (!result)
    45                      matcher.locals[countIndex] = count;
    46                  return result;
    47              }
    48              if (next.match(matcher, i, seq))
    49                  return true;
    50              if (count < cmax) {
    51                  matcher.locals[countIndex] = count + 1;
    52                  var result:Boolean = body.match(matcher, i, seq);
    53                  // If match failed we must backtrack, so
    54                  // the loop count should NOT be incremented
    55                  if (!result)
    56                      matcher.locals[countIndex] = count;
    57                  return result;
    58              }
    59              return false;
    60          }
    61          return next.match(matcher, i, seq);
    62      }
    63      
    64      public function matchInit(matcher:Object, i:Number, seq:String):Boolean {
    65          var save:Number = matcher.locals[countIndex];
    66          var ret:Boolean = false;
    67          if (0 < cmin) {
    68              matcher.locals[countIndex] = 1;
    69              ret = body.match(matcher, i, seq);
    70          } else if (next.match(matcher, i, seq)) {
    71              ret = true;
    72          } else if (0 < cmax) {
    73              matcher.locals[countIndex] = 1;
    74              ret = body.match(matcher, i, seq);
    75          }
    76          matcher.locals[countIndex] = save;
    77          return ret;
    78      }
    79      
    80      public function study(info:TreeInfo):Boolean {
    81          info.maxValid = false;
    82          info.deterministic = false;
    83          return false;
    84      }
    85  }
    86  
    87