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 Branch} Handles the branching of alternations. Note this is also 
    22   * used for the ? quantifier to branch between the case where it matches once
    23   * and where it does not occur.
    24   * 
    25   * @author Igor Sadovskiy
    26   */
    27  
    28  class org.as2lib.regexp.node.Branch extends Node {
    29  	
    30      private var prev:Node;
    31      
    32      public function Branch(lhs:Node, rhs:Node) {
    33          this.prev = lhs;
    34          this.next = rhs;
    35      }
    36      
    37      public function match(matcher:Object, i:Number, seq:String):Boolean {
    38          return (prev.match(matcher, i, seq) || next.match(matcher, i, seq));
    39      }
    40      
    41      public function study(info:TreeInfo):Boolean {
    42          var minL:Number = info.minLength;
    43          var maxL:Number = info.maxLength;
    44          var maxV:Boolean = info.maxValid;
    45          info.reset();
    46          prev.study(info);
    47  
    48          var minL2:Number = info.minLength;
    49          var maxL2:Number = info.maxLength;
    50          var maxV2:Boolean = info.maxValid;
    51          info.reset();
    52          next.study(info);
    53  
    54          info.minLength = minL + Math.min(minL2, info.minLength);
    55          info.maxLength = maxL + Math.max(maxL2, info.maxLength);
    56          info.maxValid = (maxV && maxV2 && info.maxValid);
    57          info.deterministic = false;
    58          return false;
    59      }
    60  }
    61  
    62