View Javadoc

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