001    /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */
002    /* JavaCCOptions:KEEP_LINE_COL=null */
003    /*
004     *   Copyright (C) Christian Schulte, 2005-206
005     *   All rights reserved.
006     *
007     *   Redistribution and use in source and binary forms, with or without
008     *   modification, are permitted provided that the following conditions
009     *   are met:
010     *
011     *     o Redistributions of source code must retain the above copyright
012     *       notice, this list of conditions and the following disclaimer.
013     *
014     *     o Redistributions in binary form must reproduce the above copyright
015     *       notice, this list of conditions and the following disclaimer in
016     *       the documentation and/or other materials provided with the
017     *       distribution.
018     *
019     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
020     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
021     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
022     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
023     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
024     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
026     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
028     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     *
030     *   $JOMC: VersionParser.jj 3862 2011-10-12 12:37:40Z schulte2005 $
031     *
032     */
033    package org.jomc.util;
034    
035    /**
036     * This exception is thrown when parse errors are encountered.
037     * You can explicitly create objects of this exception type by
038     * calling the method generateParseException in the generated
039     * parser.
040     *
041     * You can modify this class to customize your error reporting
042     * mechanisms so long as you retain the public fields.
043     */
044    public class ParseException extends Exception {
045    
046      /**
047       * The version identifier for this Serializable class.
048       * Increment only if the <i>serialized</i> form of the
049       * class changes.
050       */
051      private static final long serialVersionUID = 1L;
052    
053      /**
054       * This constructor is used by the method "generateParseException"
055       * in the generated parser.  Calling this constructor generates
056       * a new object of this type with the fields "currentToken",
057       * "expectedTokenSequences", and "tokenImage" set.
058       */
059      public ParseException(Token currentTokenVal,
060                            int[][] expectedTokenSequencesVal,
061                            String[] tokenImageVal
062                           )
063      {
064        super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
065        currentToken = currentTokenVal;
066        expectedTokenSequences = expectedTokenSequencesVal;
067        tokenImage = tokenImageVal;
068      }
069    
070      /**
071       * The following constructors are for use by you for whatever
072       * purpose you can think of.  Constructing the exception in this
073       * manner makes the exception behave in the normal way - i.e., as
074       * documented in the class "Throwable".  The fields "errorToken",
075       * "expectedTokenSequences", and "tokenImage" do not contain
076       * relevant information.  The JavaCC generated code does not use
077       * these constructors.
078       */
079    
080      public ParseException() {
081        super();
082      }
083    
084      /** Constructor with message. */
085      public ParseException(String message) {
086        super(message);
087      }
088    
089    
090      /**
091       * This is the last token that has been consumed successfully.  If
092       * this object has been created due to a parse error, the token
093       * followng this token will (therefore) be the first error token.
094       */
095      public Token currentToken;
096    
097      /**
098       * Each entry in this array is an array of integers.  Each array
099       * of integers represents a sequence of tokens (by their ordinal
100       * values) that is expected at this point of the parse.
101       */
102      public int[][] expectedTokenSequences;
103    
104      /**
105       * This is a reference to the "tokenImage" array of the generated
106       * parser within which the parse error occurred.  This array is
107       * defined in the generated ...Constants interface.
108       */
109      public String[] tokenImage;
110    
111      /**
112       * It uses "currentToken" and "expectedTokenSequences" to generate a parse
113       * error message and returns it.  If this object has been created
114       * due to a parse error, and you do not catch it (it gets thrown
115       * from the parser) the correct error message
116       * gets displayed.
117       */
118      private static String initialise(Token currentToken,
119                               int[][] expectedTokenSequences,
120                               String[] tokenImage) {
121        String eol = System.getProperty("line.separator", "\n");
122        StringBuffer expected = new StringBuffer();
123        int maxSize = 0;
124        for (int i = 0; i < expectedTokenSequences.length; i++) {
125          if (maxSize < expectedTokenSequences[i].length) {
126            maxSize = expectedTokenSequences[i].length;
127          }
128          for (int j = 0; j < expectedTokenSequences[i].length; j++) {
129            expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
130          }
131          if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
132            expected.append("...");
133          }
134          expected.append(eol).append("    ");
135        }
136        String retval = "Encountered \"";
137        Token tok = currentToken.next;
138        for (int i = 0; i < maxSize; i++) {
139          if (i != 0) retval += " ";
140          if (tok.kind == 0) {
141            retval += tokenImage[0];
142            break;
143          }
144          retval += " " + tokenImage[tok.kind];
145          retval += " \"";
146          retval += add_escapes(tok.image);
147          retval += " \"";
148          tok = tok.next;
149        }
150        retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
151        retval += "." + eol;
152        if (expectedTokenSequences.length == 1) {
153          retval += "Was expecting:" + eol + "    ";
154        } else {
155          retval += "Was expecting one of:" + eol + "    ";
156        }
157        retval += expected.toString();
158        return retval;
159      }
160    
161      /**
162       * The end of line string for this machine.
163       */
164      protected String eol = System.getProperty("line.separator", "\n");
165    
166      /**
167       * Used to convert raw characters to their escaped version
168       * when these raw version cannot be used as part of an ASCII
169       * string literal.
170       */
171      static String add_escapes(String str) {
172          StringBuffer retval = new StringBuffer();
173          char ch;
174          for (int i = 0; i < str.length(); i++) {
175            switch (str.charAt(i))
176            {
177               case 0 :
178                  continue;
179               case '\b':
180                  retval.append("\\b");
181                  continue;
182               case '\t':
183                  retval.append("\\t");
184                  continue;
185               case '\n':
186                  retval.append("\\n");
187                  continue;
188               case '\f':
189                  retval.append("\\f");
190                  continue;
191               case '\r':
192                  retval.append("\\r");
193                  continue;
194               case '\"':
195                  retval.append("\\\"");
196                  continue;
197               case '\'':
198                  retval.append("\\\'");
199                  continue;
200               case '\\':
201                  retval.append("\\\\");
202                  continue;
203               default:
204                  if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
205                     String s = "0000" + Integer.toString(ch, 16);
206                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
207                  } else {
208                     retval.append(ch);
209                  }
210                  continue;
211            }
212          }
213          return retval.toString();
214       }
215    
216    }
217    /* JavaCC - OriginalChecksum=ccc28d7041bf8d83968f23647b1ce0b1 (do not edit this line) */