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