PageRenderTime 35ms CodeModel.GetById 25ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/ParseException.java

#
Java | 304 lines | 160 code | 24 blank | 120 comment | 17 complexity | cfd6c534fb65543da05f26fb121dd597 MD5 | raw file
  1/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
  2
  3/* 	
  4	This file was auto generated, but has been modified since then.  If we
  5	need to regenerate it for some reason we should be careful to look at
  6	the notes below.  
  7	
  8	All BeanShell modifications are demarcated by "Begin BeanShell 
  9	Modification - ... " and "End BeanShell Modification - ..."
 10
 11	Note: Please leave the ^M carriage return in the above auto-generated line 
 12	or JavaCC will complain about version on Win systems.
 13
 14	BeanShell Modification to generated file 
 15	----------------------------------------
 16
 17	- Added sourceFile attribute
 18		setErrorSourceFile()
 19		getErrorSourceFile()
 20	- Modified getMessage() to print more tersely except on debug
 21	  (removed "Was expecting one of...)
 22	- Added sourceFile info to getMessage()
 23	- Made ParseException extend EvalError
 24	- Modified constructors to use EvalError
 25	- Override 
 26		getErrorLineNumber()
 27		getErrorText()
 28	- Add
 29		toString()
 30
 31*/
 32
 33package bsh;
 34
 35/**
 36 * This exception is thrown when parse errors are encountered.
 37 * You can explicitly create objects of this exception type by
 38 * calling the method generateParseException in the generated
 39 * parser.
 40 *
 41 * You can modify this class to customize your error reporting
 42 * mechanisms so long as you retain the public fields.
 43 */
 44 
 45// Begin BeanShell Modification - public, extend EvalError
 46public class ParseException extends EvalError {
 47// End BeanShell Modification - public, extend EvalError
 48
 49	// Begin BeanShell Modification - sourceFile
 50
 51	String sourceFile = "<unknown>";
 52
 53	/**
 54		Used to add source file info to exception
 55	*/
 56	public void setErrorSourceFile( String file ) {
 57		this.sourceFile = file;
 58	}
 59
 60	public String getErrorSourceFile() { 
 61		return sourceFile; 
 62	}
 63
 64	// End BeanShell Modification - sourceFile
 65
 66  /**
 67   * This constructor is used by the method "generateParseException"
 68   * in the generated parser.  Calling this constructor generates
 69   * a new object of this type with the fields "currentToken",
 70   * "expectedTokenSequences", and "tokenImage" set.  The boolean
 71   * flag "specialConstructor" is also set to true to indicate that
 72   * this constructor was used to create this object.
 73   * This constructor calls its super class with the empty string
 74   * to force the "toString" method of parent class "Throwable" to
 75   * print the error message in the form:
 76   *     ParseException: <result of getMessage>
 77   */
 78  public ParseException(Token currentTokenVal,
 79                        int[][] expectedTokenSequencesVal,
 80                        String[] tokenImageVal
 81                       )
 82  {
 83	// Begin BeanShell Modification - constructor
 84	this();
 85	// End BeanShell Modification - constructor
 86    specialConstructor = true;
 87    currentToken = currentTokenVal;
 88    expectedTokenSequences = expectedTokenSequencesVal;
 89    tokenImage = tokenImageVal;
 90  }
 91
 92  /**
 93   * The following constructors are for use by you for whatever
 94   * purpose you can think of.  Constructing the exception in this
 95   * manner makes the exception behave in the normal way - i.e., as
 96   * documented in the class "Throwable".  The fields "errorToken",
 97   * "expectedTokenSequences", and "tokenImage" do not contain
 98   * relevant information.  The JavaCC generated code does not use
 99   * these constructors.
100   */
101
102  public ParseException() {
103	// Begin BeanShell Modification - constructor
104	this("");
105	// End BeanShell Modification - constructor
106    specialConstructor = false;
107  }
108
109  public ParseException(String message) {
110	// Begin BeanShell Modification - super constructor args
111	// null node, null callstack, ParseException knows where the error is.
112	super( message, null, null );
113	// End BeanShell Modification - super constructor args
114    specialConstructor = false;
115  }
116
117  /**
118   * This variable determines which constructor was used to create
119   * this object and thereby affects the semantics of the
120   * "getMessage" method (see below).
121   */
122  protected boolean specialConstructor;
123
124  /**
125   * This is the last token that has been consumed successfully.  If
126   * this object has been created due to a parse error, the token
127   * followng this token will (therefore) be the first error token.
128   */
129  public Token currentToken;
130
131  /**
132   * Each entry in this array is an array of integers.  Each array
133   * of integers represents a sequence of tokens (by their ordinal
134   * values) that is expected at this point of the parse.
135   */
136  public int[][] expectedTokenSequences;
137
138  /**
139   * This is a reference to the "tokenImage" array of the generated
140   * parser within which the parse error occurred.  This array is
141   * defined in the generated ...Constants interface.
142   */
143  public String[] tokenImage;
144
145  // Begin BeanShell Modification - moved body to overloaded getMessage()
146  public String	getMessage() {
147	return getMessage( false );
148  }
149  // End BeanShell Modification - moved body to overloaded getMessage()
150
151  /**
152   * This method has the standard behavior when this object has been
153   * created using the standard constructors.  Otherwise, it uses
154   * "currentToken" and "expectedTokenSequences" to generate a parse
155   * error message and returns it.  If this object has been created
156   * due to a parse error, and you do not catch it (it gets thrown
157   * from the parser), then this method is called during the printing
158   * of the final stack trace, and hence the correct error message
159   * gets displayed.
160   */
161  // Begin BeanShell Modification - added debug param
162  public String getMessage( boolean debug ) {
163  // End BeanShell Modification - added debug param
164    if (!specialConstructor) {
165      return super.getMessage();
166    }
167    String expected = "";
168    int maxSize = 0;
169    for (int i = 0; i < expectedTokenSequences.length; i++) {
170      if (maxSize < expectedTokenSequences[i].length) {
171        maxSize = expectedTokenSequences[i].length;
172      }
173      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
174        expected += tokenImage[expectedTokenSequences[i][j]] + " ";
175      }
176      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
177        expected += "...";
178      }
179      expected += eol + "    ";
180    }
181	// Begin BeanShell Modification - added sourceFile info
182    String retval = "In file: "+ sourceFile +" Encountered \"";
183	// End BeanShell Modification - added sourceFile info
184    Token tok = currentToken.next;
185    for (int i = 0; i < maxSize; i++) {
186      if (i != 0) retval += " ";
187      if (tok.kind == 0) {
188        retval += tokenImage[0];
189        break;
190      }
191      retval += add_escapes(tok.image);
192      tok = tok.next; 
193    }
194    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
195
196	// Begin BeanShell Modification - made conditional on debug
197	if ( debug )
198	{
199		if (expectedTokenSequences.length == 1) {
200		  retval += "Was expecting:" + eol + "    ";
201		} else {
202		  retval += "Was expecting one of:" + eol + "    ";
203		}
204
205		retval += expected;
206	}
207	// End BeanShell Modification - made conditional on debug
208
209    return retval;
210  }
211
212  /**
213   * The end of line string for this machine.
214   */
215  protected String eol = System.getProperty("line.separator", "\n");
216 
217  /**
218   * Used to convert raw characters to their escaped version
219   * when these raw version cannot be used as part of an ASCII
220   * string literal.
221   */
222  protected String add_escapes(String str) {
223      StringBuffer retval = new StringBuffer();
224      char ch;
225      for (int i = 0; i < str.length(); i++) {
226        switch (str.charAt(i))
227        {
228           case 0 :
229              continue;
230           case '\b':
231              retval.append("\\b");
232              continue;
233           case '\t':
234              retval.append("\\t");
235              continue;
236           case '\n':
237              retval.append("\\n");
238              continue;
239           case '\f':
240              retval.append("\\f");
241              continue;
242           case '\r':
243              retval.append("\\r");
244              continue;
245           case '\"':
246              retval.append("\\\"");
247              continue;
248           case '\'':
249              retval.append("\\\'");
250              continue;
251           case '\\':
252              retval.append("\\\\");
253              continue;
254           default:
255              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
256                 String s = "0000" + Integer.toString(ch, 16);
257                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
258              } else {
259                 retval.append(ch);
260              }
261              continue;
262        }
263      }
264      return retval.toString();
265   }
266
267	// Begin BeanShell Modification - override error methods and toString
268
269	public int getErrorLineNumber() 
270	{
271		return currentToken.next.beginLine;
272	}
273
274	public String getErrorText() { 
275		// copied from generated getMessage()
276		int	maxSize	= 0;
277		for	(int i = 0; i <	expectedTokenSequences.length; i++) {
278		  if (maxSize < expectedTokenSequences[i].length)
279			maxSize	= expectedTokenSequences[i].length;
280		}
281
282		String retval = "";
283		Token tok =	currentToken.next;
284		for	(int i = 0; i <	maxSize; i++) 
285		{
286		  if (i != 0) retval += " ";
287		  if (tok.kind == 0) {
288			retval += tokenImage[0];
289			break;
290		  }
291		  retval +=	add_escapes(tok.image);
292		  tok = tok.next;
293		}
294		
295		return retval;
296	}
297
298	public String toString() {
299		return getMessage();
300	}
301
302	// End BeanShell Modification - override error methods and toString
303
304}