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.Pattern;
    18  import org.as2lib.regexp.node.Node;
    19  import org.as2lib.regexp.node.TreeInfo;
    20  
    21  /**
    22   * {@code Ques} is a node to anchor "?" character. It handles all three
    23   * kinds of its use.
    24   * 
    25   * @author Igor Sadovskiy
    26   */
    27   
    28  class org.as2lib.regexp.node.Ques extends Node {
    29  	
    30      private var atom:Node;
    31      private var type:Number;
    32      
    33      public function Ques(node:Node, type:Number) {
    34          this.atom = node;
    35          this.type = type;
    36      }
    37      
    38      public function match(matcher:Object, i:Number, seq:String):Boolean {
    39          switch (type) {
    40          case Pattern.GREEDY:
    41              return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
    42                  || next.match(matcher, i, seq);
    43          case Pattern.LAZY:
    44              return next.match(matcher, i, seq)
    45                  || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
    46          case Pattern.POSSESSIVE:
    47              if (atom.match(matcher, i, seq)) i = matcher.last;
    48              return next.match(matcher, i, seq);
    49          default:
    50              return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
    51          }
    52      }
    53      
    54      public function study(info:TreeInfo):Boolean {
    55          if (type != Pattern.INDEPENDENT) {
    56              var minL:Number = info.minLength;
    57              atom.study(info);
    58              info.minLength = minL;
    59              info.deterministic = false;
    60              return next.study(info);
    61          } else {
    62              atom.study(info);
    63              return next.study(info);
    64          }
    65      }
    66      
    67      public function getType(Void):Number {
    68      	return type;	
    69      }
    70  }
    71  
    72