1   /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 7.0 */
2   /* JavaCCOptions:KEEP_LINE_COLUMN=true */
3   package jminusminus;
4   
5   /**
6    * This exception is thrown when parse errors are encountered.
7    * You can explicitly create objects of this exception type by
8    * calling the method generateParseException in the generated
9    * parser.
10   *
11   * You can modify this class to customize your error reporting
12   * mechanisms so long as you retain the public fields.
13   */
14  public class ParseException extends Exception {
15  
16    /**
17     * The version identifier for this Serializable class.
18     * Increment only if the <i>serialized</i> form of the
19     * class changes.
20     */
21    private static final long serialVersionUID = 1L;
22  
23    /**
24     * The end of line string for this machine.
25     */
26    protected static String EOL = System.getProperty("line.separator", "\n");
27  
28    /**
29     * This constructor is used by the method "generateParseException"
30     * in the generated parser.  Calling this constructor generates
31     * a new object of this type with the fields "currentToken",
32     * "expectedTokenSequences", and "tokenImage" set.
33     */
34    public ParseException(Token currentTokenVal,
35                          int[][] expectedTokenSequencesVal,
36                          String[] tokenImageVal
37                         )
38    {
39      super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
40      currentToken = currentTokenVal;
41      expectedTokenSequences = expectedTokenSequencesVal;
42      tokenImage = tokenImageVal;
43    }
44  
45    /**
46     * The following constructors are for use by you for whatever
47     * purpose you can think of.  Constructing the exception in this
48     * manner makes the exception behave in the normal way - i.e., as
49     * documented in the class "Throwable".  The fields "errorToken",
50     * "expectedTokenSequences", and "tokenImage" do not contain
51     * relevant information.  The JavaCC generated code does not use
52     * these constructors.
53     */
54  
55    public ParseException() {
56      super();
57    }
58  
59    /** Constructor with message. */
60    public ParseException(String message) {
61      super(message);
62    }
63  
64  
65    /**
66     * This is the last token that has been consumed successfully.  If
67     * this object has been created due to a parse error, the token
68     * followng this token will (therefore) be the first error token.
69     */
70    public Token currentToken;
71  
72    /**
73     * Each entry in this array is an array of integers.  Each array
74     * of integers represents a sequence of tokens (by their ordinal
75     * values) that is expected at this point of the parse.
76     */
77    public int[][] expectedTokenSequences;
78  
79    /**
80     * This is a reference to the "tokenImage" array of the generated
81     * parser within which the parse error occurred.  This array is
82     * defined in the generated ...Constants interface.
83     */
84    public String[] tokenImage;
85  
86    /**
87     * It uses "currentToken" and "expectedTokenSequences" to generate a parse
88     * error message and returns it.  If this object has been created
89     * due to a parse error, and you do not catch it (it gets thrown
90     * from the parser) the correct error message
91     * gets displayed.
92     */
93    private static String initialise(Token currentToken,
94                             int[][] expectedTokenSequences,
95                             String[] tokenImage) {
96  
97      StringBuffer expected = new StringBuffer();
98      int maxSize = 0;
99      for (int i = 0; i < expectedTokenSequences.length; i++) {
100       if (maxSize < expectedTokenSequences[i].length) {
101         maxSize = expectedTokenSequences[i].length;
102       }
103       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
104         expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
105       }
106       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
107         expected.append("...");
108       }
109       expected.append(EOL).append("    ");
110     }
111     String retval = "Encountered \"";
112     Token tok = currentToken.next;
113     for (int i = 0; i < maxSize; i++) {
114       if (i != 0) retval += " ";
115       if (tok.kind == 0) {
116         retval += tokenImage[0];
117         break;
118       }
119       retval += " " + tokenImage[tok.kind];
120       retval += " \"";
121       retval += add_escapes(tok.image);
122       retval += " \"";
123       tok = tok.next;
124     }
125     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
126     retval += "." + EOL;
127     
128     
129     if (expectedTokenSequences.length == 0) {
130         // Nothing to add here
131     } else {
132         if (expectedTokenSequences.length == 1) {
133           retval += "Was expecting:" + EOL + "    ";
134         } else {
135           retval += "Was expecting one of:" + EOL + "    ";
136         }
137         retval += expected.toString();
138     }
139     
140     return retval;
141   }
142 
143 
144   /**
145    * Used to convert raw characters to their escaped version
146    * when these raw version cannot be used as part of an ASCII
147    * string literal.
148    */
149   static String add_escapes(String str) {
150       StringBuffer retval = new StringBuffer();
151       char ch;
152       for (int i = 0; i < str.length(); i++) {
153         switch (str.charAt(i))
154         {
155            case '\b':
156               retval.append("\\b");
157               continue;
158            case '\t':
159               retval.append("\\t");
160               continue;
161            case '\n':
162               retval.append("\\n");
163               continue;
164            case '\f':
165               retval.append("\\f");
166               continue;
167            case '\r':
168               retval.append("\\r");
169               continue;
170            case '\"':
171               retval.append("\\\"");
172               continue;
173            case '\'':
174               retval.append("\\\'");
175               continue;
176            case '\\':
177               retval.append("\\\\");
178               continue;
179            default:
180               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
181                  String s = "0000" + Integer.toString(ch, 16);
182                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
183               } else {
184                  retval.append(ch);
185               }
186               continue;
187         }
188       }
189       return retval.toString();
190    }
191 
192 }
193 /* JavaCC - OriginalChecksum=f012ebdef633cce027f75f33e7e8b64b (do not edit this line) */
194