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