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.Node;
    18  import org.as2lib.regexp.node.TreeInfo;
    19  
    20  /**
    21   * {@code Start} is used for REs that can start anywhere within the input 
    22   * string. This basically tries to match repeatedly at each spot in the
    23   * input string, moving forward after each try. An anchored search
    24   * or a BnM will bypass this node completely.
    25   * 
    26   * @author Igor Sadovskiy
    27   */
    28  
    29  class org.as2lib.regexp.node.Start extends Node {
    30  	
    31      private var minLength:Number;
    32      
    33      public function Start(node:Node) {
    34          this.next = node;
    35          var info:TreeInfo = new TreeInfo();
    36          next.study(info);
    37          minLength = info.minLength;
    38      }
    39      
    40      public function match(matcher:Object, i:Number, seq:String):Boolean {
    41          if (i > matcher.to - minLength) return false;
    42          var ret:Boolean = false;
    43          var guard:Number = matcher.to - minLength;
    44          
    45          for (; i <= guard; i++) {
    46              if (ret = next.match(matcher, i, seq)) break;
    47          }
    48          
    49          if (ret) {
    50              matcher.first = i;
    51              matcher.groups[0] = matcher.first;
    52              matcher.groups[1] = matcher.last;
    53          }
    54          return ret;
    55      }
    56      
    57      public function study(info:TreeInfo):Boolean {
    58          next.study(info);
    59          info.maxValid = false;
    60          info.deterministic = false;
    61          return false;
    62      }
    63  }
    64