KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > javancss > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
2
3 /*
4  * Cobertura - http://cobertura.sourceforge.net/
5  *
6  * This file was taken from JavaNCSS
7  * http://www.kclee.com/clemens/java/javancss/
8  * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com>
9  *
10  * Cobertura is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License,
13  * or (at your option) any later version.
14  *
15  * Cobertura is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Cobertura; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */

25
26 package net.sourceforge.cobertura.javancss;
27
28 /**
29  * This exception is thrown when parse errors are encountered.
30  * You can explicitly create objects of this exception type by
31  * calling the method generateParseException in the generated
32  * parser.
33  *
34  * You can modify this class to customize your error reporting
35  * mechanisms so long as you retain the public fields.
36  */

37 class ParseException extends Exception JavaDoc
38 {
39
40     private static final long serialVersionUID = 0L;
41
42     /**
43      * This constructor is used by the method "generateParseException"
44      * in the generated parser. Calling this constructor generates
45      * a new object of this type with the fields "currentToken",
46      * "expectedTokenSequences", and "tokenImage" set. The boolean
47      * flag "specialConstructor" is also set to true to indicate that
48      * this constructor was used to create this object.
49      * This constructor calls its super class with the empty string
50      * to force the "toString" method of parent class "Throwable" to
51      * print the error message in the form:
52      * ParseException: <result of getMessage>
53      */

54     public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal,
55             String JavaDoc[] tokenImageVal)
56     {
57         super("");
58         specialConstructor = true;
59         currentToken = currentTokenVal;
60         expectedTokenSequences = expectedTokenSequencesVal;
61         tokenImage = tokenImageVal;
62     }
63
64     /**
65      * The following constructors are for use by you for whatever
66      * purpose you can think of. Constructing the exception in this
67      * manner makes the exception behave in the normal way - i.e., as
68      * documented in the class "Throwable". The fields "errorToken",
69      * "expectedTokenSequences", and "tokenImage" do not contain
70      * relevant information. The JavaCC generated code does not use
71      * these constructors.
72      */

73
74     public ParseException()
75     {
76         super();
77         specialConstructor = false;
78     }
79
80     public ParseException(String JavaDoc message)
81     {
82         super(message);
83         specialConstructor = false;
84     }
85
86     /**
87      * This variable determines which constructor was used to create
88      * this object and thereby affects the semantics of the
89      * "getMessage" method (see below).
90      */

91     protected boolean specialConstructor;
92
93     /**
94      * This is the last token that has been consumed successfully. If
95      * this object has been created due to a parse error, the token
96      * followng this token will (therefore) be the first error token.
97      */

98     public Token currentToken;
99
100     /**
101      * Each entry in this array is an array of integers. Each array
102      * of integers represents a sequence of tokens (by their ordinal
103      * values) that is expected at this point of the parse.
104      */

105     public int[][] expectedTokenSequences;
106
107     /**
108      * This is a reference to the "tokenImage" array of the generated
109      * parser within which the parse error occurred. This array is
110      * defined in the generated ...Constants interface.
111      */

112     public String JavaDoc[] tokenImage;
113
114     /**
115      * This method has the standard behavior when this object has been
116      * created using the standard constructors. Otherwise, it uses
117      * "currentToken" and "expectedTokenSequences" to generate a parse
118      * error message and returns it. If this object has been created
119      * due to a parse error, and you do not catch it (it gets thrown
120      * from the parser), then this method is called during the printing
121      * of the final stack trace, and hence the correct error message
122      * gets displayed.
123      */

124     public String JavaDoc getMessage()
125     {
126         if (!specialConstructor)
127         {
128             return super.getMessage();
129         }
130         String JavaDoc expected = "";
131         int maxSize = 0;
132         for (int i = 0; i < expectedTokenSequences.length; i++)
133         {
134             if (maxSize < expectedTokenSequences[i].length)
135             {
136                 maxSize = expectedTokenSequences[i].length;
137             }
138             for (int j = 0; j < expectedTokenSequences[i].length; j++)
139             {
140                 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
141             }
142             if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0)
143             {
144                 expected += "...";
145             }
146             expected += eol + " ";
147         }
148         String JavaDoc retval = "Encountered \"";
149         Token tok = currentToken.next;
150         for (int i = 0; i < maxSize; i++)
151         {
152             if (i != 0)
153                 retval += " ";
154             if (tok.kind == 0)
155             {
156                 retval += tokenImage[0];
157                 break;
158             }
159             retval += add_escapes(tok.image);
160             tok = tok.next;
161         }
162         retval += "\" at line " + currentToken.next.beginLine + ", column "
163                 + currentToken.next.beginColumn + "." + eol;
164         if (expectedTokenSequences.length == 1)
165         {
166             retval += "Was expecting:" + eol + " ";
167         }
168         else
169         {
170             retval += "Was expecting one of:" + eol + " ";
171         }
172         retval += expected;
173         return retval;
174     }
175
176     /**
177      * The end of line string for this machine.
178      */

179     protected String JavaDoc 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     protected String JavaDoc add_escapes(String JavaDoc str)
187     {
188         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
189         char ch;
190         for (int i = 0; i < str.length(); i++)
191         {
192             switch (str.charAt(i))
193             {
194                 case 0:
195                     continue;
196                 case '\b':
197                     retval.append("\\b");
198                     continue;
199                 case '\t':
200                     retval.append("\\t");
201                     continue;
202                 case '\n':
203                     retval.append("\\n");
204                     continue;
205                 case '\f':
206                     retval.append("\\f");
207                     continue;
208                 case '\r':
209                     retval.append("\\r");
210                     continue;
211                 case '\"':
212                     retval.append("\\\"");
213                     continue;
214                 case '\'':
215                     retval.append("\\\'");
216                     continue;
217                 case '\\':
218                     retval.append("\\\\");
219                     continue;
220                 default:
221                     if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e)
222                     {
223                         String JavaDoc s = "0000" + Integer.toString(ch, 16);
224                         retval.append("\\u" + s.substring(s.length() - 4, s.length()));
225                     }
226                     else
227                     {
228                         retval.append(ch);
229                     }
230                     continue;
231             }
232         }
233         return retval.toString();
234     }
235
236 }
237
Popular Tags