Use <> format in generated #include statements.
[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 %x pp_line
34
35 %{
36
37 #include "config.h"
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <assert.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47
48 #include "widl.h"
49 #include "utils.h"
50 #include "parser.h"
51 #include "wine/wpp.h"
52
53 #include "y.tab.h"
54
55 #define YY_USE_PROTOS
56 #define YY_NO_UNPUT
57 #define YY_NO_TOP_STATE
58
59 extern char *temp_name;
60
61 static void addcchar(char c);
62 static char *get_buffered_cstring(void);
63
64 static char *cbuffer;
65 static int cbufidx;
66 static int cbufalloc = 0;
67
68 static int kw_token(const char *kw);
69
70 #define MAX_IMPORT_DEPTH 10
71 struct {
72   YY_BUFFER_STATE state;
73   char *input_name;
74   int   line_number;
75   char *temp_name;
76 } import_stack[MAX_IMPORT_DEPTH];
77 int import_stack_ptr = 0;
78
79 static void pop_import(void);
80
81 static UUID* parse_uuid(const char*u)
82 {
83   UUID* uuid = xmalloc(sizeof(UUID));
84   char b[3];
85   /* it would be nice to use UuidFromStringA */
86   uuid->Data1 = strtoul(u, NULL, 16);
87   uuid->Data2 = strtoul(u+9, NULL, 16);
88   uuid->Data3 = strtoul(u+14, NULL, 16);
89   b[2] = 0;
90   memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
91   memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
92   memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
93   memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
94   memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
95   memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
96   memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
97   memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
98   return uuid;
99 }
100
101 %}
102
103 /*
104  **************************************************************************
105  * The flexer starts here
106  **************************************************************************
107  */
108 %%
109 <INITIAL>^{ws}*\#{ws}*  yy_push_state(pp_line);
110 <pp_line>[^\n]*         {
111                             int lineno;
112                             char *cptr, *fname;
113                             yy_pop_state();
114                             lineno = (int)strtol(yytext, &cptr, 10);
115                             if(!lineno)
116                                 yyerror("Malformed '#...' line-directive; invalid linenumber");
117                             fname = strchr(cptr, '"');
118                             if(!fname)
119                                 yyerror("Malformed '#...' line-directive; missing filename");
120                             fname++;
121                             cptr = strchr(fname, '"');
122                             if(!cptr)
123                                 yyerror("Malformed '#...' line-directive; missing terminating \"");
124                             *cptr = '\0';
125                             line_number = lineno - 1;  /* We didn't read the newline */
126                             free( input_name );
127                             input_name = xstrdup(fname);
128                         }
129 \"                      yy_push_state(QUOTE); cbufidx = 0;
130 <QUOTE>\"               {
131                                 yy_pop_state();
132                                 yylval.str = get_buffered_cstring();
133                                 return aSTRING;
134                         }
135 <QUOTE>\\\\             |
136 <QUOTE>\\\"             addcchar(yytext[1]);
137 <QUOTE>\\.              addcchar('\\'); addcchar(yytext[1]);
138 <QUOTE>.                addcchar(yytext[0]);
139 {uuid}                  {
140                                 yylval.uuid = parse_uuid(yytext);
141                                 return aUUID;
142                         }
143 {hex}                   {
144                                 yylval.num = strtoul(yytext, NULL, 0);
145                                 return aHEXNUM;
146                         }
147 {int}                   {
148                                 yylval.num = strtoul(yytext, NULL, 0);
149                                 return aNUM;
150                         }
151 {cident}                return kw_token(yytext);
152 \n                      line_number++;
153 {ws}
154 \<\<                    return SHL;
155 \>\>                    return SHR;
156 .                       return yytext[0];
157 <<EOF>>                 {
158                                 if (import_stack_ptr) {
159                                         pop_import();
160                                         return aEOF;
161                                 }
162                                 else yyterminate();
163                         }
164 %%
165
166 #ifndef yywrap
167 int yywrap(void)
168 {
169         return 1;
170 }
171 #endif
172
173 static struct keyword {
174         const char *kw;
175         int token;
176         int val;
177 } keywords[] = {
178         {"__cdecl",                     tCDECL},
179         {"__int64",                     tINT64},
180         {"__stdcall",                   tSTDCALL},
181         {"_stdcall",                    tSTDCALL},
182         {"aggregatable",                tAGGREGATABLE},
183         {"allocate",                    tALLOCATE},
184         {"appobject",                   tAPPOBJECT},
185         {"arrays",                      tARRAYS},
186         {"async",                       tASYNC},
187         {"async_uuid",                  tASYNCUUID},
188         {"auto_handle",                 tAUTOHANDLE},
189         {"bindable",                    tBINDABLE},
190         {"boolean",                     tBOOLEAN},
191         {"broadcast",                   tBROADCAST},
192         {"byte",                        tBYTE},
193         {"byte_count",                  tBYTECOUNT},
194         {"call_as",                     tCALLAS},
195         {"callback",                    tCALLBACK},
196         {"case",                        tCASE},
197         {"char",                        tCHAR},
198         {"coclass",                     tCOCLASS},
199         {"code",                        tCODE},
200         {"comm_status",                 tCOMMSTATUS},
201         {"const",                       tCONST},
202         {"context_handle",              tCONTEXTHANDLE},
203         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
204         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
205         {"control",                     tCONTROL},
206         {"cpp_quote",                   tCPPQUOTE},
207 /* ... */
208         {"default",                     tDEFAULT},
209 /* ... */
210         {"double",                      tDOUBLE},
211 /* ... */
212         {"enum",                        tENUM},
213         {"error_status_t",              tERRORSTATUST},
214 /* ... */
215         {"extern",                      tEXTERN},
216 /* ... */
217         {"float",                       tFLOAT},
218 /* ... */
219         {"handle_t",                    tHANDLET},
220 /* ... */
221         {"hyper",                       tHYPER},
222 /* ... */
223         {"idempotent",                  tIDEMPOTENT},
224 /* ... */
225         {"iid_is",                      tIIDIS},
226 /* ... */
227         {"import",                      tIMPORT},
228         {"importlib",                   tIMPORTLIB},
229         {"in",                          tIN},
230         {"include",                     tINCLUDE},
231         {"in_line",                     tINLINE},
232         {"int",                         tINT},
233 /* ... */
234         {"interface",                   tINTERFACE},
235 /* ... */
236         {"length_is",                   tLENGTHIS},
237 /* ... */
238         {"local",                       tLOCAL},
239         {"long",                        tLONG},
240 /* ... */
241         {"object",                      tOBJECT},
242         {"odl",                         tODL},
243         {"oleautomation",               tOLEAUTOMATION},
244 /* ... */
245         {"out",                         tOUT},
246 /* ... */
247         {"pointer_default",             tPOINTERDEFAULT},
248 /* ... */
249         {"ref",                         tREF},
250 /* ... */
251         {"short",                       tSHORT},
252         {"signed",                      tSIGNED},
253         {"size_is",                     tSIZEIS},
254         {"sizeof",                      tSIZEOF},
255 /* ... */
256         {"string",                      tSTRING},
257         {"struct",                      tSTRUCT},
258         {"switch",                      tSWITCH},
259         {"switch_is",                   tSWITCHIS},
260         {"switch_type",                 tSWITCHTYPE},
261 /* ... */
262         {"typedef",                     tTYPEDEF},
263         {"union",                       tUNION},
264 /* ... */
265         {"unique",                      tUNIQUE},
266         {"unsigned",                    tUNSIGNED},
267 /* ... */
268         {"uuid",                        tUUID},
269         {"v1_enum",                     tV1ENUM},
270 /* ... */
271         {"version",                     tVERSION},
272         {"void",                        tVOID},
273         {"wchar_t",                     tWCHAR},
274         {"wire_marshal",                tWIREMARSHAL}
275 };
276 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
277 #define KWP(p) ((struct keyword *)(p))
278
279 static int kw_cmp_func(const void *s1, const void *s2)
280 {
281         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
282 }
283
284 #define KW_BSEARCH
285 static int kw_token(const char *kw)
286 {
287         struct keyword key, *kwp;
288         key.kw = kw;
289 #ifdef KW_BSEARCH
290         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
291 #else
292         {
293                 int i;
294                 for (kwp=NULL, i=0; i < NKEYWORDS; i++)
295                         if (!kw_cmp_func(&key, &keywords[i])) {
296                                 kwp = &keywords[i];
297                                 break;
298                         }
299         }
300 #endif
301         if (kwp) {
302                 yylval.str = (char*)kwp->kw;
303                 return kwp->token;
304         }
305         yylval.str = xstrdup(kw);
306         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
307 }
308
309 static void addcchar(char c)
310 {
311         if(cbufidx >= cbufalloc)
312         {
313                 cbufalloc += 1024;
314                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
315                 if(cbufalloc > 65536)
316                         yywarning("Reallocating string buffer larger than 64kB");
317         }
318         cbuffer[cbufidx++] = c;
319 }
320
321 static char *get_buffered_cstring(void)
322 {
323         addcchar(0);
324         return xstrdup(cbuffer);
325 }
326
327 static void pop_import(void)
328 {
329         int ptr = import_stack_ptr-1;
330
331         fclose(yyin);
332         yy_delete_buffer( YY_CURRENT_BUFFER );
333         yy_switch_to_buffer( import_stack[ptr].state );
334         if (temp_name) {
335                 unlink(temp_name);
336                 free(temp_name);
337         }
338         temp_name = import_stack[ptr].temp_name;
339         free( input_name );
340         input_name = import_stack[ptr].input_name;
341         line_number = import_stack[ptr].line_number;
342         import_stack_ptr--;
343 }
344
345 struct imports {
346         char *name;
347         struct imports *next;
348 } *first_import;
349
350 int do_import(char *fname)
351 {
352         FILE *f;
353         char *hname, *path, *p;
354         struct imports *import;
355         int ptr = import_stack_ptr;
356         int ret;
357
358         if (!parse_only) {
359                 hname = dup_basename(fname, ".idl");
360                 p = hname + strlen(hname) - 2;
361                 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
362
363                 fprintf(header, "#include <%s>\n", hname);
364                 free(hname);
365         }
366
367         import = first_import;
368         while (import && strcmp(import->name, fname))
369                 import = import->next;
370         if (import) return 0; /* already imported */
371
372         import = xmalloc(sizeof(struct imports));
373         import->name = xstrdup(fname);
374         import->next = first_import;
375         first_import = import;
376
377         if (!(path = wpp_find_include( fname, 1 )))
378             yyerror("Unable to open include file %s", fname);
379
380         import_stack[ptr].temp_name = temp_name;
381         import_stack[ptr].input_name = input_name;
382         import_stack[ptr].line_number = line_number;
383         import_stack_ptr++;
384         input_name = path;
385         line_number = 1;
386
387         ret = wpp_parse_temp( path, NULL, &temp_name );
388         if (ret) exit(1);
389
390         if((f = fopen(temp_name, "r")) == NULL)
391                 yyerror("Unable to open %s", temp_name);
392
393         import_stack[ptr].state = YY_CURRENT_BUFFER;
394         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
395         return 1;
396 }
397
398 void abort_import(void)
399 {
400         int ptr;
401
402         for (ptr=0; ptr<import_stack_ptr; ptr++)
403                 unlink(import_stack[ptr].temp_name);
404 }