~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/text-edit/src/org/netbeans/modules/xml/text/syntax/javacc/ParseException.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
 
2
/*
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
4
 *
 
5
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
6
 *
 
7
 * The contents of this file are subject to the terms of either the GNU
 
8
 * General Public License Version 2 only ("GPL") or the Common
 
9
 * Development and Distribution License("CDDL") (collectively, the
 
10
 * "License"). You may not use this file except in compliance with the
 
11
 * License. You can obtain a copy of the License at
 
12
 * http://www.netbeans.org/cddl-gplv2.html
 
13
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
14
 * specific language governing permissions and limitations under the
 
15
 * License.  When distributing the software, include this License Header
 
16
 * Notice in each file and include the License file at
 
17
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
18
 * particular file as subject to the "Classpath" exception as provided
 
19
 * by Sun in the GPL Version 2 section of the License file that
 
20
 * accompanied this code. If applicable, add the following below the
 
21
 * License Header, with the fields enclosed by brackets [] replaced by
 
22
 * your own identifying information:
 
23
 * "Portions Copyrighted [year] [name of copyright owner]"
 
24
 *
 
25
 * Contributor(s):
 
26
 *
 
27
 * The Original Software is NetBeans. The Initial Developer of the Original
 
28
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
29
 * Microsystems, Inc. All Rights Reserved.
 
30
 *
 
31
 * If you wish your version of this file to be governed by only the CDDL
 
32
 * or only the GPL Version 2, indicate your decision by adding
 
33
 * "[Contributor] elects to include this software in this distribution
 
34
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
35
 * single choice of license, a recipient has the option to distribute
 
36
 * your version of this file under either the CDDL, the GPL Version 2 or
 
37
 * to extend the choice of license to its licensees as provided above.
 
38
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
39
 * Version 2 license, then the option applies only if the new code is
 
40
 * made subject to such option by the copyright holder.
 
41
 */
 
42
 
 
43
package org.netbeans.modules.xml.text.syntax.javacc;
 
44
 
 
45
/**
 
46
 * This exception is thrown when parse errors are encountered.
 
47
 * You can explicitly create objects of this exception type by
 
48
 * calling the method generateParseException in the generated
 
49
 * parser.
 
50
 *
 
51
 * You can modify this class to customize your error reporting
 
52
 * mechanisms so long as you retain the public fields.
 
53
 */
 
54
public class ParseException extends Exception {
 
55
 
 
56
  /**
 
57
   * This constructor is used by the method "generateParseException"
 
58
   * in the generated parser.  Calling this constructor generates
 
59
   * a new object of this type with the fields "currentToken",
 
60
   * "expectedTokenSequences", and "tokenImage" set.  The boolean
 
61
   * flag "specialConstructor" is also set to true to indicate that
 
62
   * this constructor was used to create this object.
 
63
   * This constructor calls its super class with the empty string
 
64
   * to force the "toString" method of parent class "Throwable" to
 
65
   * print the error message in the form:
 
66
   *     ParseException: <result of getMessage>
 
67
   */
 
68
  public ParseException(Token currentTokenVal,
 
69
                        int[][] expectedTokenSequencesVal,
 
70
                        String[] tokenImageVal
 
71
                       )
 
72
  {
 
73
    super("");
 
74
    specialConstructor = true;
 
75
    currentToken = currentTokenVal;
 
76
    expectedTokenSequences = expectedTokenSequencesVal;
 
77
    tokenImage = tokenImageVal;
 
78
  }
 
79
 
 
80
  /**
 
81
   * The following constructors are for use by you for whatever
 
82
   * purpose you can think of.  Constructing the exception in this
 
83
   * manner makes the exception behave in the normal way - i.e., as
 
84
   * documented in the class "Throwable".  The fields "errorToken",
 
85
   * "expectedTokenSequences", and "tokenImage" do not contain
 
86
   * relevant information.  The JavaCC generated code does not use
 
87
   * these constructors.
 
88
   */
 
89
 
 
90
  public ParseException() {
 
91
    super();
 
92
    specialConstructor = false;
 
93
  }
 
94
 
 
95
  public ParseException(String message) {
 
96
    super(message);
 
97
    specialConstructor = false;
 
98
  }
 
99
 
 
100
  /**
 
101
   * This variable determines which constructor was used to create
 
102
   * this object and thereby affects the semantics of the
 
103
   * "getMessage" method (see below).
 
104
   */
 
105
  protected boolean specialConstructor;
 
106
 
 
107
  /**
 
108
   * This is the last token that has been consumed successfully.  If
 
109
   * this object has been created due to a parse error, the token
 
110
   * followng this token will (therefore) be the first error token.
 
111
   */
 
112
  public Token currentToken;
 
113
 
 
114
  /**
 
115
   * Each entry in this array is an array of integers.  Each array
 
116
   * of integers represents a sequence of tokens (by their ordinal
 
117
   * values) that is expected at this point of the parse.
 
118
   */
 
119
  public int[][] expectedTokenSequences;
 
120
 
 
121
  /**
 
122
   * This is a reference to the "tokenImage" array of the generated
 
123
   * parser within which the parse error occurred.  This array is
 
124
   * defined in the generated ...Constants interface.
 
125
   */
 
126
  public String[] tokenImage;
 
127
 
 
128
  /**
 
129
   * This method has the standard behavior when this object has been
 
130
   * created using the standard constructors.  Otherwise, it uses
 
131
   * "currentToken" and "expectedTokenSequences" to generate a parse
 
132
   * error message and returns it.  If this object has been created
 
133
   * due to a parse error, and you do not catch it (it gets thrown
 
134
   * from the parser), then this method is called during the printing
 
135
   * of the final stack trace, and hence the correct error message
 
136
   * gets displayed.
 
137
   */
 
138
  public String getMessage() {
 
139
    if (!specialConstructor) {
 
140
      return super.getMessage();
 
141
    }
 
142
    String expected = "";
 
143
    int maxSize = 0;
 
144
    for (int i = 0; i < expectedTokenSequences.length; i++) {
 
145
      if (maxSize < expectedTokenSequences[i].length) {
 
146
        maxSize = expectedTokenSequences[i].length;
 
147
      }
 
148
      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
 
149
        expected += tokenImage[expectedTokenSequences[i][j]] + " ";
 
150
      }
 
151
      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
 
152
        expected += "...";
 
153
      }
 
154
      expected += eol + "    ";
 
155
    }
 
