1 /* 2 * Copyright (c) 1999, 2013, 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 5.0 */ 27 /* JavaCCOptions:KEEP_LINE_COL=null */ 28 package com.sun.tools.example.debug.expr; 29 30 /** 31 * This exception is thrown when parse errors are encountered. 32 * You can explicitly create objects of this exception type by 33 * calling the method generateParseException in the generated 34 * parser. 35 * 36 * You can modify this class to customize your error reporting 37 * mechanisms so long as you retain the public fields. 38 */ 39 public class ParseException extends Exception { 40 41 /** 42 * The version identifier for this Serializable class. 43 * Increment only if the <i>serialized</i> form of the 44 * class changes. 45 */ 46 private static final long serialVersionUID = 1L; 47 48 /** 49 * This constructor is used by the method "generateParseException" 50 * in the generated parser. Calling this constructor generates 51 * a new object of this type with the fields "currentToken", 52 * "expectedTokenSequences", and "tokenImage" set. 53 */ 54 public ParseException(Token currentTokenVal, 55 int[][] expectedTokenSequencesVal, 56 String[] tokenImageVal 57 ) 58 { 59 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); 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 } 78 79 /** Constructor with message. */ 80 public ParseException(String message) { 81 super(message); 82 } 83 84 85 /** 86 * This is the last token that has been consumed successfully. If 87 * this object has been created due to a parse error, the token 88 * followng this token will (therefore) be the first error token. 89 */ 90 public Token currentToken; 91 92 /** 93 * Each entry in this array is an array of integers. Each array 94 * of integers represents a sequence of tokens (by their ordinal 95 * values) that is expected at this point of the parse. 96 */ 97 public int[][] expectedTokenSequences; 98 99 /** 100 * This is a reference to the "tokenImage" array of the generated 101 * parser within which the parse error occurred. This array is 102 * defined in the generated ...Constants interface. 103 */ 104 public String[] tokenImage; 105 106 /** 107 * It uses "currentToken" and "expectedTokenSequences" to generate a parse 108 * error message and returns it. If this object has been created 109 * due to a parse error, and you do not catch it (it gets thrown 110 * from the parser) the correct error message 111 * gets displayed. 112 */ 113 private static String initialise(Token currentToken, 114 int[][] expectedTokenSequences, 115 String[] tokenImage) { 116 String eol = System.getProperty("line.separator", "\n"); 117 StringBuilder expected = new StringBuilder(); 118 int maxSize = 0; 119 for (int i = 0; i < expectedTokenSequences.length; i++) { 120 if (maxSize < expectedTokenSequences[i].length) { 121 maxSize = expectedTokenSequences[i].length; 122 } 123 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 124 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' '); 125 } 126 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 127 expected.append("..."); 128 } 129 expected.append(eol).append(" "); 130 } 131 String retval = "Encountered \""; 132 Token tok = currentToken.next; 133 for (int i = 0; i < maxSize; i++) { 134 if (i != 0) retval += " "; 135 if (tok.kind == 0) { 136 retval += tokenImage[0]; 137 break; 138 } 139 retval += " " + tokenImage[tok.kind]; 140 retval += " \""; 141 retval += add_escapes(tok.image); 142 retval += " \""; 143 tok = tok.next; 144 } 145 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 146 retval += "." + eol; 147 if (expectedTokenSequences.length == 1) { 148 retval += "Was expecting:" + eol + " "; 149 } else { 150 retval += "Was expecting one of:" + eol + " "; 151 } 152 retval += expected.toString(); 153 return retval; 154 } 155 156 /** 157 * The end of line string for this machine. 158 */ 159 protected String eol = System.getProperty("line.separator", "\n"); 160 161 /** 162 * Used to convert raw characters to their escaped version 163 * when these raw version cannot be used as part of an ASCII 164 * string literal. 165 */ 166 static String add_escapes(String str) { 167 StringBuilder retval = new StringBuilder(); 168 char ch; 169 for (int i = 0; i < str.length(); i++) { 170 switch (str.charAt(i)) 171 { 172 case 0 : 173 continue; 174 case '\b': 175 retval.append("\\b"); 176 continue; 177 case '\t': 178 retval.append("\\t"); 179 continue; 180 case '\n': 181 retval.append("\\n"); 182 continue; 183 case '\f': 184 retval.append("\\f"); 185 continue; 186 case '\r': 187 retval.append("\\r"); 188 continue; 189 case '\"': 190 retval.append("\\\""); 191 continue; 192 case '\'': 193 retval.append("\\\'"); 194 continue; 195 case '\\': 196 retval.append("\\\\"); 197 continue; 198 default: 199 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 200 String s = "0000" + Integer.toString(ch, 16); 201 retval.append("\\u").append(s.substring(s.length() - 4, s.length())); 202 } else { 203 retval.append(ch); 204 } 205 continue; 206 } 207 } 208 return retval.toString(); 209 } 210 211 } 212 /* JavaCC - OriginalChecksum=3c9f049ed2bb6ade635c5bf58a386169 (do not edit this line) */