001 /** 002 * ========================================= 003 * LibFormula : a free Java formula library 004 * ========================================= 005 * 006 * Project Info: http://reporting.pentaho.org/libformula/ 007 * 008 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either version 2.1 of the License, or (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * 026 * ------------ 027 * $Id: ParseException.java 3521 2007-10-16 10:55:14Z tmorgner $ 028 * ------------ 029 * (C) Copyright 2006-2007, by Pentaho Corporation. 030 */ 031 032 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */ 033 package org.jfree.formula.parser; 034 035 /** 036 * This exception is thrown when parse errors are encountered. 037 * You can explicitly create objects of this exception type by 038 * calling the method generateParseException in the generated 039 * parser. 040 * 041 * You can modify this class to customize your error reporting 042 * mechanisms so long as you retain the public fields. 043 */ 044 public class ParseException extends Exception { 045 046 /** 047 * This constructor is used by the method "generateParseException" 048 * in the generated parser. Calling this constructor generates 049 * a new object of this type with the fields "currentToken", 050 * "expectedTokenSequences", and "tokenImage" set. The boolean 051 * flag "specialConstructor" is also set to true to indicate that 052 * this constructor was used to create this object. 053 * This constructor calls its super class with the empty string 054 * to force the "toString" method of parent class "Throwable" to 055 * print the error message in the form: 056 * ParseException: <result of getMessage> 057 */ 058 public ParseException(final Token currentTokenVal, 059 final int[][] expectedTokenSequencesVal, 060 final String[] tokenImageVal 061 ) 062 { 063 super(""); 064 specialConstructor = true; 065 currentToken = currentTokenVal; 066 expectedTokenSequences = expectedTokenSequencesVal; 067 tokenImage = tokenImageVal; 068 } 069 070 /** 071 * The following constructors are for use by you for whatever 072 * purpose you can think of. Constructing the exception in this 073 * manner makes the exception behave in the normal way - i.e., as 074 * documented in the class "Throwable". The fields "errorToken", 075 * "expectedTokenSequences", and "tokenImage" do not contain 076 * relevant information. The JavaCC generated code does not use 077 * these constructors. 078 */ 079 080 public ParseException() { 081 super(); 082 specialConstructor = false; 083 } 084 085 public ParseException(final String message) { 086 super(message); 087 specialConstructor = false; 088 } 089 090 /** 091 * This variable determines which constructor was used to create 092 * this object and thereby affects the semantics of the 093 * "getMessage" method (see below). 094 */ 095 protected boolean specialConstructor; 096 097 /** 098 * This is the last token that has been consumed successfully. If 099 * this object has been created due to a parse error, the token 100 * followng this token will (therefore) be the first error token. 101 */ 102 public Token currentToken; 103 104 /** 105 * Each entry in this array is an array of integers. Each array 106 * of integers represents a sequence of tokens (by their ordinal 107 * values) that is expected at this point of the parse. 108 */ 109 public int[][] expectedTokenSequences; 110 111 /** 112 * This is a reference to the "tokenImage" array of the generated 113 * parser within which the parse error occurred. This array is 114 * defined in the generated ...Constants interface. 115 */ 116 public String[] tokenImage; 117 118 /** 119 * This method has the standard behavior when this object has been 120 * created using the standard constructors. Otherwise, it uses 121 * "currentToken" and "expectedTokenSequences" to generate a parse 122 * error message and returns it. If this object has been created 123 * due to a parse error, and you do not catch it (it gets thrown 124 * from the parser), then this method is called during the printing 125 * of the final stack trace, and hence the correct error message 126 * gets displayed. 127 */ 128 public String getMessage() { 129 if (!specialConstructor) { 130 return super.getMessage(); 131 } 132 String expected = ""; 133 int maxSize = 0; 134 for (int i = 0; i < expectedTokenSequences.length; i++) { 135 if (maxSize < expectedTokenSequences[i].length) { 136 maxSize = expectedTokenSequences[i].length; 137 } 138 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 139 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 140 } 141 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 142 expected += "..."; 143 } 144 expected += eol + " "; 145 } 146 String retval = "Encountered \""; 147 Token tok = currentToken.next; 148 for (int i = 0; i < maxSize; i++) { 149 if (i != 0) 150 { 151 retval += " "; 152 } 153 if (tok.kind == 0) { 154 retval += tokenImage[0]; 155 break; 156 } 157 retval += add_escapes(tok.image); 158 tok = tok.next; 159 } 160 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 161 retval += "." + eol; 162 if (expectedTokenSequences.length == 1) { 163 retval += "Was expecting:" + eol + " "; 164 } else { 165 retval += "Was expecting one of:" + eol + " "; 166 } 167 retval += expected; 168 return retval; 169 } 170 171 /** 172 * The end of line string for this machine. 173 */ 174 protected String eol = System.getProperty("line.separator", "\n"); 175 176 /** 177 * Used to convert raw characters to their escaped version 178 * when these raw version cannot be used as part of an ASCII 179 * string literal. 180 */ 181 protected String add_escapes(final String str) { 182 final StringBuffer retval = new StringBuffer(); 183 for (int i = 0; i < str.length(); i++) { 184 char ch; 185 switch (str.charAt(i)) 186 { 187 case 0 : 188 continue; 189 case '\b': 190 retval.append("\\b"); 191 continue; 192 case '\t': 193 retval.append("\\t"); 194 continue; 195 case '\n': 196 retval.append("\\n"); 197 continue; 198 case '\f': 199 retval.append("\\f"); 200 continue; 201 case '\r': 202 retval.append("\\r"); 203 continue; 204 case '\"': 205 retval.append("\\\""); 206 continue; 207 case '\'': 208 retval.append("\\\'"); 209 continue; 210 case '\\': 211 retval.append("\\\\"); 212 continue; 213 default: 214 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 215 final String s = "0000" + Integer.toString(ch, 16); 216 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 217 } else { 218 retval.append(ch); 219 } 220 continue; 221 } 222 } 223 return retval.toString(); 224 } 225 226 }