View Javadoc

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