View Javadoc

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