1 /*
   2  * Copyright (c) 2005, 2010, 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  * Copyright (C) 2004-2011
  27  *
  28  * Permission is hereby granted, free of charge, to any person obtaining a copy
  29  * of this software and associated documentation files (the "Software"), to deal
  30  * in the Software without restriction, including without limitation the rights
  31  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32  * copies of the Software, and to permit persons to whom the Software is
  33  * furnished to do so, subject to the following conditions:
  34  *
  35  * The above copyright notice and this permission notice shall be included in
  36  * all copies or substantial portions of the Software.
  37  *
  38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  44  * THE SOFTWARE.
  45  */
  46 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
  47 /* JavaCCOptions:KEEP_LINE_COL=null */
  48 package com.sun.xml.internal.rngom.parse.compact;
  49 
  50 /**
  51  * This exception is thrown when parse errors are encountered.
  52  * You can explicitly create objects of this exception type by
  53  * calling the method generateParseException in the generated
  54  * parser.
  55  *
  56  * You can modify this class to customize your error reporting
  57  * mechanisms so long as you retain the public fields.
  58  */
  59 public class ParseException extends Exception {
  60 
  61   /**
  62    * The version identifier for this Serializable class.
  63    * Increment only if the <i>serialized</i> form of the
  64    * class changes.
  65    */
  66   private static final long serialVersionUID = 1L;
  67 
  68   /**
  69    * This constructor is used by the method "generateParseException"
  70    * in the generated parser.  Calling this constructor generates
  71    * a new object of this type with the fields "currentToken",
  72    * "expectedTokenSequences", and "tokenImage" set.
  73    */
  74   public ParseException(Token currentTokenVal,
  75                         int[][] expectedTokenSequencesVal,
  76                         String[] tokenImageVal
  77                        )
  78   {
  79     super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
  80     currentToken = currentTokenVal;
  81     expectedTokenSequences = expectedTokenSequencesVal;
  82     tokenImage = tokenImageVal;
  83   }
  84 
  85   /**
  86    * The following constructors are for use by you for whatever
  87    * purpose you can think of.  Constructing the exception in this
  88    * manner makes the exception behave in the normal way - i.e., as
  89    * documented in the class "Throwable".  The fields "errorToken",
  90    * "expectedTokenSequences", and "tokenImage" do not contain
  91    * relevant information.  The JavaCC generated code does not use
  92    * these constructors.
  93    */
  94 
  95   public ParseException() {
  96     super();
  97   }
  98 
  99   /** Constructor with message. */
 100   public ParseException(String message) {
 101     super(message);
 102   }
 103 
 104 
 105   /**
 106    * This is the last token that has been consumed successfully.  If
 107    * this object has been created due to a parse error, the token
 108    * followng this token will (therefore) be the first error token.
 109    */
 110   public Token currentToken;
 111 
 112   /**
 113    * Each entry in this array is an array of integers.  Each array
 114    * of integers represents a sequence of tokens (by their ordinal
 115    * values) that is expected at this point of the parse.
 116    */
 117   public int[][] expectedTokenSequences;
 118 
 119   /**
 120    * This is a reference to the "tokenImage" array of the generated
 121    * parser within which the parse error occurred.  This array is
 122    * defined in the generated ...Constants interface.
 123    */
 124   public String[] tokenImage;
 125 
 126   /**
 127    * It uses "currentToken" and "expectedTokenSequences" to generate a parse
 128    * error message and returns it.  If this object has been created
 129    * due to a parse error, and you do not catch it (it gets thrown
 130    * from the parser) the correct error message
 131    * gets displayed.
 132    */
 133   private static String initialise(Token currentToken,
 134                            int[][] expectedTokenSequences,
 135                            String[] tokenImage) {
 136     String eol = System.getProperty("line.separator", "\n");
 137     StringBuffer expected = new StringBuffer();
 138     int maxSize = 0;
 139     for (int i = 0; i < expectedTokenSequences.length; i++) {
 140       if (maxSize < expectedTokenSequences[i].length) {
 141         maxSize = expectedTokenSequences[i].length;
 142       }
 143       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
 144         expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
 145       }
 146       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
 147         expected.append("...");
 148       }
 149       expected.append(eol).append("    ");
 150     }
 151     String retval = "Encountered \"";
 152     Token tok = currentToken.next;
 153     for (int i = 0; i < maxSize; i++) {
 154       if (i != 0) retval += " ";
 155       if (tok.kind == 0) {
 156         retval += tokenImage[0];
 157         break;
 158       }
 159       retval += " " + tokenImage[tok.kind];
 160       retval += " \"";
 161       retval += add_escapes(tok.image);
 162       retval += " \"";
 163       tok = tok.next;
 164     }
 165     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
 166     retval += "." + eol;
 167     if (expectedTokenSequences.length == 1) {
 168       retval += "Was expecting:" + eol + "    ";
 169     } else {
 170       retval += "Was expecting one of:" + eol + "    ";
 171     }
 172     retval += expected.toString();
 173     return retval;
 174   }
 175 
 176   /**
 177    * The end of line string for this machine.
 178    */
 179   protected String eol = System.getProperty("line.separator", "\n");
 180 
 181   /**
 182    * Used to convert raw characters to their escaped version
 183    * when these raw version cannot be used as part of an ASCII
 184    * string literal.
 185    */
 186   static String add_escapes(String str) {
 187       StringBuffer retval = new StringBuffer();
 188       char ch;
 189       for (int i = 0; i < str.length(); i++) {
 190         switch (str.charAt(i))
 191         {
 192            case 0 :
 193               continue;
 194            case '\b':
 195               retval.append("\\b");
 196               continue;
 197            case '\t':
 198               retval.append("\\t");
 199               continue;
 200            case '\n':
 201               retval.append("\\n");
 202               continue;
 203            case '\f':
 204               retval.append("\\f");
 205               continue;
 206            case '\r':
 207               retval.append("\\r");
 208               continue;
 209            case '\"':
 210               retval.append("\\\"");
 211               continue;
 212            case '\'':
 213               retval.append("\\\'");
 214               continue;
 215            case '\\':
 216               retval.append("\\\\");
 217               continue;
 218            default:
 219               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
 220                  String s = "0000" + Integer.toString(ch, 16);
 221                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
 222               } else {
 223                  retval.append(ch);
 224               }
 225               continue;
 226         }
 227       }
 228       return retval.toString();
 229    }
 230 
 231 }
 232 /* JavaCC - OriginalChecksum=bd9c46050b4c34811443efbf89741eef (do not edit this line) */