KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > factory > duplicate > ast > ParseException


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.annotation.factory.duplicate.ast;
23
24 /**
25  * This exception is thrown when parse errors are encountered.
26  * You can explicitly create objects of this exception type by
27  * calling the method generateParseException in the generated
28  * parser.
29  *
30  * You can modify this class to customize your error reporting
31  * mechanisms so long as you retain the public fields.
32  */

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

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

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

86   protected boolean specialConstructor;
87
88   /**
89    * This is the last token that has been consumed successfully. If
90    * this object has been created due to a parse error, the token
91    * followng this token will (therefore) be the first error token.
92    */

93   public Token currentToken;
94
95   /**
96    * Each entry in this array is an array of integers. Each array
97    * of integers represents a sequence of tokens (by their ordinal
98    * values) that is expected at this point of the parse.
99    */

100   public int[][] expectedTokenSequences;
101
102   /**
103    * This is a reference to the "tokenImage" array of the generated
104    * parser within which the parse error occurred. This array is
105    * defined in the generated ...Constants interface.
106    */

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

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

162   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
163  
164   /**
165    * Used to convert raw characters to their escaped version
166    * when these raw version cannot be used as part of an ASCII
167    * string literal.
168    */

169   protected String JavaDoc add_escapes(String JavaDoc str) {
170       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
171       char ch;
172       for (int i = 0; i < str.length(); i++) {
173         switch (str.charAt(i))
174         {
175            case 0 :
176               continue;
177            case '\b':
178               retval.append("\\b");
179               continue;
180            case '\t':
181               retval.append("\\t");
182               continue;
183            case '\n':
184               retval.append("\\n");
185               continue;
186            case '\f':
187               retval.append("\\f");
188               continue;
189            case '\r':
190               retval.append("\\r");
191               continue;
192            case '\"':
193               retval.append("\\\"");
194               continue;
195            case '\'':
196               retval.append("\\\'");
197               continue;
198            case '\\':
199               retval.append("\\\\");
200               continue;
201            default:
202               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
203                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
204                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
205               } else {
206                  retval.append(ch);
207               }
208               continue;
209         }
210       }
211       return retval.toString();
212    }
213
214 }
215
Popular Tags