001    /*
002     * Cobertura - http://cobertura.sourceforge.net/
003     *
004     * This file was taken from JavaNCSS
005     * http://www.kclee.com/clemens/java/javancss/
006     * Copyright (C) 2000 Chr. Clemens Lee <clemens a.t kclee d.o.t com>
007     *
008     * Cobertura is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License as published
010     * by the Free Software Foundation; either version 2 of the License,
011     * or (at your option) any later version.
012     *
013     * Cobertura is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     * General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with Cobertura; if not, write to the Free Software
020     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021     * USA
022     */
023    
024    
025    /*
026     *
027     * WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING  
028     *
029     * WARNING TO COBERTURA DEVELOPERS
030     *
031     * DO NOT MODIFY THIS FILE!
032     *
033     * MODIFY THE FILES UNDER THE JAVANCSS DIRECTORY LOCATED AT THE ROOT OF THE COBERTURA PROJECT.
034     *
035     * FOLLOW THE PROCEDURE FOR MERGING THE LATEST JAVANCSS INTO COBERTURA LOCATED AT
036     * javancss/coberturaREADME.txt
037     *
038     * WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   WARNING   
039     */
040    /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 4.1 */
041    /* JavaCCOptions:KEEP_LINE_COL=null */
042    //cobertura - put the import on its own line - otherwise, it messes up the script that changes the package.
043    package net.sourceforge.cobertura.javancss.parser.debug;
044    
045    /**
046     * This exception is thrown when parse errors are encountered.
047     * You can explicitly create objects of this exception type by
048     * calling the method generateParseException in the generated
049     * parser.
050     *
051     * You can modify this class to customize your error reporting
052     * mechanisms so long as you retain the public fields.
053     */
054    public class ParseException extends Exception {
055    
056      /**
057       * This constructor is used by the method "generateParseException"
058       * in the generated parser.  Calling this constructor generates
059       * a new object of this type with the fields "currentToken",
060       * "expectedTokenSequences", and "tokenImage" set.  The boolean
061       * flag "specialConstructor" is also set to true to indicate that
062       * this constructor was used to create this object.
063       * This constructor calls its super class with the empty string
064       * to force the "toString" method of parent class "Throwable" to
065       * print the error message in the form:
066       *     ParseException: <result of getMessage>
067       */
068      public ParseException(Token currentTokenVal,
069                            int[][] expectedTokenSequencesVal,
070                            String[] tokenImageVal
071                           )
072      {
073        super("");
074        specialConstructor = true;
075        currentToken = currentTokenVal;
076        expectedTokenSequences = expectedTokenSequencesVal;
077        tokenImage = tokenImageVal;
078      }
079    
080      /**
081       * The following constructors are for use by you for whatever
082       * purpose you can think of.  Constructing the exception in this
083       * manner makes the exception behave in the normal way - i.e., as
084       * documented in the class "Throwable".  The fields "errorToken",
085       * "expectedTokenSequences", and "tokenImage" do not contain
086       * relevant information.  The JavaCC generated code does not use
087       * these constructors.
088       */
089    
090      public ParseException() {
091        super();
092        specialConstructor = false;
093      }
094    
095      /** Constructor with message. */
096      public ParseException(String message) {
097        super(message);
098        specialConstructor = false;
099      }
100    
101      /**
102       * This variable determines which constructor was used to create
103       * this object and thereby affects the semantics of the
104       * "getMessage" method (see below).
105       */
106      protected boolean specialConstructor;
107    
108      /**
109       * This is the last token that has been consumed successfully.  If
110       * this object has been created due to a parse error, the token
111       * followng this token will (therefore) be the first error token.
112       */
113      public Token currentToken;
114    
115      /**
116       * Each entry in this array is an array of integers.  Each array
117       * of integers represents a sequence of tokens (by their ordinal
118       * values) that is expected at this point of the parse.
119       */
120      public int[][] expectedTokenSequences;
121    
122      /**
123       * This is a reference to the "tokenImage" array of the generated
124       * parser within which the parse error occurred.  This array is
125       * defined in the generated ...Constants interface.
126       */
127      public String[] tokenImage;
128    
129      /**
130       * This method has the standard behavior when this object has been
131       * created using the standard constructors.  Otherwise, it uses
132       * "currentToken" and "expectedTokenSequences" to generate a parse
133       * error message and returns it.  If this object has been created
134       * due to a parse error, and you do not catch it (it gets thrown
135       * from the parser), then this method is called during the printing
136       * of the final stack trace, and hence the correct error message
137       * gets displayed.
138       */
139      public String getMessage() {
140        if (!specialConstructor) {
141          return super.getMessage();
142        }
143        StringBuffer expected = new StringBuffer();
144        int maxSize = 0;
145        for (int i = 0; i < expectedTokenSequences.length; i++) {
146          if (maxSize < expectedTokenSequences[i].length) {
147            maxSize = expectedTokenSequences[i].length;
148          }
149          for (int j = 0; j < expectedTokenSequences[i].length; j++) {
150            expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
151          }
152          if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
153            expected.append("...");
154          }
155          expected.append(eol).append("    ");
156        }
157        String retval = "Encountered \"";
158        Token tok = currentToken.next;
159        for (int i = 0; i < maxSize; i++) {
160          if (i != 0) retval += " ";
161          if (tok.kind == 0) {
162            retval += tokenImage[0];
163            break;
164          }
165          retval += " " + tokenImage[tok.kind];
166          retval += " \"";
167          retval += add_escapes(tok.image);
168          retval += " \"";
169          tok = tok.next;
170        }
171        retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
172        retval += "." + eol;
173        if (expectedTokenSequences.length == 1) {
174          retval += "Was expecting:" + eol + "    ";
175        } else {
176          retval += "Was expecting one of:" + eol + "    ";
177        }
178        retval += expected.toString();
179        return retval;
180      }
181    
182      /**
183       * The end of line string for this machine.
184       */
185      protected String eol = System.getProperty("line.separator", "\n");
186    
187      /**
188       * Used to convert raw characters to their escaped version
189       * when these raw version cannot be used as part of an ASCII
190       * string literal.
191       */
192      protected String add_escapes(String str) {
193          StringBuffer retval = new StringBuffer();
194          char ch;
195          for (int i = 0; i < str.length(); i++) {
196            switch (str.charAt(i))
197            {
198               case 0 :
199                  continue;
200               case '\b':
201                  retval.append("\\b");
202                  continue;
203               case '\t':
204                  retval.append("\\t");
205                  continue;
206               case '\n':
207                  retval.append("\\n");
208                  continue;
209               case '\f':
210                  retval.append("\\f");
211                  continue;
212               case '\r':
213                  retval.append("\\r");
214                  continue;
215               case '\"':
216                  retval.append("\\\"");
217                  continue;
218               case '\'':
219                  retval.append("\\\'");
220                  continue;
221               case '\\':
222                  retval.append("\\\\");
223                  continue;
224               default:
225                  if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
226                     String s = "0000" + Integer.toString(ch, 16);
227                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
228                  } else {
229                     retval.append(ch);
230                  }
231                  continue;
232            }
233          }
234          return retval.toString();
235       }
236    
237    }
238    /* JavaCC - OriginalChecksum=1dc25ef3bbdc3cc4c510b31013f38e78 (do not edit this line) */