1 28 29 package com.caucho.es.parser; 30 31 import com.caucho.es.ESBase; 32 import com.caucho.es.ESBoolean; 33 import com.caucho.es.ESNumber; 34 import com.caucho.es.ESString; 35 36 import java.io.IOException ; 37 38 41 class LiteralExpr extends Expr { 42 private ESBase value; 43 44 LiteralExpr(Block block, ESBase value) 45 { 46 super(block); 47 48 if (value == null) 49 value = ESBase.esNull; 50 51 this.value = value; 52 53 if (value instanceof ESNumber) { 54 double dv = 0; 55 56 try { 57 dv = value.toNum(); 58 } catch (Throwable e) { 59 } 60 61 if ((double) ((int) dv) == dv) 62 type = TYPE_INTEGER; 63 else if ((double) ((long) dv) == dv) 64 type = TYPE_LONG; 65 else 66 type = TYPE_NUMBER; 67 } 68 else if (value instanceof ESBoolean) 69 type = TYPE_BOOLEAN; 70 else if (value instanceof ESString) 71 type = TYPE_STRING; 72 else 73 type = TYPE_ES; 74 75 if (this.value == null) 76 throw new RuntimeException (); 77 } 78 79 82 ESBase getLiteral() 83 { 84 return value; 85 } 86 87 boolean isSimple() 88 { 89 return true; 90 } 91 92 void printInt32Impl() throws IOException 93 { 94 try { 95 cl.print(value.toInt32()); 96 } catch (Throwable e) { 97 } 98 } 99 100 void printNumImpl() throws IOException 101 { 102 try { 103 double v = value.toNum(); 104 105 if (Double.isInfinite(v)) 106 cl.print("Double.POSITIVE_INFINITY"); 107 else if (Double.isInfinite(-v)) 108 cl.print("Double.NEGATIVE_INFINITY"); 109 else if (Double.isNaN(v)) 110 cl.print("Double.NaN"); 111 else if ((long) v == v) 112 cl.print("(" + (long) v + "L)"); 113 else 114 cl.print("(" + v + "D)"); 115 } catch (Throwable e) { 116 e.printStackTrace(); 117 } 118 } 119 120 void printBooleanImpl() throws IOException 121 { 122 cl.print(value.toBoolean()); 123 } 124 125 128 void printStringImpl() throws IOException 129 { 130 try { 131 cl.print("\""); 132 String s = value.toStr().toString(); 133 for (int i = 0; i < s.length(); i++) { 134 char ch = s.charAt(i); 135 switch (ch) { 136 case '"': 137 cl.print("\\\""); 138 break; 139 case '\\': 140 cl.print("\\\\"); 141 break; 142 case '\n': 143 cl.print("\\n"); 144 break; 145 case '\r': 146 cl.print("\\r"); 147 break; 148 default: 149 cl.print(ch); 150 break; 151 } 152 } 153 cl.print("\""); 154 } catch (Throwable e) { 155 e.printStackTrace(); 156 } 157 } 158 159 void printStr() throws IOException 160 { 161 try { 162 printLiteral(value.toStr()); 163 } catch (Throwable e) { 164 e.printStackTrace(); 165 } 166 } 167 168 void print() throws IOException 169 { 170 printLiteral(value); 171 } 172 173 void printImpl() throws IOException 174 { 175 printLiteral(value); 176 } 177 178 public String toString() 179 { 180 return "[LiteralExpr " + value.toString() + "]"; 181 } 182 } 183 | Popular Tags |