PageRenderTime 30ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 13ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/ParseException.java

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