KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > Literal


1 package org.jacorb.idl;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.io.PrintWriter JavaDoc;
24 import org.jacorb.idl.runtime.long_token;
25 import org.jacorb.idl.runtime.int_token;
26 import java.math.BigInteger JavaDoc;
27
28 /**
29  * @author Gerald Brose
30  * @version $Id: Literal.java,v 1.24 2004/05/06 12:39:58 nicolas Exp $
31  */

32
33 class Literal
34     extends IdlSymbol
35 {
36     private static BigInteger JavaDoc maximum;
37     public String JavaDoc 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         // const expressions containing literals can be declared
56
// outside cons declarations (e.g, in sequence bounds),
57
// but we care only for const declarations here.
58

59         if( declared_in != null )
60         {
61             TypeSpec ts = declared_in.const_type.symbol.typeSpec();
62             // If its an alias check the actual type not the alias
63
if (ts instanceof AliasTypeSpec)
64             {
65                 ts = ((AliasTypeSpec)ts).originalType ();
66             }
67
68             // If its unsigned may need to fit the value into a signed
69
// value.
70
if (ts instanceof IntType)
71             {
72                 if (((IntType)ts).unsigned &&
73                     ts instanceof LongLongType &&
74                     token instanceof fixed_token)
75                 {
76                     // Need to reset the unsigned value to fit into a
77
// signed long value.
78
if (maximum == null)
79                     {
80                         maximum = new BigInteger JavaDoc ("18446744073709551615");
81                     }
82                     BigInteger JavaDoc current = new BigInteger JavaDoc (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                     // Need to reset the unsigned value to fit into a
102
// signed integer value. Will need to replace the
103
// token so it is a int_token not a long.
104
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                 // Not unsigned but still have a long token for a Java Int type
124
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                 // Not unsigned but still have a fixed_token for a Java Long type
134
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 JavaDoc toString()
176     {
177         String JavaDoc 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 JavaDoc ps )
194     {
195         ps.print( escapeBackslash( string ));
196     }
197
198
199     /**
200      * Doubles up instances of the backslash character in a
201      * string, to avoid them being interpreted as escape sequences
202      *
203      * @param name a <code>String</code> value
204      * @return string
205      */

206     public static String JavaDoc escapeBackslash( String JavaDoc name )
207     {
208         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
209
210         char[] chrs = name.toCharArray();
211
212         // Don't bother escaping if we have "xxx"
213
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