156
    String retval = "Encountered \"";
 
157
    Token tok = currentToken.next;
 
158
    for (int i = 0; i < maxSize; i++) {
 
159
      if (i != 0) retval += " ";
 
160
      if (tok.kind == 0) {
 
161
        retval += tokenImage[0];
 
162
        break;
 
163
      }
 
164
      retval += add_escapes(tok.image);
 
165
      tok = tok.next; 
 
166
    }
 
167
    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
 
168
    retval += "." + eol;
 
169
    if (expectedTokenSequences.length == 1) {
 
170
      retval += "Was expecting:" + eol + "    ";
 
171
    } else {
 
172
      retval += "Was expecting one of:" + eol + "    ";
 
173
    }
 
174
    retval += expected;
 
175
    return retval;
 
176
  }
 
177
 
 
178
  /**
 
179
   * The end of line string for this machine.
 
180
   */
 
181
  protected String eol = System.getProperty("line.separator", "\n");
 
182
 
 
183
  /**
 
184
   * Used to convert raw characters to their escaped version
 
185
   * when these raw version cannot be used as part of an ASCII
 
186
   * string literal.
 
187
   */
 
188
  protected String add_escapes(String str) {
 
189
      StringBuffer retval = new StringBuffer();
 
190
      char ch;
 
191
      for (int i = 0; i < str.length(); i++) {
 
192
        switch (str.charAt(i))
 
193
        {
 
194
           case 0 :
 
195
              continue;
 
196
           case '\b':
 
197
              retval.append("\\b");
 
198
              continue;
 
199
           case '\t':
 
200
              retval.append("\\t");
 
201
              continue;
 
202
           case '\n':
 
203
              retval.append("\\n");
 
204
              continue;
 
205
           case '\f':
 
206
              retval.append("\\f");
 
207
              continue;
 
208
           case '\r':
 
209
              retval.append("\\r");
 
210
              continue;
 
211
           case '\"':
 
212
              retval.append("\\\"");
 
213
              continue;
 
214
           case '\'':
 
215
              retval.append("\\\'");
 
216
              continue;
 
217
           case '\\':
 
218
              retval.append("\\\\");
 
219
              continue;
 
220
           default:
 
221
              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
 
222
                 String s = "0000" + Integer.toString(ch, 16);
 
223
                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
 
224
              } else {
 
225
                 retval.append(ch);
 
226
              }
 
227
              continue;
 
228
        }
 
229
      }
 
230
      return retval.toString();
 
231
   }
 
232
 
 
233
}