001package jtb;
002
003/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
004/**
005 * This exception is thrown when parse errors are encountered. You can explicitly create objects of
006 * this exception type by calling the method generateParseException in the generated parser.
007 *
008 * <p>You can modify this class to customize your error reporting mechanisms so long as you retain
009 * the public fields.
010 */
011@SuppressWarnings("serial")
012public class ParseException extends Exception {
013  // Added by hand to remove serialize warning.  (markro)
014  static final long serialVersionUID = 20150406L;
015
016  /**
017   * This constructor is used by the method "generateParseException" in the generated parser.
018   * Calling this constructor generates a new object of this type with the fields "currentToken",
019   * "expectedTokenSequences", and "tokenImage" set. The boolean flag "specialConstructor" is also
020   * set to true to indicate that this constructor was used to create this object. This constructor
021   * calls its superclass with the empty string to force the "toString" method of parent class
022   * "Throwable" to print the error message in the form: ParseException: result of getMessage
023   */
024  public ParseException(
025      Token currentTokenVal, int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
026    super("");
027    specialConstructor = true;
028    currentToken = currentTokenVal;
029    expectedTokenSequences = expectedTokenSequencesVal;
030    tokenImage = tokenImageVal;
031  }
032
033  /**
034   * The following constructors are for use by you for whatever purpose you can think of.
035   * Constructing the exception in this manner makes the exception behave in the normal way - i.e.,
036   * as documented in the class "Throwable". The fields "errorToken", "expectedTokenSequences", and
037   * "tokenImage" do not contain relevant information. The JavaCC generated code does not use these
038   * constructors.
039   */
040  public ParseException() {
041    super();
042    specialConstructor = false;
043  }
044
045  public ParseException(String message) {
046    super(message);
047    specialConstructor = false;
048  }
049
050  /**
051   * This variable determines which constructor was used to create this object and thereby affects
052   * the semantics of the "getMessage" method (see below).
053   */
054  protected boolean specialConstructor;
055
056  /**
057   * This is the last token that has been consumed successfully. If this object has been created due
058   * to a parse error, the token followng this token will (therefore) be the first error token.
059   */
060  public Token currentToken;
061
062  /**
063   * Each entry in this array is an array of integers. Each array of integers represents a sequence
064   * of tokens (by their ordinal values) that is expected at this point of the parse.
065   */
066  public int[][] expectedTokenSequences;
067
068  /**
069   * This is a reference to the "tokenImage" array of the generated parser within which the parse
070   * error occurred. This array is defined in the generated ...Constants interface.
071   */
072  public String[] tokenImage;
073
074  /**
075   * This method has the standard behavior when this object has been created using the standard
076   * constructors. Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a
077   * parse error message and returns it. If this object has been created due to a parse error, and
078   * you do not catch it (it gets thrown from the parser), then this method is called during the
079   * printing of the final stack trace, and hence the correct error message gets displayed.
080   */
081  public String getMessage() {
082    if (!specialConstructor) {
083      return super.getMessage();
084    }
085    String expected = "";
086    int maxSize = 0;
087    for (int i = 0; i < expectedTokenSequences.length; i++) {
088      if (maxSize < expectedTokenSequences[i].length) {
089        maxSize = expectedTokenSequences[i].length;
090      }
091      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
092        expected += tokenImage[expectedTokenSequences[i][j]] + " ";
093      }
094      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
095        expected += "...";
096      }
097      expected += eol + "    ";
098    }
099    String retval = "Encountered \"";
100    Token tok = currentToken.next;
101    for (int i = 0; i < maxSize; i++) {
102      if (i != 0) retval += " ";
103      if (tok.kind == 0) {
104        retval += tokenImage[0];
105        break;
106      }
107      retval += add_escapes(tok.image);
108      tok = tok.next;
109    }
110    retval +=
111        "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
112    retval += "." + eol;
113    if (expectedTokenSequences.length == 1) {
114      retval += "Was expecting:" + eol + "    ";
115    } else {
116      retval += "Was expecting one of:" + eol + "    ";
117    }
118    retval += expected;
119    return retval;
120  }
121
122  /** The end of line string for this machine. */
123  protected String eol = System.getProperty("line.separator", "\n");
124
125  /**
126   * Used to convert raw characters to their escaped version when these raw version cannot be used
127   * as part of an ASCII string literal.
128   */
129  protected String add_escapes(String str) {
130    StringBuffer retval = new StringBuffer();
131    char ch;
132    for (int i = 0; i < str.length(); i++) {
133      switch (str.charAt(i)) {
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}