ParseException.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * COMPONENT_NAME: idl.parser
27 *
28 * ORIGINS: 27
29 *
30 * Licensed Materials - Property of IBM
31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32 * RMI-IIOP v1.0
33 *
34 */
35
36package com.sun.tools.corba.se.idl;
37
38// NOTES:
39// -D56351<daz> Update Prefix pragma directive to CORBA 2.3 (see spec.).
40// -D57110<daz> Update ID pragma directive to CORBA 2.3 (see spec.).
41// -D46094<daz> Prohibit exceptions from appearing wihtin structs, unions, exceptions.
42// -D46094<daz> Prohibit attributes from appearing as operation parameter types,
43//  operation return types, attribute types.
44// -D59166<daz> Prohibit identifiers form colliding with IDL keywords.
45// -F60858.1<daz> Add keyword collision warning.
46// -F60942<daz> Prohibit operations from appearing as operation parameter types.
47// -D62023<daz> Add deprecated keyword warning; cast char to byte for JDK 1.2 compatability.
48
49import java.io.IOException;
50
51/**
52 *
53 **/
54class ParseException extends Exception
55{
56  /**
57   * Constructor: print the supplied message to Standard.err
58   * and create a new ParseException
59   * @return a new ParseException.
60   **/
61  ParseException (String message)
62  {
63    super (message);
64    System.err.println (message);
65    detected = true;
66  } // ctor
67
68  /**
69   * Constructor: print the supplied message to Standard.err, if it
70   * is not a warning, and create a new ParseException.
71   * @return a new ParseException.
72   **/
73  ParseException (String message, boolean onlyAWarning)
74  {
75   super (message);
76   System.err.println (message);
77   if (!onlyAWarning)
78     detected = true;
79  }
80
81  static ParseException abstractValueBox (Scanner scanner)
82  {
83    return arg0 ("abstractValueBox", scanner);
84  }
85
86  static ParseException alreadyDeclared (Scanner scanner, String type)
87  {
88    return arg1 ("alreadyDeclared", scanner, type);
89  }
90
91  static ParseException declNotInSameFile( Scanner scanner, String type,
92    String firstFile )
93  {
94    return arg2 ("declNotInSameFile", scanner, type, firstFile) ;
95  }
96
97  static ParseException alreadyDefaulted (Scanner scanner)
98  {
99    return arg0 ("alreadydefaulted", scanner);
100  }
101
102  static ParseException alreadyDerived (Scanner scanner, String derived, String iface)
103  {
104    return arg2 ("alreadyDerived", scanner, derived, iface);
105  }
106
107  static ParseException alreadyRaised (Scanner scanner, String exception)
108  {
109    return arg1 ("alreadyRaised", scanner, exception);
110  }
111
112  // <d60942>
113  static ParseException attributeNotType (Scanner scanner, String attr)
114  {
115    return arg1 ("attributeNotType", scanner, attr);
116  }
117
118  static ParseException badAbstract (Scanner scanner, String name)
119  {
120    return arg1 ("badAbstract", scanner, name);
121  }
122
123  static ParseException badCustom (Scanner scanner)
124  {
125    return arg0 ("badCustom", scanner);
126  }
127
128  // <d57110>
129  static ParseException badRepIDAlreadyAssigned (Scanner scanner, String entry)
130  {
131    return arg1 ("badRepIDAlreadyAssigned", scanner, entry);
132  }
133
134  // <d57110>
135  static ParseException badRepIDForm (Scanner scanner, String entry)
136  {
137    return arg1 ("badRepIDForm", scanner, entry);
138  }
139
140  // <d56351>
141  static ParseException badRepIDPrefix (Scanner scanner, String entry, String expected, String got)
142  {
143    return arg3 ("badRepIDPrefix", scanner, entry, expected, got);
144  }
145
146  static ParseException badState (Scanner scanner, String entry)
147  {
148    return arg1 ("badState", scanner, entry);
149  }
150
151  static ParseException branchLabel (Scanner scanner, String label)
152  {
153    return arg1 ("branchLabel", scanner, label);
154  }
155
156  static ParseException branchName (Scanner scanner, String name)
157  {
158    return arg1 ("branchName", scanner, name);
159  }
160
161  static ParseException duplicateInit (Scanner scanner)
162  {
163    return arg0 ("duplicateInit", scanner);
164  }
165
166  static ParseException duplicateState (Scanner scanner, String name)
167  {
168    return arg1 ("duplicateState", scanner, name);
169  }
170
171  static ParseException elseNoIf (Scanner scanner)
172  {
173    return arg0 ("elseNoIf", scanner);
174  }
175
176  static ParseException endNoIf (Scanner scanner)
177  {
178    return arg0 ("endNoIf", scanner);
179  }
180
181  static ParseException evaluationError (Scanner scanner, String problem)
182  {
183    return arg1 ("evaluation", scanner, problem);
184  }
185
186  static ParseException forwardEntry (Scanner scanner, String name)
187  {
188    return arg1 ("forwardEntry", scanner, name);
189  }
190
191  // <f46082.40> Cannot forward value boxes.
192  static ParseException forwardedValueBox (Scanner scanner, String name)
193  {
194    return arg1 ("forwardedValueBox", scanner, name);
195  }
196
197  static ParseException generic (Scanner scanner, String message)
198  {
199    return arg1 ("generic", scanner, message);
200  }
201
202  static ParseException illegalArray (Scanner scanner, String name)
203  {
204    return arg1 ("illegalArray", scanner, name);
205  }
206
207  static ParseException illegalException (Scanner scanner, String name)
208  {
209    return arg1 ("illegalException", scanner, name);
210  }
211
212  static ParseException invalidConst (Scanner scanner, String mustBe, String is)
213  {
214    return arg2 ("invalidConst1", scanner, mustBe, is);
215  }
216
217  static ParseException invalidConst (Scanner scanner, String type)
218  {
219    return arg1 ("invalidConst2", scanner, type);
220  }
221
222  // <d59166> Non-escaped identifiers that collide with keywords are illegal.
223  static ParseException keywordCollision (Scanner scanner, String id)
224  {
225    return arg1 ("keywordCollision", scanner, id);
226  }
227
228  // <d62023> Warning for keywords that will be removed in a future version of IDL.
229  static ParseException deprecatedKeywordWarning (Scanner scanner, String id)
230  {
231    return arg1Warning ("deprecatedKeywordWarning", scanner, id);
232  }
233
234  // <f60858.1> Warning for above error.
235  static ParseException keywordCollisionWarning (Scanner scanner, String id)
236  {
237    return arg1Warning ("keywordCollisionWarning", scanner, id);
238  }
239
240  static ParseException methodClash (Scanner scanner, String interf, String method)
241  {
242    return arg2 ("methodClash", scanner, interf, method);
243  }
244
245  static ParseException moduleNotType (Scanner scanner, String module)
246  {
247    return arg1 ("moduleNotType", scanner, module);
248  }
249
250  // <d59067>
251  static ParseException nestedValueBox (Scanner scanner)
252  {
253    return arg0 ("nestedValueBox", scanner);
254  }
255
256  static ParseException noDefault (Scanner scanner)
257  {
258    return arg0 ("noDefault", scanner);
259  }
260
261  static ParseException nonAbstractParent (Scanner scanner, String baseClass, String parentClass)
262  {
263    return arg2 ("nonAbstractParent", scanner, baseClass, parentClass);
264  }
265
266  static ParseException nonAbstractParent2 (Scanner scanner, String baseClass, String parentClass)
267  {
268    return arg2 ("nonAbstractParent2", scanner, baseClass, parentClass);
269  }
270
271  static ParseException nonAbstractParent3 (Scanner scanner, String baseClass, String parentClass)
272  {
273    return arg2 ("nonAbstractParent3", scanner, baseClass, parentClass);
274  }
275
276  static ParseException notANumber (Scanner scanner, String notNumber)
277  {
278    return arg1 ("notANumber", scanner, notNumber);
279  }
280
281  static ParseException nothing (String filename)
282  {
283    return new ParseException (Util.getMessage ("ParseException.nothing", filename));
284  }
285
286  static ParseException notPositiveInt (Scanner scanner, String notPosInt)
287  {
288    return arg1 ("notPosInt", scanner, notPosInt);
289  }
290
291  static ParseException oneway (Scanner scanner, String method)
292  {
293    return arg1 ("oneway", scanner, method);
294  }
295
296  // <d60942>
297  static ParseException operationNotType (Scanner scanner, String op)
298  {
299    return arg1 ("operationNotType", scanner, op);
300  }
301
302  static ParseException outOfRange (Scanner scanner, String value, String type)
303  {
304    return arg2 ("outOfRange", scanner, value, type);
305  }
306
307  static ParseException recursive (Scanner scanner, String type, String name)
308  {
309    return arg2 ("recursive", scanner, type, name);
310  }
311
312  static ParseException selfInherit (Scanner scanner, String name)
313  {
314    return arg1 ("selfInherit", scanner, name);
315  }
316
317  static ParseException stringTooLong (Scanner scanner, String str, String max)
318  {
319    return arg2 ("stringTooLong", scanner, str, max);
320  }
321
322  static ParseException syntaxError (Scanner scanner, int expected, int got)
323  {
324    return arg2 ("syntax1", scanner, Token.toString (expected), Token.toString (got));
325  }
326
327  static ParseException syntaxError (Scanner scanner, String expected, String got)
328  {
329    return arg2 ("syntax1", scanner, expected, got);
330  }
331
332  static ParseException syntaxError (Scanner scanner, int[] expected, int got)
333  {
334    return syntaxError (scanner, expected, Token.toString (got));
335  }
336
337  static ParseException syntaxError (Scanner scanner, int[] expected, String got)
338  {
339    String tokenList = "";
340    for (int i = 0; i < expected.length; ++i)
341      tokenList += " `" + Token.toString (expected[i]) + "'";
342    return arg2 ("syntax2", scanner, tokenList, got);
343  }
344
345  static ParseException unclosedComment (String filename)
346  {
347    return new ParseException (Util.getMessage ("ParseException.unclosed", filename));
348  }
349
350  static ParseException undeclaredType (Scanner scanner, String undeclaredType)
351  {
352    return arg1 ("undeclaredType", scanner, undeclaredType);
353  }
354
355  static ParseException warning (Scanner scanner, String message)
356  {
357    scannerInfo (scanner);
358    String[] parameters = { filename, Integer.toString (lineNumber), message, line, pointer };
359    return new ParseException (Util.getMessage ("ParseException.warning", parameters), true);
360  }
361
362  static ParseException wrongType (Scanner scanner, String name, String mustBe, String is)
363  {
364    scannerInfo (scanner);
365    String[] parameters = {filename, Integer.toString (lineNumber), name, is, mustBe, line, pointer};
366    return new ParseException (Util.getMessage ("ParseException.wrongType", parameters));
367  }
368
369  static ParseException wrongExprType (Scanner scanner, String mustBe, String is)
370  {
371    scannerInfo (scanner);
372    String[] parameters = {filename, Integer.toString (lineNumber),
373      is, mustBe, line, pointer};
374    return new ParseException (Util.getMessage ("ParseException.constExprType",
375      parameters));
376  }
377
378  static ParseException illegalForwardInheritance( Scanner scanner, String declName,
379    String baseName )
380  {
381    scannerInfo( scanner ) ;
382    String[] parameters = { filename, Integer.toString(lineNumber),
383        declName, baseName, line, pointer } ;
384    return new ParseException (Util.getMessage(
385        "ParseException.forwardInheritance", parameters ) ) ;
386  }
387
388  static ParseException illegalIncompleteTypeReference( Scanner scanner,
389    String declName )
390  {
391    scannerInfo( scanner ) ;
392    String[] parameters = { filename, Integer.toString(lineNumber),
393        declName, line, pointer } ;
394    return new ParseException (Util.getMessage(
395        "ParseException.illegalIncompleteTypeReference", parameters ) ) ;
396  }
397
398  private static void scannerInfo (Scanner scanner)
399  {
400    filename   = scanner.filename ();
401    line       = scanner.lastTokenLine ();
402    lineNumber = scanner.lastTokenLineNumber ();
403    int pos    = scanner.lastTokenLinePosition ();
404    pointer    = "^";
405    if (pos > 1)
406    {
407      byte[] bytes = new byte[ pos - 1 ];
408      for (int i = 0; i < pos - 1; ++i)
409        bytes[i] = (byte)' ';  // <d62023>
410      pointer = new String (bytes) + pointer;
411    }
412  }
413
414  private static ParseException arg0 (String msgId, Scanner scanner)
415  {
416    scannerInfo (scanner);
417    String[] parameters = {filename, Integer.toString (lineNumber), line, pointer};
418    return new ParseException (Util.getMessage ("ParseException." + msgId, parameters));
419  }
420
421  private static ParseException arg1 (String msgId, Scanner scanner, String arg1)
422  {
423    scannerInfo (scanner);
424    String[] parameters = {filename, Integer.toString (lineNumber), arg1, line, pointer};
425    return new ParseException (Util.getMessage ("ParseException." + msgId, parameters));
426  }
427
428  // <f60858.1>
429  private static ParseException arg1Warning (String msgId, Scanner scanner, String arg1)
430  {
431    scannerInfo (scanner);
432    String[] parameters = {filename, Integer.toString (lineNumber), arg1, line, pointer};
433    return new ParseException (Util.getMessage ("ParseException." + msgId, parameters), true);
434  }
435
436  private static ParseException arg2 (String msgId, Scanner scanner, String arg1, String arg2)
437  {
438    scannerInfo (scanner);
439    String[] parameters = {filename, Integer.toString (lineNumber), arg1, arg2, line, pointer};
440    return new ParseException (Util.getMessage ("ParseException." + msgId, parameters));
441  }
442
443  private static ParseException arg3 (String msgId, Scanner scanner, String arg1, String arg2, String arg3)
444  {
445    scannerInfo (scanner);
446    String[] parameters = {filename, Integer.toString (lineNumber), arg1, arg2, arg3, line, pointer};
447    return new ParseException (Util.getMessage ("ParseException." + msgId, parameters));
448  }
449
450  private static String filename  = "";
451  private static String line      = "";
452  private static int   lineNumber = 0;
453  private static String pointer   = "^";
454
455  static boolean detected = false;
456} // class ParseException
457