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