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