widl: Write-strings warnings fix.
[wine] / tools / widl / parser.l
1 /* -*-C-*-
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 %option stack
22 %option nounput noyy_top_state
23 %option 8bit never-interactive
24
25 nl      \r?\n
26 ws      [ \f\t\r]
27 cident  [a-zA-Z_][0-9a-zA-Z_]*
28 int     [0-9]+
29 hexd    [0-9a-fA-F]
30 hex     0x{hexd}+
31 uuid    {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
32
33 %x QUOTE
34 %x pp_line
35
36 %{
37
38 #include "config.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <assert.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48
49 #include "widl.h"
50 #include "utils.h"
51 #include "parser.h"
52 #include "wine/wpp.h"
53
54 #include "parser.tab.h"
55
56 extern char *temp_name;
57
58 static void addcchar(char c);
59 static char *get_buffered_cstring(void);
60
61 static char *cbuffer;
62 static int cbufidx;
63 static int cbufalloc = 0;
64
65 static int kw_token(const char *kw);
66
67 #define MAX_IMPORT_DEPTH 10
68 struct {
69   YY_BUFFER_STATE state;
70   char *input_name;
71   int   line_number;
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 = strtoul(u, NULL, 16);
84   uuid->Data2 = strtoul(u+9, NULL, 16);
85   uuid->Data3 = strtoul(u+14, NULL, 16);
86   b[2] = 0;
87   memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
88   memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
89   memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
90   memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
91   memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
92   memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
93   memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
94   memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
95   return uuid;
96 }
97
98 %}
99
100 /*
101  **************************************************************************
102  * The flexer starts here
103  **************************************************************************
104  */
105 %%
106 <INITIAL>^{ws}*\#{ws}*  yy_push_state(pp_line);
107 <pp_line>[^\n]*         {
108                             int lineno;
109                             char *cptr, *fname;
110                             yy_pop_state();
111                             lineno = (int)strtol(yytext, &cptr, 10);
112                             if(!lineno)
113                                 yyerror("Malformed '#...' line-directive; invalid linenumber");
114                             fname = strchr(cptr, '"');
115                             if(!fname)
116                                 yyerror("Malformed '#...' line-directive; missing filename");
117                             fname++;
118                             cptr = strchr(fname, '"');
119                             if(!cptr)
120                                 yyerror("Malformed '#...' line-directive; missing terminating \"");
121                             *cptr = '\0';
122                             line_number = lineno - 1;  /* We didn't read the newline */
123                             free( input_name );
124                             input_name = xstrdup(fname);
125                         }
126 \"                      yy_push_state(QUOTE); cbufidx = 0;
127 <QUOTE>\"               {
128                                 yy_pop_state();
129                                 yylval.str = get_buffered_cstring();
130                                 return aSTRING;
131                         }
132 <QUOTE>\\\\             |
133 <QUOTE>\\\"             addcchar(yytext[1]);
134 <QUOTE>\\.              addcchar('\\'); addcchar(yytext[1]);
135 <QUOTE>.                addcchar(yytext[0]);
136 {uuid}                  {
137                                 yylval.uuid = parse_uuid(yytext);
138                                 return aUUID;
139                         }
140 {hex}                   {
141                                 yylval.num = strtoul(yytext, NULL, 0);
142                                 return aHEXNUM;
143                         }
144 {int}                   {
145                                 yylval.num = strtoul(yytext, NULL, 0);
146                                 return aNUM;
147                         }
148 {cident}                return kw_token(yytext);
149 \n                      line_number++;
150 {ws}
151 \<\<                    return SHL;
152 \>\>                    return SHR;
153 .                       return yytext[0];
154 <<EOF>>                 {
155                                 if (import_stack_ptr) {
156                                         pop_import();
157                                         return aEOF;
158                                 }
159                                 else yyterminate();
160                         }
161 %%
162
163 #ifndef yywrap
164 int yywrap(void)
165 {
166         return 1;
167 }
168 #endif
169
170 static struct keyword {
171         const char *kw;
172         int token;
173         int val;
174 } keywords[] = {
175         {"__cdecl",                     tCDECL},
176         {"__int64",                     tINT64},
177         {"__stdcall",                   tSTDCALL},
178         {"_stdcall",                    tSTDCALL},
179         {"aggregatable",                tAGGREGATABLE},
180         {"allocate",                    tALLOCATE},
181         {"appobject",                   tAPPOBJECT},
182         {"async",                       tASYNC},
183         {"async_uuid",                  tASYNCUUID},
184         {"auto_handle",                 tAUTOHANDLE},
185         {"bindable",                    tBINDABLE},
186         {"boolean",                     tBOOLEAN},
187         {"broadcast",                   tBROADCAST},
188         {"byte",                        tBYTE},
189         {"byte_count",                  tBYTECOUNT},
190         {"call_as",                     tCALLAS},
191         {"callback",                    tCALLBACK},
192         {"case",                        tCASE},
193         {"char",                        tCHAR},
194         {"coclass",                     tCOCLASS},
195         {"code",                        tCODE},
196         {"comm_status",                 tCOMMSTATUS},
197         {"const",                       tCONST},
198         {"context_handle",              tCONTEXTHANDLE},
199         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
200         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
201         {"control",                     tCONTROL},
202         {"cpp_quote",                   tCPPQUOTE},
203 /* ... */
204         {"default",                     tDEFAULT},
205         {"defaultcollelem",             tDEFAULTCOLLELEM},
206         {"defaultvalue",                tDEFAULTVALUE},
207         {"defaultvtable",               tDEFAULTVTABLE},
208         {"dispinterface",               tDISPINTERFACE},
209         {"displaybind",                 tDISPLAYBIND},
210         {"dllname",                     tDLLNAME},
211         {"double",                      tDOUBLE},
212         {"dual",                        tDUAL},
213         {"endpoint",                    tENDPOINT},
214         {"entry",                       tENTRY},
215         {"enum",                        tENUM},
216         {"error_status_t",              tERRORSTATUST},
217         {"explicit_handle",             tEXPLICITHANDLE},
218         {"extern",                      tEXTERN},
219         {"float",                       tFLOAT},
220         {"handle",                      tHANDLE},
221         {"handle_t",                    tHANDLET},
222         {"helpcontext",                 tHELPCONTEXT},
223         {"helpfile",                    tHELPFILE},
224         {"helpstring",                  tHELPSTRING},
225         {"helpstringcontext",           tHELPSTRINGCONTEXT},
226         {"helpstringdll",               tHELPSTRINGDLL},
227         {"hidden",                      tHIDDEN},
228         {"hyper",                       tHYPER},
229         {"id",                          tID},
230         {"idempotent",                  tIDEMPOTENT},
231 /* ... */
232         {"iid_is",                      tIIDIS},
233         {"immediatebind",               tIMMEDIATEBIND},
234         {"implicit_handle",             tIMPLICITHANDLE},
235         {"import",                      tIMPORT},
236         {"importlib",                   tIMPORTLIB},
237         {"in",                          tIN},
238         {"in_line",                     tINLINE},
239         {"input_sync",                  tINPUTSYNC},
240         {"int",                         tINT},
241 /* ... */
242         {"interface",                   tINTERFACE},
243         {"lcid",                        tLCID},
244         {"length_is",                   tLENGTHIS},
245         {"library",                     tLIBRARY},
246 /* ... */
247         {"local",                       tLOCAL},
248         {"long",                        tLONG},
249 /* ... */
250         {"methods",                     tMETHODS},
251 /* ... */
252         {"module",                      tMODULE},
253 /* ... */
254         {"nonbrowsable",                tNONBROWSABLE},
255         {"noncreatable",                tNONCREATABLE},
256         {"nonextensible",               tNONEXTENSIBLE},
257         {"object",                      tOBJECT},
258         {"odl",                         tODL},
259         {"oleautomation",               tOLEAUTOMATION},
260 /* ... */
261         {"optional",                    tOPTIONAL},
262         {"out",                         tOUT},
263 /* ... */
264         {"pointer_default",             tPOINTERDEFAULT},
265 /* ... */
266         {"properties",                  tPROPERTIES},
267         {"propget",                     tPROPGET},
268         {"propput",                     tPROPPUT},
269         {"propputref",                  tPROPPUTREF},
270         {"ptr",                         tPTR},
271 /* ... */
272         {"public",                      tPUBLIC},
273         {"range",                       tRANGE},
274 /* ... */
275         {"readonly",                    tREADONLY},
276         {"ref",                         tREF},
277         {"requestedit",                 tREQUESTEDIT},
278         {"restricted",                  tRESTRICTED},
279         {"retval",                      tRETVAL},
280 /* ... */
281         {"short",                       tSHORT},
282         {"signed",                      tSIGNED},
283         {"single",                      tSINGLE},
284         {"size_is",                     tSIZEIS},
285         {"sizeof",                      tSIZEOF},
286         {"small",                       tSMALL},
287 /* ... */
288         {"source",                      tSOURCE},
289 /* ... */       
290         {"string",                      tSTRING},
291         {"struct",                      tSTRUCT},
292         {"switch",                      tSWITCH},
293         {"switch_is",                   tSWITCHIS},
294         {"switch_type",                 tSWITCHTYPE},
295 /* ... */
296         {"transmit_as",                 tTRANSMITAS},
297         {"typedef",                     tTYPEDEF},
298         {"union",                       tUNION},
299 /* ... */
300         {"unique",                      tUNIQUE},
301         {"unsigned",                    tUNSIGNED},
302 /* ... */
303         {"uuid",                        tUUID},
304         {"v1_enum",                     tV1ENUM},
305 /* ... */
306         {"vararg",                      tVARARG},
307         {"version",                     tVERSION},
308         {"void",                        tVOID},
309         {"wchar_t",                     tWCHAR},
310         {"wire_marshal",                tWIREMARSHAL}
311 };
312 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
313 #define KWP(p) ((const struct keyword *)(p))
314
315 static int kw_cmp_func(const void *s1, const void *s2)
316 {
317         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
318 }
319
320 #define KW_BSEARCH
321 static int kw_token(const char *kw)
322 {
323         struct keyword key, *kwp;
324         key.kw = kw;
325 #ifdef KW_BSEARCH
326         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
327 #else
328         {
329                 int i;
330                 for (kwp=NULL, i=0; i < NKEYWORDS; i++)
331                         if (!kw_cmp_func(&key, &keywords[i])) {
332                                 kwp = &keywords[i];
333                                 break;
334                         }
335         }
336 #endif
337         if (kwp) {
338                 yylval.str = (char*)kwp->kw;
339                 return kwp->token;
340         }
341         yylval.str = xstrdup(kw);
342         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
343 }
344
345 static void addcchar(char c)
346 {
347         if(cbufidx >= cbufalloc)
348         {
349                 cbufalloc += 1024;
350                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
351                 if(cbufalloc > 65536)
352                         yywarning("Reallocating string buffer larger than 64kB");
353         }
354         cbuffer[cbufidx++] = c;
355 }
356
357 static char *get_buffered_cstring(void)
358 {
359         addcchar(0);
360         return xstrdup(cbuffer);
361 }
362
363 static void pop_import(void)
364 {
365         int ptr = import_stack_ptr-1;
366
367         fclose(yyin);
368         yy_delete_buffer( YY_CURRENT_BUFFER );
369         yy_switch_to_buffer( import_stack[ptr].state );
370         if (temp_name) {
371                 unlink(temp_name);
372                 free(temp_name);
373         }
374         temp_name = import_stack[ptr].temp_name;
375         free( input_name );
376         input_name = import_stack[ptr].input_name;
377         line_number = import_stack[ptr].line_number;
378         import_stack_ptr--;
379 }
380
381 struct imports {
382         char *name;
383         struct imports *next;
384 } *first_import;
385
386 int do_import(char *fname)
387 {
388         FILE *f;
389         char *hname, *path, *p;
390         struct imports *import;
391         int ptr = import_stack_ptr;
392         int ret;
393
394         if (!parse_only && do_header) {
395                 hname = dup_basename(fname, ".idl");
396                 p = hname + strlen(hname) - 2;
397                 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
398
399                 fprintf(header, "#include <%s>\n", hname);
400                 free(hname);
401         }
402
403         import = first_import;
404         while (import && strcmp(import->name, fname))
405                 import = import->next;
406         if (import) return 0; /* already imported */
407
408         import = xmalloc(sizeof(struct imports));
409         import->name = xstrdup(fname);
410         import->next = first_import;
411         first_import = import;
412
413         if (!(path = wpp_find_include( fname, input_name )))
414             yyerror("Unable to open include file %s", fname);
415
416         import_stack[ptr].temp_name = temp_name;
417         import_stack[ptr].input_name = input_name;
418         import_stack[ptr].line_number = line_number;
419         import_stack_ptr++;
420         input_name = path;
421         line_number = 1;
422
423         ret = wpp_parse_temp( path, NULL, &temp_name );
424         if (ret) exit(1);
425
426         if((f = fopen(temp_name, "r")) == NULL)
427                 yyerror("Unable to open %s", temp_name);
428
429         import_stack[ptr].state = YY_CURRENT_BUFFER;
430         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
431         return 1;
432 }
433
434 void abort_import(void)
435 {
436         int ptr;
437
438         for (ptr=0; ptr<import_stack_ptr; ptr++)
439                 unlink(import_stack[ptr].temp_name);
440 }