View Javadoc

    1   /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
    2
    3   /***************************************************************************************
    4    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
    5    * http://aspectwerkz.codehaus.org                                                    *
    6    * ---------------------------------------------------------------------------------- *
    7    * The software in this package is published under the terms of the LGPL license      *
    8    * a copy of which has been included with this distribution in the license.txt file.  *
    9    **************************************************************************************/
    10  package org.codehaus.aspectwerkz.annotation.expression.ast;
    11
    12  /***
    13   * This exception is thrown when parse errors are encountered. You can explicitly create objects of this exception type
    14   * by calling the method generateParseException in the generated parser. <p/>You can modify this class to customize your
    15   * error reporting mechanisms so long as you retain the public fields.
    16   */
    17  public class ParseException extends Exception {
    18      /***
    19       * This variable determines which constructor was used to create this object and thereby affects the semantics of
    20       * the "getMessage" method (see below).
    21       */
    22      protected boolean specialConstructor;
    23
    24      /***
    25       * This is the last token that has been consumed successfully. If this object has been created due to a parse error,
    26       * the token followng this token will (therefore) be the first error token.
    27       */
    28      public Token currentToken;
    29
    30      /***
    31       * Each entry in this array is an array of integers. Each array of integers represents a sequence of tokens (by
    32       * their ordinal values) that is expected at this point of the parse.
    33       */
    34      public int[][] expectedTokenSequences;
    35
    36      /***
    37       * This is a reference to the "tokenImage" array of the generated parser within which the parse error occurred. This
    38       * array is defined in the generated ...Constants interface.
    39       */
    40      public String[] tokenImage;
    41
    42      /***
    43       * The end of line string for this machine.
    44       */
    45      protected String eol = System.getProperty("line.separator", "\n");
    46
    47      /***
    48       * This constructor is used by the method "generateParseException" in the generated parser. Calling this constructor
    49       * generates a new object of this type with the fields "currentToken", "expectedTokenSequences", and "tokenImage"
    50       * set. The boolean flag "specialConstructor" is also set to true to indicate that this constructor was used to
    51       * create this object. This constructor calls its super class with the empty string to force the "toString" method
    52       * of parent class "Throwable" to print the error message in the form: ParseException: <result of getMessage>
    53       */
    54      public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
    55          super("");
    56          specialConstructor = true;
    57          currentToken = currentTokenVal;
    58          expectedTokenSequences = expectedTokenSequencesVal;
    59          tokenImage = tokenImageVal;
    60      }
    61
    62      /***
    63       * The following constructors are for use by you for whatever purpose you can think of. Constructing the exception
    64       * in this manner makes the exception behave in the normal way - i.e., as documented in the class "Throwable". The
    65       * fields "errorToken", "expectedTokenSequences", and "tokenImage" do not contain relevant information. The JavaCC
    66       * generated code does not use these constructors.
    67       */
    68      public ParseException() {
    69          super();
    70          specialConstructor = false;
    71      }
    72
    73      public ParseException(String message) {
    74          super(message);
    75          specialConstructor = false;
    76      }
    77
    78      /***
    79       * This method has the standard behavior when this object has been created using the standard constructors.
    80       * Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a parse error message and returns it.
    81       * If this object has been created due to a parse error, and you do not catch it (it gets thrown from the parser),
    82       * then this method is called during the printing of the final stack trace, and hence the correct error message gets
    83       * displayed.
    84       */
    85      public String getMessage() {
    86          if (!specialConstructor) {
    87              return super.getMessage();
    88          }
    89          String expected = "";
    90          int maxSize = 0;
    91          for (int i = 0; i < expectedTokenSequences.length; i++) {
    92              if (maxSize < expectedTokenSequences[i].length) {
    93                  maxSize = expectedTokenSequences[i].length;
    94              }
    95              for (int j = 0; j < expectedTokenSequences[i].length; j++) {
    96                  expected += (tokenImage[expectedTokenSequences[i][j]] + " ");
    97              }
    98              if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
    99                  expected += "...";
    100             }
    101             expected += (eol + "    ");
    102         }
    103         String retval = "Encountered \"";
    104         Token tok = currentToken.next;
    105         for (int i = 0; i < maxSize; i++) {
    106             if (i != 0) {
    107                 retval += " ";
    108             }
    109             if (tok.kind == 0) {
    110                 retval += tokenImage[0];
    111                 break;
    112             }
    113             retval += add_escapes(tok.image);
    114             tok = tok.next;
    115         }
    116         retval += ("\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn);
    117         retval += ("." + eol);
    118         if (expectedTokenSequences.length == 1) {
    119             retval += ("Was expecting:" + eol + "    ");
    120         } else {
    121             retval += ("Was expecting one of:" + eol + "    ");
    122         }
    123         retval += expected;
    124         return retval;
    125     }
    126
    127     /***
    128      * Used to convert raw characters to their escaped version when these raw version cannot be used as part of an ASCII
    129      * string literal.
    130      */
    131     protected String add_escapes(String str) {
    132         StringBuffer retval = new StringBuffer();
    133         char ch;
    134         for (int i = 0; i < str.length(); i++) {
    135             switch (str.charAt(i)) {
    136                 case 0:
    137                     continue;
    138                 case '\b':
    139                     retval.append("//b");
    140                     continue;
    141                 case '\t':
    142                     retval.append("//t");
    143                     continue;
    144                 case '\n':
    145                     retval.append("//n");
    146                     continue;
    147                 case '\f':
    148                     retval.append("//f");
    149                     continue;
    150                 case '\r':
    151                     retval.append("//r");
    152                     continue;
    153                 case '\"':
    154                     retval.append("//\"");
    155                     continue;
    156                 case '\'':
    157                     retval.append("//\'");
    158                     continue;
    159                 case '//':
    160                     retval.append("////");
    161                     continue;
    162                 default:
    163                     if (((ch = str.charAt(i)) < 0x20) || (ch > 0x7e)) {
    164                         String s = "0000" + Integer.toString(ch, 16);
    165                         retval.append("//u" + s.substring(s.length() - 4, s.length()));
    166                     } else {
    167                         retval.append(ch);
    168                     }
    169                     continue;
    170             }
    171         }
    172         return retval.toString();
    173     }
    174 }