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