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.jj 7.3KB

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