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.env.except.Exception;
    19  import org.as2lib.util.StringUtil;
    20  
    21  /**
    22   * Unchecked exception thrown to indicate a syntax error in a
    23   * regular-expression pattern.
    24   *
    25   * @author Igor Sadovskiy
    26   */
    27  
    28  class org.as2lib.regexp.PatternSyntaxException extends Exception {
    29  
    30      private var description:String;
    31      private var pattern:String;
    32      private var index:Number;
    33  
    34  	private static var NEW_LINE:String = "\n";
    35  
    36      /**
    37       * Constructs a new instance of this class.
    38       *
    39       * @param description A description of the error
    40       * @param thrower The erroneous Pattern's instance thrown exception
    41       * @param args Arguments of the function thrown exception
    42       * 			
    43  
    44       */
    45      public function PatternSyntaxException(description:String, thrower:Pattern, args:FunctionArguments) {
    46  		
    47  		super(description, thrower, args);
    48  
    49  		this.description = description;
    50  		this.pattern = thrower["pattern"];
    51  		this.index = thrower["cursor"];
    52      }
    53  
    54      /**
    55       * Retrieves the error index.
    56       *
    57       * @return  The approximate index in the pattern of the error
    58       */
    59      public function getIndex(Void):Number {
    60  		return this.index;
    61      }
    62  
    63      /**
    64       * Retrieves the description of the error.
    65       *
    66       * @return  The description of the error
    67       */
    68      public function getDescription(Void):String {
    69  		return this.description;
    70      }
    71  
    72      /**
    73       * Retrieves the erroneous regular-expression pattern.
    74       *
    75       * @return  The erroneous pattern
    76       */
    77      public function getPattern(Void):String {
    78  		return this.pattern;
    79      }
    80  
    81      /**
    82       * Returns a multi-line string containing the description of the syntax
    83       * error and its index, the erroneous regular-expression pattern, and a
    84       * visual indication of the error index within the pattern.
    85       *
    86       * @return  The full detail message
    87       */
    88      public function getMessage(Void):String {
    89          var message:String = description;
    90  		if (index >= 0) {
    91  		    message += " near index " + index + ": ";
    92  		}
    93          message += NEW_LINE + pattern;
    94  		if (index >= 0) {
    95  		    message += NEW_LINE + StringUtil.multiply(" ", index) + "^";
    96  		}
    97          return message;
    98      }
    99  
   100  }
   101