1 4 5 9 10 package org.openlaszlo.sc.parser; 11 12 public class ASTLiteral extends SimpleNode { 13 14 private Object mValue = null; 15 16 public ASTLiteral(int id) { 17 super(id); 18 } 19 20 public ASTLiteral(Parser p, int id) { 21 super(p, id); 22 } 23 24 public static Node jjtCreate(int id) { 25 return new ASTLiteral(id); 26 } 27 28 public static Node jjtCreate(Parser p, int id) { 29 return new ASTLiteral(p, id); 30 } 31 32 public ASTLiteral(Object value) { 34 mValue = value; 35 } 36 37 public Object getValue() { 38 return mValue; 39 } 40 41 static final int hexval(char c) { 42 switch(c) { 43 case '0' : 44 return 0; 45 case '1' : 46 return 1; 47 case '2' : 48 return 2; 49 case '3' : 50 return 3; 51 case '4' : 52 return 4; 53 case '5' : 54 return 5; 55 case '6' : 56 return 6; 57 case '7' : 58 return 7; 59 case '8' : 60 return 8; 61 case '9' : 62 return 9; 63 64 case 'a' : 65 case 'A' : 66 return 10; 67 case 'b' : 68 case 'B' : 69 return 11; 70 case 'c' : 71 case 'C' : 72 return 12; 73 case 'd' : 74 case 'D' : 75 return 13; 76 case 'e' : 77 case 'E' : 78 return 14; 79 case 'f' : 80 case 'F' : 81 return 15; 82 } 83 84 throw new RuntimeException ("Illegal hex or unicode constant"); 85 } 87 88 static final int octval(char c) { 89 switch(c) { 90 case '0' : 91 return 0; 92 case '1' : 93 return 1; 94 case '2' : 95 return 2; 96 case '3' : 97 return 3; 98 case '4' : 99 return 4; 100 case '5' : 101 return 5; 102 case '6' : 103 return 6; 104 case '7' : 105 return 7; 106 case '8' : 107 return 8; 108 case '9' : 109 return 9; 110 111 case 'a' : 112 case 'A' : 113 return 10; 114 case 'b' : 115 case 'B' : 116 return 11; 117 case 'c' : 118 case 'C' : 119 return 12; 120 case 'd' : 121 case 'D' : 122 return 13; 123 case 'e' : 124 case 'E' : 125 return 14; 126 case 'f' : 127 case 'F' : 128 return 15; 129 } 130 131 throw new RuntimeException ("Illegal octal constant"); 132 } 134 135 public void setStringValue(String image) { 136 int l = image.length(); 137 StringBuffer sb = new StringBuffer (l); 138 for (int i=0; i<l; i++) { 139 char c = image.charAt(i); 140 if ((c == '\\') && (i+1<l)){ 141 i++; 142 c = image.charAt(i); 143 if (c=='n') c='\n'; 144 else if (c=='b') c = '\b'; 145 else if (c=='f') c = '\f'; 146 else if (c=='r') c = '\r'; 147 else if (c=='t') c = '\t'; 148 else if (c =='x') { 149 c = (char)(hexval(image.charAt(i+1)) << 4 | 150 hexval(image.charAt(i+1))); 151 i +=2; 152 } else if (c =='u') { 153 c = (char)(hexval(image.charAt(i+1)) << 12 | 154 hexval(image.charAt(i+2)) << 8 | 155 hexval(image.charAt(i+3)) << 4 | 156 hexval(image.charAt(i+4))); 157 i +=4; 158 } else if (c >='0' && c <= '7') { 159 c = (char)(octval(image.charAt(i))); 160 if ((image.length()>i) && 161 (image.charAt(i+1)>='0') && (image.charAt(i+1)<='7')) { 162 i++; 163 c = (char) ((c<<4) | octval(image.charAt(i))); 164 } 165 } 166 } 167 sb.append(c); 168 } 169 mValue = sb.toString(); 170 } 171 172 public void setDecimalValue(String image) { 173 try { 174 mValue = new Long (Long.parseLong(image)); 175 } catch (NumberFormatException e) { 176 mValue = new Double (image); 177 } 178 } 179 180 public void setOctalValue(String image) { 181 try { 182 String imageWithout0 = image.substring(1); 183 mValue = new Long (Long.parseLong(imageWithout0,8)); 184 } catch (NumberFormatException e) { 185 mValue = new Double (image); 186 } 187 } 188 189 public void setHexValue(String image) { 190 try { 191 String imageWithout0x = image.substring(2); 192 mValue = new Long (Long.parseLong(imageWithout0x,16)); 193 } catch (NumberFormatException e) { 194 mValue = new Double (image); 195 } 196 } 197 198 public void setFloatingPointValue(String image) { 199 mValue = new Double (image); 200 } 201 202 public void setBooleanValue(boolean value) { 203 mValue = new Boolean (value); 204 } 205 206 public void setNullValue() { 207 mValue = null; 208 } 209 210 public String toString() { 211 if (mValue == null) { 212 return "null"; 213 } 214 return "Literal(" + mValue.toString() + ")"; 215 } 216 217 } 218 | Popular Tags |