Initialize Tizen 2.3
[external/ragel.git] / ragel / common.h
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@complang.org>
3  */
4
5 /*  This file is part of Ragel.
6  *
7  *  Ragel is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  Ragel is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with Ragel; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20  */
21
22 #ifndef _COMMON_H
23 #define _COMMON_H
24
25 #include <fstream>
26 #include <climits>
27 #include "dlist.h"
28
29 /* Location in an input file. */
30 struct InputLoc
31 {
32         const char *fileName;
33         long line;
34         long col;
35 };
36
37
38 typedef unsigned long long Size;
39
40 struct Key
41 {
42 private:
43         long key;
44
45 public:
46         friend inline Key operator+(const Key key1, const Key key2);
47         friend inline Key operator-(const Key key1, const Key key2);
48         friend inline Key operator/(const Key key1, const Key key2);
49         friend inline long operator&(const Key key1, const Key key2);
50
51         friend inline bool operator<( const Key key1, const Key key2 );
52         friend inline bool operator<=( const Key key1, const Key key2 );
53         friend inline bool operator>( const Key key1, const Key key2 );
54         friend inline bool operator>=( const Key key1, const Key key2 );
55         friend inline bool operator==( const Key key1, const Key key2 );
56         friend inline bool operator!=( const Key key1, const Key key2 );
57
58         friend struct KeyOps;
59         
60         Key( ) {}
61         Key( const Key &key ) : key(key.key) {}
62         Key( long key ) : key(key) {}
63
64         /* Returns the value used to represent the key. This value must be
65          * interpreted based on signedness. */
66         long getVal() const { return key; };
67
68         /* Returns the key casted to a long long. This form of the key does not
69          * require any signedness interpretation. */
70         long long getLongLong() const;
71
72         /* Returns the distance from the key value to the maximum value that the
73          * key implementation can hold. */
74         Size availableSpace() const;
75
76         bool isUpper() const { return ( 'A' <= key && key <= 'Z' ); }
77         bool isLower() const { return ( 'a' <= key && key <= 'z' ); }
78         bool isPrintable() const
79         {
80             return ( 7 <= key && key <= 13 ) || ( 32 <= key && key < 127 );
81         }
82
83         Key toUpper() const
84                 { return Key( 'A' + ( key - 'a' ) ); }
85         Key toLower() const
86                 { return Key( 'a' + ( key - 'A' ) ); }
87
88         void operator+=( const Key other )
89         {
90                 /* FIXME: must be made aware of isSigned. */
91                 key += other.key;
92         }
93
94         void operator-=( const Key other )
95         {
96                 /* FIXME: must be made aware of isSigned. */
97                 key -= other.key;
98         }
99
100         void operator|=( const Key other )
101         {
102                 /* FIXME: must be made aware of isSigned. */
103                 key |= other.key;
104         }
105
106         /* Decrement. Needed only for ranges. */
107         inline void decrement();
108         inline void increment();
109 };
110
111 struct HostType
112 {
113         const char *data1;
114         const char *data2;
115         const char *internalName;
116         bool isSigned;
117         long long minVal;
118         long long maxVal;
119         unsigned int size;
120 };
121
122 struct HostLang
123 {
124         /* Target language. */
125         enum Lang
126         {
127                 C, D, Java, Ruby, CSharp
128         };
129
130         Lang lang;
131         HostType *hostTypes;
132         int numHostTypes;
133         HostType *defaultAlphType;
134         bool explicitUnsigned;
135 };
136
137 extern HostLang *hostLang;
138
139 extern HostLang hostLangC;
140 extern HostLang hostLangD;
141 extern HostLang hostLangJava;
142 extern HostLang hostLangRuby;
143 extern HostLang hostLangCSharp;
144
145 HostType *findAlphType( const char *s1 );
146 HostType *findAlphType( const char *s1, const char *s2 );
147 HostType *findAlphTypeInternal( const char *s1 );
148
149 /* An abstraction of the key operators that manages key operations such as
150  * comparison and increment according the signedness of the key. */
151 struct KeyOps
152 {
153         /* Default to signed alphabet. */
154         KeyOps() :
155                 isSigned(true),
156                 alphType(0)
157         {}
158
159         /* Default to signed alphabet. */
160         KeyOps( bool isSigned ) 
161                 :isSigned(isSigned) {}
162
163         bool isSigned;
164         Key minKey, maxKey;
165         HostType *alphType;
166
167         void setAlphType( HostType *alphType )
168         {
169                 this->alphType = alphType;
170                 isSigned = alphType->isSigned;
171                 if ( isSigned ) {
172                         minKey = (long) alphType->minVal;
173                         maxKey = (long) alphType->maxVal;
174                 }
175                 else {
176                         minKey = (long) (unsigned long) alphType->minVal; 
177                         maxKey = (long) (unsigned long) alphType->maxVal;
178                 }
179         }
180
181         /* Compute the distance between two keys. */
182         Size span( Key key1, Key key2 )
183         {
184                 return isSigned ? 
185                         (unsigned long long)(
186                                 (long long)key2.key - 
187                                 (long long)key1.key + 1) : 
188                         (unsigned long long)(
189                                 (unsigned long)key2.key) - 
190                                 (unsigned long long)((unsigned long)key1.key) + 1;
191         }
192
193         Size alphSize()
194                 { return span( minKey, maxKey ); }
195
196         HostType *typeSubsumes( long long maxVal )
197         {
198                 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
199                         if ( maxVal <= hostLang->hostTypes[i].maxVal )
200                                 return hostLang->hostTypes + i;
201                 }
202                 return 0;
203         }
204
205         HostType *typeSubsumes( bool isSigned, long long maxVal )
206         {
207                 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
208                         if ( ( ( isSigned && hostLang->hostTypes[i].isSigned ) || !isSigned ) &&
209                                         maxVal <= hostLang->hostTypes[i].maxVal )
210                                 return hostLang->hostTypes + i;
211                 }
212                 return 0;
213         }
214 };
215
216 extern KeyOps *keyOps;
217
218 inline bool operator<( const Key key1, const Key key2 )
219 {
220         return keyOps->isSigned ? key1.key < key2.key : 
221                 (unsigned long)key1.key < (unsigned long)key2.key;
222 }
223
224 inline bool operator<=( const Key key1, const Key key2 )
225 {
226         return keyOps->isSigned ?  key1.key <= key2.key : 
227                 (unsigned long)key1.key <= (unsigned long)key2.key;
228 }
229
230 inline bool operator>( const Key key1, const Key key2 )
231 {
232         return keyOps->isSigned ? key1.key > key2.key : 
233                 (unsigned long)key1.key > (unsigned long)key2.key;
234 }
235
236 inline bool operator>=( const Key key1, const Key key2 )
237 {
238         return keyOps->isSigned ? key1.key >= key2.key : 
239                 (unsigned long)key1.key >= (unsigned long)key2.key;
240 }
241
242 inline bool operator==( const Key key1, const Key key2 )
243 {
244         return key1.key == key2.key;
245 }
246
247 inline bool operator!=( const Key key1, const Key key2 )
248 {
249         return key1.key != key2.key;
250 }
251
252 /* Decrement. Needed only for ranges. */
253 inline void Key::decrement()
254 {
255         key = keyOps->isSigned ? key - 1 : ((unsigned long)key)-1;
256 }
257
258 /* Increment. Needed only for ranges. */
259 inline void Key::increment()
260 {
261         key = keyOps->isSigned ? key+1 : ((unsigned long)key)+1;
262 }
263
264 inline long long Key::getLongLong() const
265 {
266         return keyOps->isSigned ? (long long)key : (long long)(unsigned long)key;
267 }
268
269 inline Size Key::availableSpace() const
270 {
271         if ( keyOps->isSigned ) 
272                 return (long long)LONG_MAX - (long long)key;
273         else
274                 return (unsigned long long)ULONG_MAX - (unsigned long long)(unsigned long)key;
275 }
276         
277 inline Key operator+(const Key key1, const Key key2)
278 {
279         /* FIXME: must be made aware of isSigned. */
280         return Key( key1.key + key2.key );
281 }
282
283 inline Key operator-(const Key key1, const Key key2)
284 {
285         /* FIXME: must be made aware of isSigned. */
286         return Key( key1.key - key2.key );
287 }
288
289 inline long operator&(const Key key1, const Key key2)
290 {
291         /* FIXME: must be made aware of isSigned. */
292         return key1.key & key2.key;
293 }
294
295 inline Key operator/(const Key key1, const Key key2)
296 {
297         /* FIXME: must be made aware of isSigned. */
298         return key1.key / key2.key;
299 }
300
301 /* Filter on the output stream that keeps track of the number of lines
302  * output. */
303 class output_filter : public std::filebuf
304 {
305 public:
306         output_filter( const char *fileName ) : fileName(fileName), line(1) { }
307
308         virtual int sync();
309         virtual std::streamsize xsputn(const char* s, std::streamsize n);
310
311         const char *fileName;
312         int line;
313 };
314
315 class cfilebuf : public std::streambuf
316 {
317 public:
318         cfilebuf( char *fileName, FILE* file ) : fileName(fileName), file(file) { }
319         char *fileName;
320         FILE *file;
321
322         int sync()
323         {
324                 fflush( file );
325                 return 0;
326         }
327
328         int overflow( int c )
329         {
330                 if ( c != EOF )
331                         fputc( c, file );
332                 return 0;
333         }
334
335         std::streamsize xsputn( const char* s, std::streamsize n )
336         {
337                 std::streamsize written = fwrite( s, 1, n, file );
338                 return written;
339         }
340 };
341
342 class costream : public std::ostream
343 {
344 public:
345         costream( cfilebuf *b ) : 
346                 std::ostream(b), b(b) {}
347         
348         ~costream()
349                 { delete b; }
350
351         void fclose()
352                 { ::fclose( b->file ); }
353
354         cfilebuf *b;
355 };
356
357
358 const char *findFileExtension( const char *stemFile );
359 const char *fileNameFromStem( const char *stemFile, const char *suffix );
360
361 struct Export
362 {
363         Export( const char *name, Key key )
364                 : name(name), key(key) {}
365
366         const char *name;
367         Key key;
368
369         Export *prev, *next;
370 };
371
372 typedef DList<Export> ExportList;
373
374 struct exit_object { };
375 extern exit_object endp;
376 void operator<<( std::ostream &out, exit_object & );
377
378 #endif