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.AsciiUtil;
    18  import org.as2lib.regexp.node.Node; 
    19  
    20  /**
    21   * {@code Bound} handles word boundaries. Includes a field to allow this 
    22   * one class to deal with the different types of word boundaries we can 
    23   * match. The word characters include underscores, letters, and digits.
    24   * 
    25   * @author Igor Sadovskiy
    26   */
    27   
    28  class org.as2lib.regexp.node.Bound extends Node {
    29  	
    30      public static var LEFT:Number = 0x1;
    31      public static var RIGHT:Number= 0x2;
    32      public static var BOTH:Number = 0x3;
    33      public static var NONE:Number = 0x4;
    34      
    35      private var type:Number;
    36      
    37      public function Bound(n:Number) {
    38          type = n;
    39      }
    40      
    41      public function check(matcher:Object, i:Number, seq:String):Number {
    42          var ch:Number;
    43          var left:Boolean = false;
    44          if (i > matcher.from) {
    45              ch = seq.charCodeAt(i-1);
    46              left = (ch == 0x5F || AsciiUtil.isLower(ch) || AsciiUtil.isDigit(ch));
    47          }
    48          var right:Boolean = false;
    49          if (i < matcher.to) {
    50              ch = seq.charCodeAt(i);
    51              right = (ch == 0x5F || AsciiUtil.isLower(ch) || AsciiUtil.isDigit(ch));
    52          }
    53          return ((Number(left) ^ Number(right)) ? (right ? LEFT : RIGHT) : NONE);
    54      }
    55      
    56      public function match(matcher:Object, i:Number, seq:String):Boolean {
    57          return (check(matcher, i, seq) & type) > 0
    58              && next.match(matcher, i, seq);
    59      }
    60      
    61  }
    62  
    63