Move simple interrupt handlers to winedos.
[wine] / tools / widl / parser.l
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2002 Ove Kaaven
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 %option stack
22 %option never-interactive
23
24 nl      \r?\n
25 ws      [ \f\t\r]
26 cident  [a-zA-Z_][0-9a-zA-Z_]*
27 int     [0-9]+
28 hexd    [0-9a-fA-F]
29 hex     0x{hexd}+
30 uuid    {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
31
32 %x QUOTE
33
34 %{
35
36 #include "config.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <assert.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46
47 #include "widl.h"
48 #include "utils.h"
49 #include "parser.h"
50 #include "../wpp/wpp.h"
51
52 #include "y.tab.h"
53
54 #define YY_USE_PROTOS
55 #define YY_NO_UNPUT
56 #define YY_NO_TOP_STATE
57
58 extern char *temp_name;
59
60 static void addcchar(char c);
61 static char *get_buffered_cstring(void);
62
63 static char *cbuffer;
64 static int cbufidx;
65 static int cbufalloc = 0;
66
67 static int kw_token(const char *kw);
68
69 #define MAX_IMPORT_DEPTH 10
70 struct {
71   YY_BUFFER_STATE state;
72   char *temp_name;
73 } import_stack[MAX_IMPORT_DEPTH];
74 int import_stack_ptr = 0;
75
76 static void pop_import(void);
77
78 static UUID* parse_uuid(const char*u)
79 {
80   UUID* uuid = xmalloc(sizeof(UUID));
81   char b[3];
82   /* it would be nice to use UuidFromStringA */
83   uuid->Data1 = strtol(u, NULL, 16);
84   uuid->Data2 = strtol(u+9, NULL, 16);
85   uuid->Data3 = strtol(u+14, NULL, 16);
86   b[2] = 0;
87   memcpy(b, u+19, 2); uuid->Data4[0] = strtol(b, NULL, 16);
88   memcpy(b, u+21, 2); uuid->Data4[1] = strtol(b, NULL, 16);
89   memcpy(b, u+24, 2); uuid->Data4[2] = strtol(b, NULL, 16);
90   memcpy(b, u+26, 2); uuid->Data4[3] = strtol(b, NULL, 16);
91   memcpy(b, u+28, 2); uuid->Data4[4] = strtol(b, NULL, 16);
92   memcpy(b, u+30, 2); uuid->Data4[5] = strtol(b, NULL, 16);
93   memcpy(b, u+32, 2); uuid->Data4[6] = strtol(b, NULL, 16);
94   memcpy(b, u+34, 2); uuid->Data4[7] = strtol(b, NULL, 16);
95   return uuid;
96 }
97
98 %}
99
100 /*
101  **************************************************************************
102  * The flexer starts here
103  **************************************************************************
104  */
105 %%
106 ^#.*
107 \"                      yy_push_state(QUOTE); cbufidx = 0;
108 <QUOTE>\"               {
109                                 yy_pop_state();
110                                 yylval.str = get_buffered_cstring();
111                                 return aSTRING;
112                         }
113 <QUOTE>\\\\             |
114 <QUOTE>\\\"             addcchar(yytext[1]);
115 <QUOTE>\\.              addcchar('\\'); addcchar(yytext[1]);
116 <QUOTE>.                addcchar(yytext[0]);
117 {uuid}                  {
118                                 yylval.uuid = parse_uuid(yytext);
119                                 return aUUID;
120                         }
121 {hex}                   |
122 {int}                   {
123                                 yylval.num = strtol(yytext, NULL, 0);
124                                 return aNUM;
125                         }
126 {cident}                return kw_token(yytext);
127 \n
128 {ws}
129 \<\<                    return SHL;
130 \>\>                    return SHR;
131 .                       return yytext[0];
132 <<EOF>>                 {
133                                 if (import_stack_ptr) pop_import();
134                                 else yyterminate();
135                         }
136 %%
137
138 #ifndef yywrap
139 int yywrap(void)
140 {
141         return 1;
142 }
143 #endif
144
145 static struct {
146         const char *kw;
147         int token;
148         int val;
149 } keywords[] = {
150         {"__cdecl",                     tCDECL},
151         {"__int64",                     tINT64},
152         {"__stdcall",                   tSTDCALL},
153         {"_stdcall",                    tSTDCALL},
154         {"aggregatable",                tAGGREGATABLE},
155         {"allocate",                    tALLOCATE},
156         {"appobject",                   tAPPOBJECT},
157         {"arrays",                      tARRAYS},
158         {"async",                       tASYNC},
159         {"async_uuid",                  tASYNCUUID},
160         {"auto_handle",                 tAUTOHANDLE},
161         {"bindable",                    tBINDABLE},
162         {"boolean",                     tBOOLEAN},
163         {"broadcast",                   tBROADCAST},
164         {"byte",                        tBYTE},
165         {"byte_count",                  tBYTECOUNT},
166         {"call_as",                     tCALLAS},
167         {"callback",                    tCALLBACK},
168         {"case",                        tCASE},
169         {"char",                        tCHAR},
170         {"coclass",                     tCOCLASS},
171         {"code",                        tCODE},
172         {"comm_status",                 tCOMMSTATUS},
173         {"const",                       tCONST},
174         {"context_handle",              tCONTEXTHANDLE},
175         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
176         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
177         {"control",                     tCONTROL},
178         {"cpp_quote",                   tCPPQUOTE},
179 /* ... */
180         {"default",                     tDEFAULT},
181 /* ... */
182         {"double",                      tDOUBLE},
183 /* ... */
184         {"enum",                        tENUM},
185 /* ... */
186         {"extern",                      tEXTERN},
187 /* ... */
188         {"float",                       tFLOAT},
189 /* ... */
190         {"hyper",                       tHYPER},
191 /* ... */
192         {"iid_is",                      tIIDIS},
193 /* ... */
194         {"import",                      tIMPORT},
195         {"importlib",                   tIMPORTLIB},
196         {"in",                          tIN},
197         {"include",                     tINCLUDE},
198         {"in_line",                     tINLINE},
199         {"int",                         tINT},
200 /* ... */
201         {"interface",                   tINTERFACE},
202 /* ... */
203         {"length_is",                   tLENGTHIS},
204 /* ... */
205         {"local",                       tLOCAL},
206         {"long",                        tLONG},
207 /* ... */
208         {"object",                      tOBJECT},
209         {"odl",                         tODL},
210         {"oleautomation",               tOLEAUTOMATION},
211 /* ... */
212         {"out",                         tOUT},
213 /* ... */
214         {"pointer_default",             tPOINTERDEFAULT},
215 /* ... */
216         {"ref",                         tREF},
217 /* ... */
218         {"short",                       tSHORT},
219         {"signed",                      tSIGNED},
220         {"size_is",                     tSIZEIS},
221         {"sizeof",                      tSIZEOF},
222 /* ... */
223         {"string",                      tSTRING},
224         {"struct",                      tSTRUCT},
225         {"switch",                      tSWITCH},
226         {"switch_is",                   tSWITCHIS},
227         {"switch_type",                 tSWITCHTYPE},
228 /* ... */
229         {"typedef",                     tTYPEDEF},
230         {"union",                       tUNION},
231 /* ... */
232         {"unique",                      tUNIQUE},
233         {"unsigned",                    tUNSIGNED},
234 /* ... */
235         {"uuid",                        tUUID},
236         {"v1_enum",                     tV1ENUM},
237 /* ... */
238         {"version",                     tVERSION},
239         {"void",                        tVOID},
240         {"wchar_t",                     tWCHAR},
241         {"wire_marshal",                tWIREMARSHAL},
242         {NULL}
243 };
244
245 static int kw_token(const char *kw)
246 {
247         int i;
248         for (i=0; keywords[i].kw; i++)
249                 if (strcmp(kw, keywords[i].kw) == 0)
250                         return keywords[i].token;
251         yylval.str = xstrdup(kw);
252         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
253 }
254
255 static void addcchar(char c)
256 {
257         if(cbufidx >= cbufalloc)
258         {
259                 cbufalloc += 1024;
260                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
261                 if(cbufalloc > 65536)
262                         yywarning("Reallocating string buffer larger than 64kB");
263         }
264         cbuffer[cbufidx++] = c;
265 }
266
267 static char *get_buffered_cstring(void)
268 {
269         addcchar(0);
270         return xstrdup(cbuffer);
271 }
272
273 static void pop_import(void)
274 {
275         int ptr = import_stack_ptr-1;
276
277         fclose(yyin);
278         yy_delete_buffer( YY_CURRENT_BUFFER );
279         yy_switch_to_buffer( import_stack[ptr].state );
280         if (temp_name) {
281                 unlink(temp_name);
282                 free(temp_name);
283         }
284         temp_name = import_stack[ptr].temp_name;
285         import_stack_ptr--;
286 }
287
288 struct imports {
289         char *name;
290         struct imports *next;
291 } *first_import;
292
293 void do_import(char *fname)
294 {
295         FILE *f;
296         char *hname, *path;
297         struct imports *import;
298         int ptr = import_stack_ptr;
299         int ret;
300
301         if (!parse_only) {
302                 hname = dup_basename(fname, ".idl");
303                 strcat(hname, ".h");
304
305                 fprintf(header, "#include \"%s\"\n", hname);
306                 free(hname);
307         }
308
309         import = first_import;
310         while (import && strcmp(import->name, fname))
311                 import = import->next;
312         if (import) return; /* already imported */
313
314         import = xmalloc(sizeof(struct imports));
315         import->name = xstrdup(fname);
316         import->next = first_import;
317         first_import = import;
318
319         if (!(path = wpp_find_include( fname, 1 )))
320             yyerror("Unable to open include file %s", fname);
321
322         import_stack[ptr].temp_name = temp_name;
323         import_stack_ptr++;
324
325         ret = wpp_parse_temp( path, NULL, &temp_name );
326         free( path );
327         if (ret) exit(1);
328
329         if((f = fopen(temp_name, "r")) == NULL)
330                 yyerror("Unable to open %s", temp_name);
331
332         import_stack[ptr].state = YY_CURRENT_BUFFER;
333         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
334 }
335
336 void abort_import(void)
337 {
338         int ptr;
339
340         for (ptr=0; ptr<import_stack_ptr; ptr++)
341                 unlink(import_stack[ptr].temp_name);
342 }