/plugins/XML/tags/release-2-0-4/sidekick/ecmascript/parser/ASTLiteral.java

# · Java · 359 lines · 315 code · 26 blank · 18 comment · 55 complexity · 5dcf0ebbf3afd4c11ed57ca10f638e4b MD5 · raw file

  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. return "Literal[" + value.toString() + "]";
  166. }
  167. /**
  168. * Overwrites <code>equals</code> from <code>Object</code>.
  169. *
  170. * @param obj
  171. * a object
  172. * @return true if specified object equal to receiver
  173. * @see java.lang.Object#equals(Object)
  174. */
  175. @Override
  176. public boolean equals(Object obj) {
  177. if ((obj != null) && (obj.getClass().equals(this.getClass()))) {
  178. ASTLiteral other = (ASTLiteral) obj;
  179. boolean result = true;
  180. if (tokenImage != null) {
  181. result = tokenImage.equals(other.tokenImage);
  182. } else {
  183. result = (other.tokenImage == null);
  184. }
  185. if (result) {
  186. result = value.equals(other.value);
  187. }
  188. return result;
  189. }
  190. return false;
  191. }
  192. /**
  193. * Overwrites <code>hashCode</code> from <code>Object</code>.
  194. *
  195. * @return hash code of receiver
  196. * @see java.lang.Object#hashCode()
  197. */
  198. @Override
  199. public int hashCode() {
  200. int result = 17;
  201. if (tokenImage != null) {
  202. result = 37 * result + tokenImage.hashCode();
  203. } else {
  204. result = 37 * result;
  205. }
  206. if (value != null) {
  207. result = 37 * result + value.hashCode();
  208. } else {
  209. result = 37 * result;
  210. }
  211. return result;
  212. }
  213. static final int hexval(char c) {
  214. switch (c) {
  215. case '0':
  216. return 0;
  217. case '1':
  218. return 1;
  219. case '2':
  220. return 2;
  221. case '3':
  222. return 3;
  223. case '4':
  224. return 4;
  225. case '5':
  226. return 5;
  227. case '6':
  228. return 6;
  229. case '7':
  230. return 7;
  231. case '8':
  232. return 8;
  233. case '9':
  234. return 9;
  235. case 'a':
  236. case 'A':
  237. return 10;
  238. case 'b':
  239. case 'B':
  240. return 11;
  241. case 'c':
  242. case 'C':
  243. return 12;
  244. case 'd':
  245. case 'D':
  246. return 13;
  247. case 'e':
  248. case 'E':
  249. return 14;
  250. case 'f':
  251. case 'F':
  252. return 15;
  253. }
  254. throw new IllegalStateException("Illegal hex or unicode constant"); // Should
  255. // never
  256. // come
  257. // here
  258. }
  259. static final int octval(char c) {
  260. switch (c) {
  261. case '0':
  262. return 0;
  263. case '1':
  264. return 1;
  265. case '2':
  266. return 2;
  267. case '3':
  268. return 3;
  269. case '4':
  270. return 4;
  271. case '5':
  272. return 5;
  273. case '6':
  274. return 6;
  275. case '7':
  276. return 7;
  277. case '8':
  278. return 8;
  279. case '9':
  280. return 9;
  281. case 'a':
  282. case 'A':
  283. return 10;
  284. case 'b':
  285. case 'B':
  286. return 11;
  287. case 'c':
  288. case 'C':
  289. return 12;
  290. case 'd':
  291. case 'D':
  292. return 13;
  293. case 'e':
  294. case 'E':
  295. return 14;
  296. case 'f':
  297. case 'F':
  298. return 15;
  299. }
  300. throw new IllegalStateException("Illegal octal constant"); // Should
  301. // never
  302. // come here
  303. }
  304. }