1 package org.jacorb.idl; 2 3 22 23 import java.io.PrintWriter ; 24 import org.jacorb.idl.runtime.long_token; 25 import org.jacorb.idl.runtime.int_token; 26 import java.math.BigInteger ; 27 28 32 33 class Literal 34 extends IdlSymbol 35 { 36 private static BigInteger maximum; 37 public String string; 38 public boolean wide; 39 public org.jacorb.idl.runtime.token token; 40 41 private ConstDecl declared_in; 42 43 public Literal( int num ) 44 { 45 super( num ); 46 } 47 48 public void setDeclaration( ConstDecl declared_in ) 49 { 50 this.declared_in = declared_in; 51 } 52 53 public void parse() 54 { 55 59 if( declared_in != null ) 60 { 61 TypeSpec ts = declared_in.const_type.symbol.typeSpec(); 62 if (ts instanceof AliasTypeSpec) 64 { 65 ts = ((AliasTypeSpec)ts).originalType (); 66 } 67 68 if (ts instanceof IntType) 71 { 72 if (((IntType)ts).unsigned && 73 ts instanceof LongLongType && 74 token instanceof fixed_token) 75 { 76 if (maximum == null) 79 { 80 maximum = new BigInteger ("18446744073709551615"); 81 } 82 BigInteger current = new BigInteger (string); 83 if (current.compareTo (maximum ) > 0) 84 { 85 parser.error ("Value too big for unsigned long"); 86 } 87 else 88 { 89 token = new long_token 90 ( 91 ((fixed_token)token).sym, 92 ((fixed_token)token).fixed_val.longValue () 93 ); 94 string = Long.toString (((long_token)token).long_val); 95 } 96 } 97 else if (((IntType)ts).unsigned == true && 98 ts instanceof LongType && 99 token instanceof long_token) 100 { 101 if (((long_token)token).long_val > 4294967295L ) 105 { 106 parser.error 107 ( 108 "Value (" + 109 ((long_token)token).long_val + 110 ") too big for unsigned long" 111 ); 112 } 113 else 114 { 115 token = new int_token 116 ( 117 ((long_token)token).sym, 118 (int)((long_token)token).long_val 119 ); 120 string = Integer.toString (((int_token)token).int_val); 121 } 122 } 123 else if (ts instanceof LongType && token instanceof long_token) 125 { 126 parser.error 127 ( 128 "Value (" + 129 ((long_token)token).long_val + 130 ") too big for Java int" 131 ); 132 } 133 else if (ts instanceof LongLongType && token instanceof fixed_token) 135 { 136 parser.error 137 ( 138 "Value (" + 139 ((fixed_token)token).fixed_val.toString () + 140 ") too big for Java long" 141 ); 142 } 143 } 144 145 if( logger.isWarnEnabled() ) 146 logger.warn( "Literal " + ts.getClass().getName() + " " + 147 ( token != null? token.getClass().getName() :"<no token>" ) ); 148 149 if( ts instanceof FloatPtType && 150 !( token instanceof org.jacorb.idl.runtime.float_token ) ) 151 { 152 parser.error( "Expecting float/double constant!" ); 153 } 154 else if( ts instanceof FixedPointConstType && 155 !( token instanceof fixed_token ) ) 156 { 157 parser.error( "Expecting fixed point constant (perhaps a missing \"d\")!" ); 158 } 159 else if( ts instanceof StringType ) 160 { 161 if( wide && !( (StringType)ts ).isWide() ) 162 parser.error( "Illegal assignment of wide string constant to string!" ); 163 164 } 165 else if( ( ts instanceof LongType ) || ( ts instanceof ShortType ) ) 166 { 167 if( token instanceof org.jacorb.idl.runtime.long_token ) 168 { 169 parser.error( "Illegal assignment from long long" ); 170 } 171 } 172 } 173 } 174 175 public String toString() 176 { 177 String result = string; 178 179 if (token instanceof org.jacorb.idl.runtime.long_token) 180 { 181 if (string.indexOf( '.' ) > 0 ) 182 { 183 result = (string + 'D'); 184 } 185 else 186 { 187 result = (string + 'L'); 188 } 189 } 190 return escapeBackslash (result); 191 } 192 193 public void print( PrintWriter ps ) 194 { 195 ps.print( escapeBackslash( string )); 196 } 197 198 199 206 public static String escapeBackslash( String name ) 207 { 208 StringBuffer result = new StringBuffer (); 209 210 char[] chrs = name.toCharArray(); 211 212 if( chrs[ 0 ] == '\"' ) 214 { 215 return name; 216 } 217 218 for( int i = 0; i < chrs.length; i++ ) 219 { 220 switch( chrs[ i ] ) 221 { 222 case '\n': 223 { 224 result.append( '\\' ); 225 result.append( 'n' ); 226 break; 227 } 228 case '\t': 229 { 230 result.append( '\\' ); 231 result.append( 't' ); 232 break; 233 } 234 case '\013': 235 { 236 result.append( '\\' ); 237 result.append( "013" ); 238 break; 239 } 240 case '\b': 241 { 242 result.append( '\\' ); 243 result.append( 'b' ); 244 break; 245 } 246 case '\r': 247 { 248 result.append( '\\' ); 249 result.append( 'r' ); 250 break; 251 } 252 case '\f': 253 { 254 result.append( '\\' ); 255 result.append( 'f' ); 256 break; 257 } 258 case '\007': 259 { 260 result.append( '\\' ); 261 result.append( "007" ); 262 break; 263 } 264 case '\\': 265 { 266 result.append( '\\' ); 267 result.append( '\\' ); 268 break; 269 } 270 case '\0': 271 { 272 result.append( '\\' ); 273 result.append( '0' ); 274 break; 275 } 276 case '\'': 277 { 278 if( i == 1 ) 279 { 280 result.append( '\\' ); 281 result.append( '\'' ); 282 } 283 else 284 { 285 result.append( chrs[ i ] ); 286 } 287 break; 288 } 289 case '\"': 290 { 291 if( i == 1 ) 292 { 293 result.append( '\\' ); 294 result.append( '\"' ); 295 } 296 else 297 { 298 result.append( chrs[ i ] ); 299 } 300 break; 301 } 302 default: 303 { 304 result.append( chrs[ i ] ); 305 } 306 } 307 } 308 return result.toString(); 309 } 310 } 311 | Popular Tags |