ParseException.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package com.yahoo.jute.compiler.generated;
  20. /**
  21. * This exception is thrown when parse errors are encountered.
  22. * You can explicitly create objects of this exception type by
  23. * calling the method generateParseException in the generated
  24. * parser.
  25. *
  26. * You can modify this class to customize your error reporting
  27. * mechanisms so long as you retain the public fields.
  28. */
  29. public class ParseException extends Exception {
  30. /**
  31. * This constructor is used by the method "generateParseException"
  32. * in the generated parser. Calling this constructor generates
  33. * a new object of this type with the fields "currentToken",
  34. * "expectedTokenSequences", and "tokenImage" set. The boolean
  35. * flag "specialConstructor" is also set to true to indicate that
  36. * this constructor was used to create this object.
  37. * This constructor calls its super class with the empty string
  38. * to force the "toString" method of parent class "Throwable" to
  39. * print the error message in the form:
  40. * ParseException: <result of getMessage>
  41. */
  42. public ParseException(Token currentTokenVal,
  43. int[][] expectedTokenSequencesVal,
  44. String[] tokenImageVal
  45. )
  46. {
  47. super("");
  48. specialConstructor = true;
  49. currentToken = currentTokenVal;
  50. expectedTokenSequences = expectedTokenSequencesVal;
  51. tokenImage = tokenImageVal;
  52. }
  53. /**
  54. * The following constructors are for use by you for whatever
  55. * purpose you can think of. Constructing the exception in this
  56. * manner makes the exception behave in the normal way - i.e., as
  57. * documented in the class "Throwable". The fields "errorToken",
  58. * "expectedTokenSequences", and "tokenImage" do not contain
  59. * relevant information. The JavaCC generated code does not use
  60. * these constructors.
  61. */
  62. public ParseException() {
  63. super();
  64. specialConstructor = false;
  65. }
  66. public ParseException(String message) {
  67. super(message);
  68. specialConstructor = false;
  69. }
  70. /**
  71. * This variable determines which constructor was used to create
  72. * this object and thereby affects the semantics of the
  73. * "getMessage" method (see below).
  74. */
  75. protected boolean specialConstructor;
  76. /**
  77. * This is the last token that has been consumed successfully. If
  78. * this object has been created due to a parse error, the token
  79. * followng this token will (therefore) be the first error token.
  80. */
  81. public Token currentToken;
  82. /**
  83. * Each entry in this array is an array of integers. Each array
  84. * of integers represents a sequence of tokens (by their ordinal
  85. * values) that is expected at this point of the parse.
  86. */
  87. public int[][] expectedTokenSequences;
  88. /**
  89. * This is a reference to the "tokenImage" array of the generated
  90. * parser within which the parse error occurred. This array is
  91. * defined in the generated ...Constants interface.
  92. */
  93. public String[] tokenImage;
  94. /**
  95. * This method has the standard behavior when this object has been
  96. * created using the standard constructors. Otherwise, it uses
  97. * "currentToken" and "expectedTokenSequences" to generate a parse
  98. * error message and returns it. If this object has been created
  99. * due to a parse error, and you do not catch it (it gets thrown
  100. * from the parser), then this method is called during the printing
  101. * of the final stack trace, and hence the correct error message
  102. * gets displayed.
  103. */
  104. public String getMessage() {
  105. if (!specialConstructor) {
  106. return super.getMessage();
  107. }
  108. StringBuffer expected = new StringBuffer();
  109. int maxSize = 0;
  110. for (int i = 0; i < expectedTokenSequences.length; i++) {
  111. if (maxSize < expectedTokenSequences[i].length) {
  112. maxSize = expectedTokenSequences[i].length;
  113. }
  114. for (int j = 0; j < expectedTokenSequences[i].length; j++) {
  115. expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
  116. }
  117. if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
  118. expected.append("...");
  119. }
  120. expected.append(eol).append(" ");
  121. }
  122. String retval = "Encountered \"";
  123. Token tok = currentToken.next;
  124. for (int i = 0; i < maxSize; i++) {
  125. if (i != 0) retval += " ";
  126. if (tok.kind == 0) {
  127. retval += tokenImage[0];
  128. break;
  129. }
  130. retval += add_escapes(tok.image);
  131. tok = tok.next;
  132. }
  133. retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
  134. retval += "." + eol;
  135. if (expectedTokenSequences.length == 1) {
  136. retval += "Was expecting:" + eol + " ";
  137. } else {
  138. retval += "Was expecting one of:" + eol + " ";
  139. }
  140. retval += expected.toString();
  141. return retval;
  142. }
  143. /**
  144. * The end of line string for this machine.
  145. */
  146. protected String eol = System.getProperty("line.separator", "\n");
  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. }