PageRenderTime 664ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/ecmascript/parser/ASTLiteral.java

#
Java | 364 lines | 284 code | 50 blank | 30 comment | 71 complexity | f4dcd29c9860d7caab188330367abf73 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:JJTree: Do not edit this line. ASTLiteral.java */
  2. package sidekick.ecmascript.parser;
  3. import sidekick.ecmascript.parser.EcmaScript;
  4. import sidekick.ecmascript.parser.EcmaScriptVisitor;
  5. import sidekick.ecmascript.parser.ParseException;
  6. import sidekick.ecmascript.parser.Token;
  7. public class ASTLiteral extends SimpleNode {
  8. static public final Object NULL = new Object();
  9. static public final Object REGEX = new Object();
  10. static public final Object HTML = new Object();
  11. protected String tokenImage;
  12. protected Object value;
  13. public ASTLiteral(int id) {
  14. super(id);
  15. }
  16. public ASTLiteral(EcmaScript p, int id) {
  17. super(p, id);
  18. }
  19. /** Accept the visitor. * */
  20. @Override
  21. public Object jjtAccept(EcmaScriptVisitor visitor, Object data) {
  22. return visitor.visit(this, data);
  23. }
  24. public void setValue(Object value) {
  25. this.value = value;
  26. }
  27. public Object getValue() {
  28. return value;
  29. }
  30. public String getTokenImage() {
  31. return tokenImage;
  32. }
  33. static public String unescapedString(String image) {
  34. image = image.substring(1, image.length() - 1);
  35. int l = image.length();
  36. StringBuffer sb = new StringBuffer(l);
  37. /*
  38. *
  39. * \XXX - The character specified by up to three octal digits XXX
  40. * between 0 and 377 ( example: \251 ) \xXX - The character specified by
  41. * the two hexadecimal digits XX between 00 and FF ( example: \xA9 ) \
  42. * uXXXX - The Unicode character specified by the four hexadecimal
  43. * digits XXXX ( example: \u00A9 )
  44. */
  45. for (int i = 0; i < l; i++) {
  46. char c = image.charAt(i);
  47. if ((c == '\\') && (i + 1 < l)) {
  48. i++;
  49. c = image.charAt(i);
  50. if (c == 'n') {
  51. c = '\n';
  52. } else if (c == 'b') {
  53. c = '\b';
  54. } else if (c == 'f') {
  55. c = '\f';
  56. } else if (c == 'r') {
  57. c = '\r';
  58. } else if (c == 't') {
  59. c = '\t';
  60. } else if (c == 'x' && l == 4) {
  61. c = (char) (hexval(image.charAt(i + 1)) << 4 | hexval(image
  62. .charAt(i + 1)));
  63. i += 2;
  64. } else if (c == 'u') {
  65. c = (char) (hexval(image.charAt(i + 1)) << 12
  66. | hexval(image.charAt(i + 2)) << 8
  67. | hexval(image.charAt(i + 3)) << 4 | hexval(image
  68. .charAt(i + 4)));
  69. i += 4;
  70. } else if ((c >= '0') && (c <= '7') && l == 4) {
  71. c = (char) (octval(image.charAt(i)));
  72. if ((image.length() > i) && (image.charAt(i + 1) >= '0')
  73. && (image.charAt(i + 1) <= '7')) {
  74. i++;
  75. c = (char) ((c << 4) | octval(image.charAt(i)));
  76. }
  77. }
  78. }
  79. sb.append(c);
  80. }
  81. return sb.toString();
  82. }
  83. static public String escapedString(String image, char quoteChar) {
  84. int l = image.length();
  85. StringBuffer sb = new StringBuffer(l);
  86. for (int i = 0; i < l; i++) {
  87. char c = image.charAt(i);
  88. if (c == '\n') {
  89. sb.append("\\n");
  90. } else if (c == '\r') {
  91. sb.append("\\r");
  92. } else if (c == '\b') {
  93. sb.append("\\b");
  94. } else if (c == '\f') {
  95. sb.append("\\f");
  96. } else if (c == '\t') {
  97. sb.append("\\t");
  98. } else if (c == quoteChar) {
  99. sb.append("\\");
  100. sb.append(quoteChar);
  101. } else {
  102. // PENDING(uwe): unicode escapes, hex escapes etc
  103. sb.append(c);
  104. }
  105. }
  106. return sb.toString();
  107. }
  108. public void setStringValue(String image) throws ParseException {
  109. tokenImage = image;
  110. value = ASTLiteral.unescapedString(image);
  111. /*
  112. * if (!expectActionScript) { if (value.equals("class") ||
  113. * value.equals("super")) { throw new ParseException( "reserved
  114. * identifier class or super used"); } }
  115. */
  116. }
  117. public void setUnquotedStringValue(String valueStr) {
  118. value = valueStr;
  119. // PENDING(uwe): clean up and other value types
  120. // PENDING(uwe): serious shit here -> escaping/unescaping, quotes etc
  121. Token token = getBeginToken();
  122. char quoteChar = token.image.charAt(0);
  123. if (quoteChar == '"') {
  124. token.image = "\"" + ASTLiteral.escapedString(valueStr, quoteChar)
  125. + "\"";
  126. } else {
  127. token.image = "'" + ASTLiteral.escapedString(valueStr, quoteChar)
  128. + "'";
  129. }
  130. }
  131. public void setRegexValue(String image) {
  132. tokenImage = image;
  133. value = ASTLiteral.REGEX;
  134. }
  135. public void setHtmlValue(String image) {
  136. tokenImage = image;
  137. value = ASTLiteral.HTML;
  138. }
  139. public void setDecimalValue(String image) {
  140. tokenImage = image;
  141. try {
  142. value = new Long(image);
  143. } catch (NumberFormatException e) {
  144. // it's a floating point
  145. value = new Double(image);
  146. }
  147. }
  148. public void setHexValue(String image) {
  149. tokenImage = image;
  150. value = new Long(Long.parseLong(image.substring(2), 16));
  151. }
  152. public void setFloatingPointValue(String image) {
  153. tokenImage = image;
  154. value = new Double(image);
  155. }
  156. public void setBooleanValue(String image) {
  157. tokenImage = image;
  158. value = new Boolean(image);
  159. }
  160. public void setNullValue() {
  161. value = ASTLiteral.NULL;
  162. }
  163. @Override
  164. public String toString() {
  165. boolean b = System.getProperty( "sidekick.ecmascript.general.allNodes", "false" ).startsWith("t");
  166. if (b) {
  167. return super.toString();
  168. }
  169. return "Literal[" + (value == null ? "" : value.toString()) + "]";
  170. }
  171. /**
  172. * Overwrites <code>equals</code> from <code>Object</code>.
  173. *
  174. * @param obj
  175. * a object
  176. * @return true if specified object equal to receiver
  177. * @see java.lang.Object#equals(Object)
  178. */
  179. @Override
  180. public boolean equals(Object obj) {
  181. if ((obj != null) && (obj.getClass().equals(this.getClass()))) {
  182. ASTLiteral other = (ASTLiteral) obj;
  183. boolean result = true;
  184. if (tokenImage != null) {
  185. result = tokenImage.equals(other.tokenImage);
  186. } else {
  187. result = (other.tokenImage == null);
  188. }
  189. if (result) {
  190. result = value.equals(other.value);
  191. }
  192. return result;
  193. }
  194. return false;
  195. }
  196. /**
  197. * Overwrites <code>hashCode</code> from <code>Object</code>.
  198. *
  199. * @return hash code of receiver
  200. * @see java.lang.Object#hashCode()
  201. */
  202. @Override
  203. public int hashCode() {
  204. int result = 17;
  205. if (tokenImage != null) {
  206. result = 37 * result + tokenImage.hashCode();
  207. } else {
  208. result = 37 * result;
  209. }
  210. if (value != null) {
  211. result = 37 * result + value.hashCode();
  212. } else {
  213. result = 37 * result;
  214. }
  215. return result;
  216. }
  217. static final int hexval(char c) {
  218. switch (c) {
  219. case '0':
  220. return 0;
  221. case '1':
  222. return 1;
  223. case '2':
  224. return 2;
  225. case '3':
  226. return 3;
  227. case '4':
  228. return 4;
  229. case '5':
  230. return 5;
  231. case '6':
  232. return 6;
  233. case '7':
  234. return 7;
  235. case '8':
  236. return 8;
  237. case '9':
  238. return 9;
  239. case 'a':
  240. case 'A':
  241. return 10;
  242. case 'b':
  243. case 'B':
  244. return 11;
  245. case 'c':
  246. case 'C':
  247. return 12;
  248. case 'd':
  249. case 'D':
  250. return 13;
  251. case 'e':
  252. case 'E':
  253. return 14;
  254. case 'f':
  255. case 'F':
  256. return 15;
  257. }
  258. throw new IllegalStateException("Illegal hex or unicode constant"); // Should
  259. // never
  260. // come
  261. // here
  262. }
  263. static final int octval(char c) {
  264. switch (c) {
  265. case '0':
  266. return 0;
  267. case '1':
  268. return 1;
  269. case '2':
  270. return 2;
  271. case '3':
  272. return 3;
  273. case '4':
  274. return 4;
  275. case '5':
  276. return 5;
  277. case '6':
  278. return 6;
  279. case '7':
  280. return 7;
  281. case '8':
  282. return 8;
  283. case '9':
  284. return 9;
  285. case 'a':
  286. case 'A':
  287. return 10;
  288. case 'b':
  289. case 'B':
  290. return 11;
  291. case 'c':
  292. case 'C':
  293. return 12;
  294. case 'd':
  295. case 'D':
  296. return 13;
  297. case 'e':
  298. case 'E':
  299. return 14;
  300. case 'f':
  301. case 'F':
  302. return 15;
  303. }
  304. throw new IllegalStateException("Illegal octal constant"); // Should
  305. // never
  306. // come here
  307. }
  308. }