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