You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParseException.java 6.7KB

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