- Handle "? :" conditionals.
[wine.git] / tools / widl / parser.l
blob5639f91ef1d36b0f7b022dc02c5f57efdb539379
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  */
21 %option stack
22 %option never-interactive
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}
32 %x QUOTE
33 %x pp_line
37 #include "config.h"
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
48 #include "widl.h"
49 #include "utils.h"
50 #include "parser.h"
51 #include "wine/wpp.h"
53 #include "y.tab.h"
55 #define YY_USE_PROTOS
56 #define YY_NO_UNPUT
57 #define YY_NO_TOP_STATE
59 extern char *temp_name;
61 static void addcchar(char c);
62 static char *get_buffered_cstring(void);
64 static char *cbuffer;
65 static int cbufidx;
66 static int cbufalloc = 0;
68 static int kw_token(const char *kw);
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;
79 static void pop_import(void);
81 static UUID* parse_uuid(const char*u)
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;
104  **************************************************************************
105  * The flexer starts here
106  **************************************************************************
107  */
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                         }
166 #ifndef yywrap
167 int yywrap(void)
169         return 1;
171 #endif
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         {"dispinterface",               tDISPINTERFACE},
211 /* ... */
212         {"dllname",                     tDLLNAME},
213         {"double",                      tDOUBLE},
214         {"dual",                        tDUAL},
215 /* ... */
216         {"entry",                       tENTRY},
217         {"enum",                        tENUM},
218         {"error_status_t",              tERRORSTATUST},
219 /* ... */
220         {"extern",                      tEXTERN},
221 /* ... */
222         {"float",                       tFLOAT},
223 /* ... */
224         {"handle",                      tHANDLE},
225         {"handle_t",                    tHANDLET},
226 /* ... */
227         {"helpstring",                  tHELPSTRING},
228 /* ... */
229         {"hyper",                       tHYPER},
230         {"id",                          tID},
231         {"idempotent",                  tIDEMPOTENT},
232 /* ... */
233         {"iid_is",                      tIIDIS},
234 /* ... */
235         {"import",                      tIMPORT},
236         {"importlib",                   tIMPORTLIB},
237         {"in",                          tIN},
238         {"include",                     tINCLUDE},
239         {"in_line",                     tINLINE},
240         {"input_sync",                  tINPUTSYNC},
241         {"int",                         tINT},
242 /* ... */
243         {"interface",                   tINTERFACE},
244 /* ... */
245         {"length_is",                   tLENGTHIS},
246         {"library",                     tLIBRARY},
247 /* ... */
248         {"local",                       tLOCAL},
249         {"long",                        tLONG},
250 /* ... */
251         {"methods",                     tMETHODS},
252 /* ... */
253         {"module",                      tMODULE},
254 /* ... */
255         {"object",                      tOBJECT},
256         {"odl",                         tODL},
257         {"oleautomation",               tOLEAUTOMATION},
258 /* ... */
259         {"out",                         tOUT},
260 /* ... */
261         {"pointer_default",             tPOINTERDEFAULT},
262 /* ... */
263         {"properties",                  tPROPERTIES},
264 /* ... */
265         {"public",                      tPUBLIC},
266 /* ... */
267         {"readonly",                    tREADONLY},
268         {"ref",                         tREF},
269 /* ... */
270         {"retval",                      tRETVAL},
271 /* ... */
272         {"short",                       tSHORT},
273         {"signed",                      tSIGNED},
274         {"size_is",                     tSIZEIS},
275         {"sizeof",                      tSIZEOF},
276 /* ... */
277         {"source",                      tSOURCE},
278 /* ... */       
279         {"string",                      tSTRING},
280         {"struct",                      tSTRUCT},
281         {"switch",                      tSWITCH},
282         {"switch_is",                   tSWITCHIS},
283         {"switch_type",                 tSWITCHTYPE},
284 /* ... */
285         {"typedef",                     tTYPEDEF},
286         {"union",                       tUNION},
287 /* ... */
288         {"unique",                      tUNIQUE},
289         {"unsigned",                    tUNSIGNED},
290 /* ... */
291         {"uuid",                        tUUID},
292         {"v1_enum",                     tV1ENUM},
293 /* ... */
294         {"version",                     tVERSION},
295         {"void",                        tVOID},
296         {"wchar_t",                     tWCHAR},
297         {"wire_marshal",                tWIREMARSHAL}
299 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
300 #define KWP(p) ((struct keyword *)(p))
302 static int kw_cmp_func(const void *s1, const void *s2)
304         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
307 #define KW_BSEARCH
308 static int kw_token(const char *kw)
310         struct keyword key, *kwp;
311         key.kw = kw;
312 #ifdef KW_BSEARCH
313         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
314 #else
315         {
316                 int i;
317                 for (kwp=NULL, i=0; i < NKEYWORDS; i++)
318                         if (!kw_cmp_func(&key, &keywords[i])) {
319                                 kwp = &keywords[i];
320                                 break;
321                         }
322         }
323 #endif
324         if (kwp) {
325                 yylval.str = (char*)kwp->kw;
326                 return kwp->token;
327         }
328         yylval.str = xstrdup(kw);
329         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
332 static void addcchar(char c)
334         if(cbufidx >= cbufalloc)
335         {
336                 cbufalloc += 1024;
337                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
338                 if(cbufalloc > 65536)
339                         yywarning("Reallocating string buffer larger than 64kB");
340         }
341         cbuffer[cbufidx++] = c;
344 static char *get_buffered_cstring(void)
346         addcchar(0);
347         return xstrdup(cbuffer);
350 static void pop_import(void)
352         int ptr = import_stack_ptr-1;
354         fclose(yyin);
355         yy_delete_buffer( YY_CURRENT_BUFFER );
356         yy_switch_to_buffer( import_stack[ptr].state );
357         if (temp_name) {
358                 unlink(temp_name);
359                 free(temp_name);
360         }
361         temp_name = import_stack[ptr].temp_name;
362         free( input_name );
363         input_name = import_stack[ptr].input_name;
364         line_number = import_stack[ptr].line_number;
365         import_stack_ptr--;
368 struct imports {
369         char *name;
370         struct imports *next;
371 } *first_import;
373 int do_import(char *fname)
375         FILE *f;
376         char *hname, *path, *p;
377         struct imports *import;
378         int ptr = import_stack_ptr;
379         int ret;
381         if (!parse_only) {
382                 hname = dup_basename(fname, ".idl");
383                 p = hname + strlen(hname) - 2;
384                 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
386                 fprintf(header, "#include <%s>\n", hname);
387                 free(hname);
388         }
390         import = first_import;
391         while (import && strcmp(import->name, fname))
392                 import = import->next;
393         if (import) return 0; /* already imported */
395         import = xmalloc(sizeof(struct imports));
396         import->name = xstrdup(fname);
397         import->next = first_import;
398         first_import = import;
400         if (!(path = wpp_find_include( fname, 1 )))
401             yyerror("Unable to open include file %s", fname);
403         import_stack[ptr].temp_name = temp_name;
404         import_stack[ptr].input_name = input_name;
405         import_stack[ptr].line_number = line_number;
406         import_stack_ptr++;
407         input_name = path;
408         line_number = 1;
410         ret = wpp_parse_temp( path, NULL, &temp_name );
411         if (ret) exit(1);
413         if((f = fopen(temp_name, "r")) == NULL)
414                 yyerror("Unable to open %s", temp_name);
416         import_stack[ptr].state = YY_CURRENT_BUFFER;
417         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
418         return 1;
421 void abort_import(void)
423         int ptr;
425         for (ptr=0; ptr<import_stack_ptr; ptr++)
426                 unlink(import_stack[ptr].temp_name);