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