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