PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/ParseException.java

#
Java | 304 lines | 160 code | 24 blank | 120 comment | 17 complexity | cfd6c534fb65543da05f26fb121dd597 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
  2. /*
  3. This file was auto generated, but has been modified since then. If we
  4. need to regenerate it for some reason we should be careful to look at
  5. the notes below.
  6. All BeanShell modifications are demarcated by "Begin BeanShell
  7. Modification - ... " and "End BeanShell Modification - ..."
  8. Note: Please leave the ^M carriage return in the above auto-generated line
  9. or JavaCC will complain about version on Win systems.
  10. BeanShell Modification to generated file
  11. ----------------------------------------
  12. - Added sourceFile attribute
  13. setErrorSourceFile()
  14. getErrorSourceFile()
  15. - Modified getMessage() to print more tersely except on debug
  16. (removed "Was expecting one of...)
  17. - Added sourceFile info to getMessage()
  18. - Made ParseException extend EvalError
  19. - Modified constructors to use EvalError
  20. - Override
  21. getErrorLineNumber()
  22. getErrorText()
  23. - Add
  24. toString()
  25. */
  26. package bsh;
  27. /**
  28. * This exception is thrown when parse errors are encountered.
  29. * You can explicitly create objects of this exception type by
  30. * calling the method generateParseException in the generated
  31. * parser.
  32. *
  33. * You can modify this class to customize your error reporting
  34. * mechanisms so long as you retain the public fields.
  35. */
  36. // Begin BeanShell Modification - public, extend EvalError
  37. public class ParseException extends EvalError {
  38. // End BeanShell Modification - public, extend EvalError
  39. // Begin BeanShell Modification - sourceFile
  40. String sourceFile = "<unknown>";
  41. /**
  42. Used to add source file info to exception
  43. */
  44. public void setErrorSourceFile( String file ) {
  45. this.sourceFile = file;
  46. }
  47. public String getErrorSourceFile() {
  48. return sourceFile;
  49. }
  50. // End BeanShell Modification - sourceFile
  51. /**
  52. * This constructor is used by the method "generateParseException"
  53. * in the generated parser. Calling this constructor generates
  54. * a new object of this type with the fields "currentToken",
  55. * "expectedTokenSequences", and "tokenImage" set. The boolean
  56. * flag "specialConstructor" is also set to true to indicate that
  57. * this constructor was used to create this object.
  58. * This constructor calls its super class with the empty string
  59. * to force the "toString" method of parent class "Throwable" to
  60. * print the error message in the form:
  61. * ParseException: <result of getMessage>
  62. */
  63. public ParseException(Token currentTokenVal,
  64. int[][] expectedTokenSequencesVal,
  65. String[] tokenImageVal
  66. )
  67. {
  68. // Begin BeanShell Modification - constructor
  69. this();
  70. // End BeanShell Modification - constructor
  71. specialConstructor = true;
  72. currentToken = currentTokenVal;
  73. expectedTokenSequences = expectedTokenSequencesVal;
  74. tokenImage = tokenImageVal;
  75. }
  76. /**
  77. * The following constructors are for use by you for whatever
  78. * purpose you can think of. Constructing the exception in this
  79. * manner makes the exception behave in the normal way - i.e., as
  80. * documented in the class "Throwable". The fields "errorToken",
  81. * "expectedTokenSequences", and "tokenImage" do not contain
  82. * relevant information. The JavaCC generated code does not use
  83. * these constructors.
  84. */
  85. public ParseException() {
  86. // Begin BeanShell Modification - constructor
  87. this("");
  88. // End BeanShell Modification - constructor
  89. specialConstructor = false;
  90. }
  91. public ParseException(String message) {
  92. // Begin BeanShell Modification - super constructor args
  93. // null node, null callstack, ParseException knows where the error is.
  94. super( message, null, null );
  95. // End BeanShell Modification - super constructor args
  96. specialConstructor = false;
  97. }
  98. /**
  99. * This variable determines which constructor was used to create
  100. * this object and thereby affects the semantics of the
  101. * "getMessage" method (see below).
  102. */
  103. protected boolean specialConstructor;
  104. /**
  105. * This is the last token that has been consumed successfully. If
  106. * this object has been created due to a parse error, the token
  107. * followng this token will (therefore) be the first error token.
  108. */
  109. public Token currentToken;
  110. /**
  111. * Each entry in this array is an array of integers. Each array
  112. * of integers represents a sequence of tokens (by their ordinal
  113. * values) that is expected at this point of the parse.
  114. */
  115. public int[][] expectedTokenSequences;
  116. /**
  117. * This is a reference to the "tokenImage" array of the generated
  118. * parser within which the parse error occurred. This array is
  119. * defined in the generated ...Constants interface.
  120. */
  121. public String[] tokenImage;
  122. // Begin BeanShell Modification - moved body to overloaded getMessage()
  123. public String getMessage() {
  124. return getMessage( false );
  125. }
  126. // End BeanShell Modification - moved body to overloaded getMessage()
  127. /**
  128. * This method has the standard behavior when this object has been
  129. * created using the standard constructors. Otherwise, it uses
  130. * "currentToken" and "expectedTokenSequences" to generate a parse
  131. * error message and returns it. If this object has been created
  132. * due to a parse error, and you do not catch it (it gets thrown
  133. * from the parser), then this method is called during the printing
  134. * of the final stack trace, and hence the correct error message
  135. * gets displayed.
  136. */
  137. // Begin BeanShell Modification - added debug param
  138. public String getMessage( boolean debug ) {
  139. // End BeanShell Modification - added debug param
  140. if (!specialConstructor) {
  141. return super.getMessage();
  142. }
  143. String expected = "";
  144. int maxSize = 0;
  145. for (int i = 0; i < expectedTokenSequences.length; i++) {
  146. if (maxSize < expectedTokenSequences[i].length) {
  147. maxSize = expectedTokenSequences[i].length;
  148. }
  149. for (int j = 0; j < expectedTokenSequences[i].length; j++) {
  150. expected += tokenImage[expectedTokenSequences[i][j]] + " ";
  151. }
  152. if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
  153. expected += "...";
  154. }
  155. expected += eol + " ";
  156. }
  157. // Begin BeanShell Modification - added sourceFile info
  158. String retval = "In file: "+ sourceFile +" Encountered \"";
  159. // End BeanShell Modification - added sourceFile info
  160. Token tok = currentToken.next;
  161. for (int i = 0; i < maxSize; i++) {
  162. if (i != 0) retval += " ";
  163. if (tok.kind == 0) {
  164. retval += tokenImage[0];
  165. break;
  166. }
  167. retval += add_escapes(tok.image);
  168. tok = tok.next;
  169. }
  170. retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
  171. // Begin BeanShell Modification - made conditional on debug
  172. if ( debug )
  173. {
  174. if (expectedTokenSequences.length == 1) {
  175. retval += "Was expecting:" + eol + " ";
  176. } else {
  177. retval += "Was expecting one of:" + eol + " ";
  178. }
  179. retval += expected;
  180. }
  181. // End BeanShell Modification - made conditional on debug
  182. return retval;
  183. }
  184. /**
  185. * The end of line string for this machine.
  186. */
  187. protected String eol = System.getProperty("line.separator", "\n");
  188. /**
  189. * Used to convert raw characters to their escaped version
  190. * when these raw version cannot be used as part of an ASCII
  191. * string literal.
  192. */
  193. protected String add_escapes(String str) {
  194. StringBuffer retval = new StringBuffer();
  195. char ch;
  196. for (int i = 0; i < str.length(); i++) {
  197. switch (str.charAt(i))
  198. {
  199. case 0 :
  200. continue;
  201. case '\b':
  202. retval.append("\\b");
  203. continue;
  204. case '\t':
  205. retval.append("\\t");
  206. continue;
  207. case '\n':
  208. retval.append("\\n");
  209. continue;
  210. case '\f':
  211. retval.append("\\f");
  212. continue;
  213. case '\r':
  214. retval.append("\\r");
  215. continue;
  216. case '\"':
  217. retval.append("\\\"");
  218. continue;
  219. case '\'':
  220. retval.append("\\\'");
  221. continue;
  222. case '\\':
  223. retval.append("\\\\");
  224. continue;
  225. default:
  226. if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
  227. String s = "0000" + Integer.toString(ch, 16);
  228. retval.append("\\u" + s.substring(s.length() - 4, s.length()));
  229. } else {
  230. retval.append(ch);
  231. }
  232. continue;
  233. }
  234. }
  235. return retval.toString();
  236. }
  237. // Begin BeanShell Modification - override error methods and toString
  238. public int getErrorLineNumber()
  239. {
  240. return currentToken.next.beginLine;
  241. }
  242. public String getErrorText() {
  243. // copied from generated getMessage()
  244. int maxSize = 0;
  245. for (int i = 0; i < expectedTokenSequences.length; i++) {
  246. if (maxSize < expectedTokenSequences[i].length)
  247. maxSize = expectedTokenSequences[i].length;
  248. }
  249. String retval = "";
  250. Token tok = currentToken.next;
  251. for (int i = 0; i < maxSize; i++)
  252. {
  253. if (i != 0) retval += " ";
  254. if (tok.kind == 0) {
  255. retval += tokenImage[0];
  256. break;
  257. }
  258. retval += add_escapes(tok.image);
  259. tok = tok.next;
  260. }
  261. return retval;
  262. }
  263. public String toString() {
  264. return getMessage();
  265. }
  266. // End BeanShell Modification - override error methods and toString
  267. }