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  
    19  /**
    20   * {@code GroupTail} handles the setting of group beginning and ending
    21   * locations when groups are successfully matched. It must also be able to
    22   * unset groups that have to be backed off of.
    23   *
    24   * The {@code GroupTail} node is also used when a previous group is referenced,
    25   * and in that case no group information needs to be set.
    26   * 
    27   * @author Igor Sadovskiy
    28   */
    29   
    30  class org.as2lib.regexp.node.GroupTail extends Node {
    31  	
    32      private var localIndex:Number;
    33      private var groupIndex:Number;
    34      
    35      public function GroupTail(localCount:Number, groupCount:Number) {
    36          localIndex = localCount;
    37          groupIndex = groupCount + groupCount;
    38      }
    39      
    40      public function match(matcher:Object, i:Number, seq:String):Boolean {
    41          var tmp:Number = matcher.locals[localIndex];
    42          if (tmp >= 0) { // This is the normal group case.
    43              // Save the group so we can unset it if it
    44              // backs off of a match.
    45              var groupStart:Number = matcher.groups[groupIndex];
    46              var groupEnd:Number = matcher.groups[groupIndex+1];
    47  
    48              matcher.groups[groupIndex] = tmp;
    49              matcher.groups[groupIndex+1] = i;
    50              if (next.match(matcher, i, seq)) {
    51                  return true;
    52              }
    53              matcher.groups[groupIndex] = groupStart;
    54              matcher.groups[groupIndex+1] = groupEnd;
    55              return false;
    56          } else {
    57              // This is a group reference case. We don't need to save any
    58              // group info because it isn't really a group.
    59              matcher.last = i;
    60              return true;
    61          }
    62      }
    63      
    64      public function getLocalIndex(Void):Number {
    65      	return localIndex;
    66      }
    67      
    68      public function getGroupIndex(Void):Number {
    69      	return groupIndex;
    70      }    
    71  }
    72  
    73