001    /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
002    /* JavaCCOptions:KEEP_LINE_COL=null */
003    /**
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    package org.apache.activemq.selector;
021    
022    /**
023     * This exception is thrown when parse errors are encountered.
024     * You can explicitly create objects of this exception type by
025     * calling the method generateParseException in the generated
026     * parser.
027     *
028     * You can modify this class to customize your error reporting
029     * mechanisms so long as you retain the public fields.
030     */
031    public class ParseException extends Exception {
032    
033      /**
034       * The version identifier for this Serializable class.
035       * Increment only if the <i>serialized</i> form of the
036       * class changes.
037       */
038      private static final long serialVersionUID = 1L;
039    
040      /**
041       * This constructor is used by the method "generateParseException"
042       * in the generated parser.  Calling this constructor generates
043       * a new object of this type with the fields "currentToken",
044       * "expectedTokenSequences", and "tokenImage" set.
045       */
046      public ParseException(Token currentTokenVal,
047                            int[][] expectedTokenSequencesVal,
048                            String[] tokenImageVal
049                           )
050      {
051        super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
052        currentToken = currentTokenVal;
053        expectedTokenSequences = expectedTokenSequencesVal;
054        tokenImage = tokenImageVal;
055      }
056    
057      /**
058       * The following constructors are for use by you for whatever
059       * purpose you can think of.  Constructing the exception in this
060       * manner makes the exception behave in the normal way - i.e., as
061       * documented in the class "Throwable".  The fields "errorToken",
062       * "expectedTokenSequences", and "tokenImage" do not contain
063       * relevant information.  The JavaCC generated code does not use
064       * these constructors.
065       */
066    
067      public ParseException() {
068        super();
069      }
070    
071      /** Constructor with message. */
072      public ParseException(String message) {
073        super(message);
074      }
075    
076    
077      /**
078       * This is the last token that has been consumed successfully.  If
079       * this object has been created due to a parse error, the token
080       * followng this token will (therefore) be the first error token.
081       */
082      public Token currentToken;
083    
084      /**
085       * Each entry in this array is an array of integers.  Each array
086       * of integers represents a sequence of tokens (by their ordinal
087       * values) that is expected at this point of the parse.
088       */
089      public int[][] expectedTokenSequences;
090    
091      /**
092       * This is a reference to the "tokenImage" array of the generated
093       * parser within which the parse error occurred.  This array is
094       * defined in the generated ...Constants interface.
095       */
096      public String[] tokenImage;
097    
098      /**
099       * It uses "currentToken" and "expectedTokenSequences" to generate a parse
100       * error message and returns it.  If this object has been created
101       * due to a parse error, and you do not catch it (it gets thrown
102       * from the parser) the correct error message
103       * gets displayed.
104       */
105      private static String initialise(Token currentToken,
106                               int[][] expectedTokenSequences,
107                               String[] tokenImage) {
108        String eol = System.getProperty("line.separator", "\n");
109        StringBuffer expected = new StringBuffer();
110        int maxSize = 0;
111        for (int i = 0; i < expectedTokenSequences.length; i++) {
112          if (maxSize < expectedTokenSequences[i].length) {
113            maxSize = expectedTokenSequences[i].length;
114          }
115          for (int j = 0; j < expectedTokenSequences[i].length; j++) {
116            expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
117          }
118          if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
119            expected.append("...");
120          }
121          expected.append(eol).append("    ");
122        }
123        String retval = "Encountered \"";
124        Token tok = currentToken.next;
125        for (int i = 0; i < maxSize; i++) {
126          if (i != 0) retval += " ";
127          if (tok.kind == 0) {
128            retval += tokenImage[0];
129            break;
130          }
131          retval += " " + tokenImage[tok.kind];
132          retval += " \"";
133          retval += add_escapes(tok.image);
134          retval += " \"";
135          tok = tok.next;
136        }
137        retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
138        retval += "." + eol;
139        if (expectedTokenSequences.length == 1) {
140          retval += "Was expecting:" + eol + "    ";
141        } else {
142          retval += "Was expecting one of:" + eol + "    ";
143        }
144        retval += expected.toString();
145        return retval;
146      }
147    
148      /**
149       * The end of line string for this machine.
150       */
151      protected String eol = System.getProperty("line.separator", "\n");
152    
153      /**
154       * Used to convert raw characters to their escaped version
155       * when these raw version cannot be used as part of an ASCII
156       * string literal.
157       */
158      static String add_escapes(String str) {
159          StringBuffer retval = new StringBuffer();
160          char ch;
161          for (int i = 0; i < str.length(); i++) {
162            switch (str.charAt(i))
163            {
164               case 0 :
165                  continue;
166               case '\b':
167                  retval.append("\\b");
168                  continue;
169               case '\t':
170                  retval.append("\\t");
171                  continue;
172               case '\n':
173                  retval.append("\\n");
174                  continue;
175               case '\f':
176                  retval.append("\\f");
177                  continue;
178               case '\r':
179                  retval.append("\\r");
180                  continue;
181               case '\"':
182                  retval.append("\\\"");
183                  continue;
184               case '\'':
185                  retval.append("\\\'");
186                  continue;
187               case '\\':
188                  retval.append("\\\\");
189                  continue;
190               default:
191                  if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
192                     String s = "0000" + Integer.toString(ch, 16);
193                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
194                  } else {
195                     retval.append(ch);
196                  }
197                  continue;
198            }
199          }
200          return retval.toString();
201       }
202    
203    }
204    /* JavaCC - OriginalChecksum=b9214f06b2c752c7be8d5d26aa9e434d (do not edit this line) */