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