PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/ParseException.java

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