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 Dollar} is a node to anchor at the end of a line or the 
    22   * end of input based on the multiline mode.
    23   *
    24   * When not in multiline mode, the $ can only match at the very end
    25   * of the input, unless the input ends in a line terminator in which
    26   * it matches right before the last line terminator.
    27   *
    28   * Note that \r\n is considered an atomic line terminator.
    29   * 
    30   * Like ^ the $ operator matches at a position, it does not match the
    31   * line terminators themselves.
    32   * 
    33   * @author Igor Sadovskiy
    34   */
    35  
    36  class org.as2lib.regexp.node.Dollar extends Node {
    37  	
    38      private var multiline:Boolean;
    39      
    40      public function Dollar(mul:Boolean) {
    41          multiline = mul;
    42      }
    43      
    44      public function match(matcher:Object, i:Number, seq:String):Boolean {
    45          if (!multiline) {
    46              if (i < matcher.to - 2)
    47                  return false;
    48              if (i == matcher.to - 2) {
    49                  var ch:Number = seq.charCodeAt(i);
    50                  if (ch != ord("r")) return false;
    51                  ch = seq.charCodeAt(i+1);
    52                  if (ch != ord("\n")) return false;
    53              }
    54          }
    55          // Matches before any line terminator; also matches at the
    56          // end of input
    57          if (i < matcher.to) {
    58              var ch:Number = seq.charCodeAt(i);
    59               if (ch == ord("\n")) {
    60                   // No match between \r\n
    61                   if (i > 0 && seq.charAt(i-1) == "r")
    62                       return false;
    63               } else if (ch == ord("r") || ch == ord("u0085") ||
    64                          (ch|1) == ord("u2029")) {
    65                   // line terminator; match
    66               } else { // No line terminator, no match
    67                   return false;
    68               }
    69          }
    70          return next.match(matcher, i, seq);
    71      }
    72      
    73      public function study(info:TreeInfo):Boolean {
    74          next.study(info);
    75          return info.deterministic;
    76      }
    77  }
    78