ParseException.java

  1. /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
  2. /* JavaCCOptions:KEEP_LINE_COL=null */
  3. package org.apache.commons.configuration2.plist;

  4. /**
  5.  * This exception is thrown when parse errors are encountered.
  6.  * You can explicitly create objects of this exception type by
  7.  * calling the method generateParseException in the generated
  8.  * parser.
  9.  *
  10.  * You can modify this class to customize your error reporting
  11.  * mechanisms so long as you retain the public fields.
  12.  */
  13. public class ParseException extends Exception {

  14.   /**
  15.    * The version identifier for this Serializable class.
  16.    * Increment only if the <i>serialized</i> form of the
  17.    * class changes.
  18.    */
  19.   private static final long serialVersionUID = 1L;

  20.   /**
  21.    * This constructor is used by the method "generateParseException"
  22.    * in the generated parser.  Calling this constructor generates
  23.    * a new object of this type with the fields "currentToken",
  24.    * "expectedTokenSequences", and "tokenImage" set.
  25.    */
  26.   public ParseException(Token currentTokenVal,
  27.                         int[][] expectedTokenSequencesVal,
  28.                         String[] tokenImageVal
  29.                        )
  30.   {
  31.     super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
  32.     currentToken = currentTokenVal;
  33.     expectedTokenSequences = expectedTokenSequencesVal;
  34.     tokenImage = tokenImageVal;
  35.   }

  36.   /**
  37.    * The following constructors are for use by you for whatever
  38.    * purpose you can think of.  Constructing the exception in this
  39.    * manner makes the exception behave in the normal way - i.e., as
  40.    * documented in the class "Throwable".  The fields "errorToken",
  41.    * "expectedTokenSequences", and "tokenImage" do not contain
  42.    * relevant information.  The JavaCC generated code does not use
  43.    * these constructors.
  44.    */

  45.   public ParseException() {
  46.     super();
  47.   }

  48.   /** Constructor with message. */
  49.   public ParseException(String message) {
  50.     super(message);
  51.   }


  52.   /**
  53.    * This is the last token that has been consumed successfully.  If
  54.    * this object has been created due to a parse error, the token
  55.    * followng this token will (therefore) be the first error token.
  56.    */
  57.   public Token currentToken;

  58.   /**
  59.    * Each entry in this array is an array of integers.  Each array
  60.    * of integers represents a sequence of tokens (by their ordinal
  61.    * values) that is expected at this point of the parse.
  62.    */
  63.   public int[][] expectedTokenSequences;

  64.   /**
  65.    * This is a reference to the "tokenImage" array of the generated
  66.    * parser within which the parse error occurred.  This array is
  67.    * defined in the generated ...Constants interface.
  68.    */
  69.   public String[] tokenImage;

  70.   /**
  71.    * It uses "currentToken" and "expectedTokenSequences" to generate a parse
  72.    * error message and returns it.  If this object has been created
  73.    * due to a parse error, and you do not catch it (it gets thrown
  74.    * from the parser) the correct error message
  75.    * gets displayed.
  76.    */
  77.   private static String initialise(Token currentToken,
  78.                            int[][] expectedTokenSequences,
  79.                            String[] tokenImage) {
  80.     String eol = System.getProperty("line.separator", "\n");
  81.     StringBuffer expected = new StringBuffer();
  82.     int maxSize = 0;
  83.     for (int i = 0; i < expectedTokenSequences.length; i++) {
  84.       if (maxSize < expectedTokenSequences[i].length) {
  85.         maxSize = expectedTokenSequences[i].length;
  86.       }
  87.       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
  88.         expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
  89.       }
  90.       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
  91.         expected.append("...");
  92.       }
  93.       expected.append(eol).append("    ");
  94.     }
  95.     String retval = "Encountered \"";
  96.     Token tok = currentToken.next;
  97.     for (int i = 0; i < maxSize; i++) {
  98.       if (i != 0) retval += " ";
  99.       if (tok.kind == 0) {
  100.         retval += tokenImage[0];
  101.         break;
  102.       }
  103.       retval += " " + tokenImage[tok.kind];
  104.       retval += " \"";
  105.       retval += add_escapes(tok.image);
  106.       retval += " \"";
  107.       tok = tok.next;
  108.     }
  109.     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
  110.     retval += "." + eol;
  111.     if (expectedTokenSequences.length == 1) {
  112.       retval += "Was expecting:" + eol + "    ";
  113.     } else {
  114.       retval += "Was expecting one of:" + eol + "    ";
  115.     }
  116.     retval += expected.toString();
  117.     return retval;
  118.   }

  119.   /**
  120.    * The end of line string for this machine.
  121.    */
  122.   protected String eol = System.getProperty("line.separator", "\n");

  123.   /**
  124.    * Used to convert raw characters to their escaped version
  125.    * when these raw version cannot be used as part of an ASCII
  126.    * string literal.
  127.    */
  128.   static String add_escapes(String str) {
  129.       StringBuffer retval = new StringBuffer();
  130.       char ch;
  131.       for (int i = 0; i < str.length(); i++) {
  132.         switch (str.charAt(i))
  133.         {
  134.            case 0 :
  135.               continue;
  136.            case '\b':
  137.               retval.append("\\b");
  138.               continue;
  139.            case '\t':
  140.               retval.append("\\t");
  141.               continue;
  142.            case '\n':
  143.               retval.append("\\n");
  144.               continue;
  145.            case '\f':
  146.               retval.append("\\f");
  147.               continue;
  148.            case '\r':
  149.               retval.append("\\r");
  150.               continue;
  151.            case '\"':
  152.               retval.append("\\\"");
  153.               continue;
  154.            case '\'':
  155.               retval.append("\\\'");
  156.               continue;
  157.            case '\\':
  158.               retval.append("\\\\");
  159.               continue;
  160.            default:
  161.               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
  162.                  String s = "0000" + Integer.toString(ch, 16);
  163.                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
  164.               } else {
  165.                  retval.append(ch);
  166.               }
  167.               continue;
  168.         }
  169.       }
  170.       return retval.toString();
  171.    }

  172. }
  173. /* JavaCC - OriginalChecksum=511e60e8dc5275c2349a384d892ee0da (do not edit this line) */