Source for org.w3c.flute.parser.ParseException

   1: /**
   2:  * =========================================
   3:  * LibLayout : a free Java layouting library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://www.jfree.org/liblayout/
   7:  * Project Lead:  Thomas Morgner;
   8:  *
   9:  * (C) Copyright 2006, by Pentaho Corperation and Contributors.
  10:  *
  11:  * This library is free software; you can redistribute it and/or modify it under the terms
  12:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  13:  * either version 2.1 of the License, or (at your option) any later version.
  14:  *
  15:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17:  * See the GNU Lesser General Public License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public License along with this
  20:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21:  * Boston, MA 02111-1307, USA.
  22:  *
  23:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24:  * in the United States and other countries.]
  25:  *
  26:  * ------------
  27:  * ParseException.java
  28:  * ------------
  29:  * (C) Copyright 2006, by Pentaho Corperation.
  30:  *
  31:  * Original Author:  Thomas Morgner;
  32:  * Contributor(s):   -;
  33:  *
  34:  * $Id: ParseException.java,v 1.1.1.1 2006/04/23 14:51:51 taqua Exp $
  35:  *
  36:  * Changes
  37:  * -------
  38:  *
  39:  *
  40:  */
  41: 
  42: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
  43: package org.w3c.flute.parser;
  44: 
  45: import org.w3c.css.sac.CSSException;
  46: 
  47: /**
  48:  * This exception is thrown when parse errors are encountered.
  49:  * You can explicitly create objects of this exception type by
  50:  * calling the method generateParseException in the generated
  51:  * parser.
  52:  *
  53:  * You can modify this class to customize your error reporting
  54:  * mechanisms so long as you retain the public fields.
  55:  */
  56: public class ParseException extends CSSException {
  57: 
  58:   /**
  59:    * This constructor is used by the method "generateParseException"
  60:    * in the generated parser.  Calling this constructor generates
  61:    * a new object of this type with the fields "currentToken",
  62:    * "expectedTokenSequences", and "tokenImage" set.  The boolean
  63:    * flag "specialConstructor" is also set to true to indicate that
  64:    * this constructor was used to create this object.
  65:    * This constructor calls its super class with the empty string
  66:    * to force the "toString" method of parent class "Throwable" to
  67:    * print the error message in the form:
  68:    *     ParseException: <result of getMessage>
  69:    */
  70:   public ParseException(Token currentTokenVal,
  71:                         int[][] expectedTokenSequencesVal,
  72:                         String[] tokenImageVal
  73:                        )
  74:   {
  75:     super("");
  76:     specialConstructor = true;
  77:     currentToken = currentTokenVal;
  78:     expectedTokenSequences = expectedTokenSequencesVal;
  79:     tokenImage = tokenImageVal;
  80:   }
  81: 
  82:   /**
  83:    * The following constructors are for use by you for whatever
  84:    * purpose you can think of.  Constructing the exception in this
  85:    * manner makes the exception behave in the normal way - i.e., as
  86:    * documented in the class "Throwable".  The fields "errorToken",
  87:    * "expectedTokenSequences", and "tokenImage" do not contain
  88:    * relevant information.  The JavaCC generated code does not use
  89:    * these constructors.
  90:    */
  91: 
  92:   public ParseException() {
  93:     super();
  94:     specialConstructor = false;
  95:   }
  96: 
  97:   public ParseException(String message) {
  98:     super(message);
  99:     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: }