c0470d3c2676e202e2392d9c7a0587500f062600
[external/ragel.git] / common / common.h
1 /*
2  *  Copyright 2001-2006 Adrian Thurston <thurston@cs.queensu.ca>
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 typedef unsigned long long Size;
30
31 struct Key
32 {
33 private:
34         long key;
35
36 public:
37         friend inline Key operator+(const Key key1, const Key key2);
38         friend inline Key operator-(const Key key1, const Key key2);
39         friend inline Key operator/(const Key key1, const Key key2);
40         friend inline long operator&(const Key key1, const Key key2);
41
42         friend inline bool operator<( const Key key1, const Key key2 );
43         friend inline bool operator<=( const Key key1, const Key key2 );
44         friend inline bool operator>( const Key key1, const Key key2 );
45         friend inline bool operator>=( const Key key1, const Key key2 );
46         friend inline bool operator==( const Key key1, const Key key2 );
47         friend inline bool operator!=( const Key key1, const Key key2 );
48
49         friend struct KeyOps;
50         
51         Key( ) {}
52         Key( const Key &key ) : key(key.key) {}
53         Key( long key ) : key(key) {}
54
55         /* Returns the value used to represent the key. This value must be
56          * interpreted based on signedness. */
57         long getVal() const { return key; };
58
59         /* Returns the key casted to a long long. This form of the key does not
60          * require and signedness interpretation. */
61         long long getLongLong() const;
62
63         bool isUpper() const { return ( 'A' <= key && key <= 'Z' ); }
64         bool isLower() const { return ( 'a' <= key && key <= 'z' ); }
65         bool isPrintable() const
66         {
67             return ( 7 <= key && key <= 13 ) || ( 32 <= key && key < 127 );
68         }
69
70         Key toUpper() const
71                 { return Key( 'A' + ( key - 'a' ) ); }
72         Key toLower() const
73                 { return Key( 'a' + ( key - 'A' ) ); }
74
75         void operator+=( const Key other )
76         {
77                 /* FIXME: must be made aware of isSigned. */
78                 key += other.key;
79         }
80
81         void operator-=( const Key other )
82         {
83                 /* FIXME: must be made aware of isSigned. */
84                 key -= other.key;
85         }
86
87         void operator|=( const Key other )
88         {
89                 /* FIXME: must be made aware of isSigned. */
90                 key |= other.key;
91         }
92
93         /* Decrement. Needed only for ranges. */
94         inline void decrement();
95         inline void increment();
96 };
97
98 struct HostType
99 {
100         char *data1;
101         char *data2;
102         char *internalName;
103         bool isSigned;
104         long long minVal;
105         long long maxVal;
106         unsigned int size;
107 };
108
109 struct HostLang
110 {
111         /* Target language. */
112         enum Lang
113         {
114                 C, D, Java, Ruby
115         };
116
117         Lang lang;
118         HostType *hostTypes;
119         int numHostTypes;
120         HostType *defaultAlphType;
121         bool explicitUnsigned;
122 };
123
124 extern HostLang *hostLang;
125
126 extern HostLang hostLangC;
127 extern HostLang hostLangD;
128 extern HostLang hostLangJava;
129 extern HostLang hostLangRuby;
130
131 HostType *findAlphType( char *s1 );
132 HostType *findAlphType( char *s1, char *s2 );
133 HostType *findAlphTypeInternal( char *s1 );
134
135 /* An abstraction of the key operators that manages key operations such as
136  * comparison and increment according the signedness of the key. */
137 struct KeyOps
138 {
139         /* Default to signed alphabet. */
140         KeyOps() :
141                 isSigned(true),
142                 alphType(0)
143         {}
144
145         /* Default to signed alphabet. */
146         KeyOps( bool isSigned ) 
147                 :isSigned(isSigned) {}
148
149         bool isSigned;
150         Key minKey, maxKey;
151         HostType *alphType;
152
153         void setAlphType( HostType *alphType )
154         {
155                 this->alphType = alphType;
156                 isSigned = alphType->isSigned;
157                 if ( isSigned ) {
158                         minKey = (long) alphType->minVal;
159                         maxKey = (long) alphType->maxVal;
160                 }
161                 else {
162                         minKey = (long) (unsigned long) alphType->minVal; 
163                         maxKey = (long) (unsigned long) alphType->maxVal;
164                 }
165         }
166
167         /* Compute the distance between two keys. */
168         Size span( Key key1, Key key2 )
169         {
170                 return isSigned ? 
171                         (unsigned long long)(
172                                 (long long)key2.key - 
173                                 (long long)key1.key + 1) : 
174                         (unsigned long long)(
175                                 (unsigned long)key2.key) - 
176                                 (unsigned long long)((unsigned long)key1.key) + 1;
177         }
178
179         Size alphSize()
180                 { return span( minKey, maxKey ); }
181
182         HostType *typeSubsumes( long long maxVal )
183         {
184                 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
185                         if ( maxVal <= hostLang->hostTypes[i].maxVal )
186                                 return hostLang->hostTypes + i;
187                 }
188                 return 0;
189         }
190
191         HostType *typeSubsumes( bool isSigned, long long maxVal )
192         {
193                 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
194                         if ( ( isSigned && hostLang->hostTypes[i].isSigned || !isSigned ) &&
195                                         maxVal <= hostLang->hostTypes[i].maxVal )
196                                 return hostLang->hostTypes + i;
197                 }
198                 return 0;
199         }
200 };
201
202 extern KeyOps *keyOps;
203
204 inline bool operator<( const Key key1, const Key key2 )
205 {
206         return keyOps->isSigned ? key1.key < key2.key : 
207                 (unsigned long)key1.key < (unsigned long)key2.key;
208 }
209
210 inline bool operator<=( const Key key1, const Key key2 )
211 {
212         return keyOps->isSigned ?  key1.key <= key2.key : 
213                 (unsigned long)key1.key <= (unsigned long)key2.key;
214 }
215
216 inline bool operator>( const Key key1, const Key key2 )
217 {
218         return keyOps->isSigned ? key1.key > key2.key : 
219                 (unsigned long)key1.key > (unsigned long)key2.key;
220 }
221
222 inline bool operator>=( const Key key1, const Key key2 )
223 {
224         return keyOps->isSigned ? key1.key >= key2.key : 
225                 (unsigned long)key1.key >= (unsigned long)key2.key;
226 }
227
228 inline bool operator==( const Key key1, const Key key2 )
229 {
230         return key1.key == key2.key;
231 }
232
233 inline bool operator!=( const Key key1, const Key key2 )
234 {
235         return key1.key != key2.key;
236 }
237
238 /* Decrement. Needed only for ranges. */
239 inline void Key::decrement()
240 {
241         key = keyOps->isSigned ? key - 1 : ((unsigned long)key)-1;
242 }
243
244 /* Increment. Needed only for ranges. */
245 inline void Key::increment()
246 {
247         key = keyOps->isSigned ? key+1 : ((unsigned long)key)+1;
248 }
249
250 inline long long Key::getLongLong() const
251 {
252         return keyOps->isSigned ? (long long)key : (long long)(unsigned long)key;
253 }
254
255 inline Key operator+(const Key key1, const Key key2)
256 {
257         /* FIXME: must be made aware of isSigned. */
258         return Key( key1.key + key2.key );
259 }
260
261 inline Key operator-(const Key key1, const Key key2)
262 {
263         /* FIXME: must be made aware of isSigned. */
264         return Key( key1.key - key2.key );
265 }
266
267 inline long operator&(const Key key1, const Key key2)
268 {
269         /* FIXME: must be made aware of isSigned. */
270         return key1.key & key2.key;
271 }
272
273 inline Key operator/(const Key key1, const Key key2)
274 {
275         /* FIXME: must be made aware of isSigned. */
276         return key1.key / key2.key;
277 }
278
279 /* Filter on the output stream that keeps track of the number of lines
280  * output. */
281 class output_filter : public std::filebuf
282 {
283 public:
284         output_filter( char *fileName ) : fileName(fileName), line(1) { }
285
286         virtual int sync();
287         virtual std::streamsize xsputn(const char* s, std::streamsize n);
288
289         char *fileName;
290         int line;
291 };
292
293 char *findFileExtension( char *stemFile );
294 char *fileNameFromStem( char *stemFile, char *suffix );
295
296 struct Export
297 {
298         Export( char *name, Key key )
299                 : name(name), key(key) {}
300
301         char *name;
302         Key key;
303
304         Export *prev, *next;
305 };
306
307 typedef DList<Export> ExportList;
308
309 struct exit_object { };
310 extern exit_object endp;
311 void operator<<( std::ostream &out, exit_object & );
312
313 #endif /* _COMMON_H */