001    /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
002    
003    /*      
004            This file was auto generated, but has been modified since then.  If we
005            need to regenerate it for some reason we should be careful to look at
006            the notes below.  
007            
008            All BeanShell modifications are demarcated by "Begin BeanShell 
009            Modification - ... " and "End BeanShell Modification - ..."
010    
011            Note: Please leave the ^M carriage return in the above auto-generated line 
012            or JavaCC will complain about version on Win systems.
013    
014            BeanShell Modification to generated file 
015            ----------------------------------------
016    
017            - Added sourceFile attribute
018                    setErrorSourceFile()
019                    getErrorSourceFile()
020            - Modified getMessage() to print more tersely except on debug
021              (removed "Was expecting one of...)
022            - Added sourceFile info to getMessage()
023            - Made ParseException extend EvalError
024            - Modified constructors to use EvalError
025            - Override 
026                    getErrorLineNumber()
027                    getErrorText()
028            - Add
029                    toString()
030    
031    */
032    
033    package bsh;
034    
035    /**
036     * This exception is thrown when parse errors are encountered.
037     * You can explicitly create objects of this exception type by
038     * calling the method generateParseException in the generated
039     * parser.
040     *
041     * You can modify this class to customize your error reporting
042     * mechanisms so long as you retain the public fields.
043     */
044     
045    // Begin BeanShell Modification - public, extend EvalError
046    public class ParseException extends EvalError {
047    // End BeanShell Modification - public, extend EvalError
048    
049            // Begin BeanShell Modification - sourceFile
050    
051            String sourceFile = "<unknown>";
052    
053            /**
054                    Used to add source file info to exception
055            */
056            public void setErrorSourceFile( String file ) {
057                    this.sourceFile = file;
058            }
059    
060            public String getErrorSourceFile() { 
061                    return sourceFile; 
062            }
063    
064            // End BeanShell Modification - sourceFile
065    
066      /**
067       * This constructor is used by the method "generateParseException"
068       * in the generated parser.  Calling this constructor generates
069       * a new object of this type with the fields "currentToken",
070       * "expectedTokenSequences", and "tokenImage" set.  The boolean
071       * flag "specialConstructor" is also set to true to indicate that
072       * this constructor was used to create this object.
073       * This constructor calls its super class with the empty string
074       * to force the "toString" method of parent class "Throwable" to
075       * print the error message in the form:
076       *     ParseException: <result of getMessage>
077       */
078      public ParseException(Token currentTokenVal,
079                            int[][] expectedTokenSequencesVal,
080                            String[] tokenImageVal
081                           )
082      {
083            // Begin BeanShell Modification - constructor
084            this();
085            // End BeanShell Modification - constructor
086        specialConstructor = true;
087        currentToken = currentTokenVal;
088        expectedTokenSequences = expectedTokenSequencesVal;
089        tokenImage = tokenImageVal;
090      }
091    
092      /**
093       * The following constructors are for use by you for whatever
094       * purpose you can think of.  Constructing the exception in this
095       * manner makes the exception behave in the normal way - i.e., as
096       * documented in the class "Throwable".  The fields "errorToken",
097       * "expectedTokenSequences", and "tokenImage" do not contain
098       * relevant information.  The JavaCC generated code does not use
099       * 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 != null && currentToken.next != null)?
272                currentToken.next.beginLine : super.getErrorLineNumber();
273            }
274    
275            public String getErrorText() { 
276                    // copied from generated getMessage()
277                    int     maxSize = 0;
278                    for     (int i = 0; i <      expectedTokenSequences.length; i++) {
279                      if (maxSize < expectedTokenSequences[i].length)
280                            maxSize = expectedTokenSequences[i].length;
281                    }
282    
283                    String retval = "";
284                    Token tok =     currentToken.next;
285                    for     (int i = 0; i <      maxSize; i++) 
286                    {
287                      if (i != 0) retval += " ";
288                      if (tok.kind == 0) {
289                            retval += tokenImage[0];
290                            break;
291                      }
292                      retval +=     add_escapes(tok.image);
293                      tok = tok.next;
294                    }
295                    
296                    return retval;
297            }
298    
299            public String toString() {
300                    return getMessage();
301            }
302    
303            // End BeanShell Modification - override error methods and toString
304    
305    }