1 /*
   2  * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
  27 package com.sun.jmx.snmp.IPAcl;
  28 
  29 /**
  30  * This exception is thrown when parse errors are encountered.
  31  * You can explicitly create objects of this exception type by
  32  * calling the method generateParseException in the generated
  33  * parser.
  34  *
  35  * You can modify this class to customize your error reporting
  36  * mechanisms so long as you retain the public fields.
  37  */
  38 class ParseException extends Exception {
  39   private static final long serialVersionUID = -3695190720704845876L;
  40 
  41   /**
  42    * This constructor is used by the method "generateParseException"
  43    * in the generated parser.  Calling this constructor generates
  44    * a new object of this type with the fields "currentToken",
  45    * "expectedTokenSequences", and "tokenImage" set.  The boolean
  46    * flag "specialConstructor" is also set to true to indicate that
  47    * this constructor was used to create this object.
  48    * This constructor calls its super class with the empty string
  49    * to force the "toString" method of parent class "Throwable" to
  50    * print the error message in the form:
  51    *     ParseException: <result of getMessage>
  52    */
  53   public ParseException(Token currentTokenVal,
  54                         int[][] expectedTokenSequencesVal,
  55                         String[] tokenImageVal
  56                        )
  57   {
  58     super("");
  59     specialConstructor = true;
  60     currentToken = currentTokenVal;
  61     expectedTokenSequences = expectedTokenSequencesVal;
  62     tokenImage = tokenImageVal;
  63   }
  64 
  65   /**
  66    * The following constructors are for use by you for whatever
  67    * purpose you can think of.  Constructing the exception in this
  68    * manner makes the exception behave in the normal way - i.e., as
  69    * documented in the class "Throwable".  The fields "errorToken",
  70    * "expectedTokenSequences", and "tokenImage" do not contain
  71    * relevant information.  The JavaCC generated code does not use
  72    * these constructors.
  73    */
  74 
  75   public ParseException() {
  76     super();
  77     specialConstructor = false;
  78   }
  79 
  80   public ParseException(String message) {
  81     super(message);
  82     specialConstructor = false;
  83   }
  84 
  85   /**
  86    * This variable determines which constructor was used to create
  87    * this object and thereby affects the semantics of the
  88    * "getMessage" method (see below).
  89    */
  90   protected boolean specialConstructor;
  91 
  92   /**
  93    * This is the last token that has been consumed successfully.  If
  94    * this object has been created due to a parse error, the token
  95    * followng this token will (therefore) be the first error token.
  96    */
  97   public Token currentToken;
  98 
  99   /**
 100    * Each entry in this array is an array of integers.  Each array
 101    * of integers represents a sequence of tokens (by their ordinal
 102    * values) that is expected at this point of the parse.
 103    */
 104   public int[][] expectedTokenSequences;
 105 
 106   /**
 107    * This is a reference to the "tokenImage" array of the generated
 108    * parser within which the parse error occurred.  This array is
 109    * defined in the generated ...Constants interface.
 110    */
 111   public String[] tokenImage;
 112 
 113   /**
 114    * This method has the standard behavior when this object has been
 115    * created using the standard constructors.  Otherwise, it uses
 116    * "currentToken" and "expectedTokenSequences" to generate a parse
 117    * error message and returns it.  If this object has been created
 118    * due to a parse error, and you do not catch it (it gets thrown
 119    * from the parser), then this method is called during the printing
 120    * of the final stack trace, and hence the correct error message
 121    * gets displayed.
 122    */
 123   public String getMessage() {
 124     if (!specialConstructor) {
 125       return super.getMessage();
 126     }
 127     String expected = "";
 128     int maxSize = 0;
 129     for (int i = 0; i < expectedTokenSequences.length; i++) {
 130       if (maxSize < expectedTokenSequences[i].length) {
 131         maxSize = expectedTokenSequences[i].length;
 132       }
 133       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
 134         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
 135       }
 136       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
 137         expected += "...";
 138       }
 139       expected += eol + "    ";
 140     }
 141     String retval = "Encountered \"";
 142     Token tok = currentToken.next;
 143     for (int i = 0; i < maxSize; i++) {
 144       if (i != 0) retval += " ";
 145       if (tok.kind == 0) {
 146         retval += tokenImage[0];
 147         break;
 148       }
 149       retval += add_escapes(tok.image);
 150       tok = tok.next;
 151     }
 152     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
 153     if (expectedTokenSequences.length == 1) {
 154       retval += "Was expecting:" + eol + "    ";
 155     } else {
 156       retval += "Was expecting one of:" + eol + "    ";
 157     }
 158     retval += expected;
 159     return retval;
 160   }
 161 
 162   /**
 163    * The end of line string for this machine.
 164    */
 165   protected String eol = System.getProperty("line.separator", "\n");
 166 
 167   /**
 168    * Used to convert raw characters to their escaped version
 169    * when these raw version cannot be used as part of an ASCII
 170    * string literal.
 171    */
 172   protected String add_escapes(String str) {
 173       StringBuilder retval = new StringBuilder();
 174       char ch;
 175       for (int i = 0; i < str.length(); i++) {
 176         switch (str.charAt(i))
 177         {
 178            case 0 :
 179               continue;
 180            case '\b':
 181               retval.append("\\b");
 182               continue;
 183            case '\t':
 184               retval.append("\\t");
 185               continue;
 186            case '\n':
 187               retval.append("\\n");
 188               continue;
 189            case '\f':
 190               retval.append("\\f");
 191               continue;
 192            case '\r':
 193               retval.append("\\r");
 194               continue;
 195            case '\"':
 196               retval.append("\\\"");
 197               continue;
 198            case '\'':
 199               retval.append("\\\'");
 200               continue;
 201            case '\\':
 202               retval.append("\\\\");
 203               continue;
 204            default:
 205               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
 206                  String s = "0000" + Integer.toString(ch, 16);
 207                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
 208               } else {
 209                  retval.append(ch);
 210               }
 211               continue;
 212         }
 213       }
 214       return retval.toString();
 215    }
 216 
 217 }