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