urlmon/tests: Use BOOL type where appropriate.
[wine.git] / tools / widl / parser.l
blobfb61e21968578ac27b71837aa13fccf35106a0fe
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  */
21 %option stack
22 %option noinput nounput noyy_top_state
23 %option 8bit never-interactive prefix="parser_"
25 nl      \r?\n
26 ws      [ \f\t\r]
27 cident  [a-zA-Z_][0-9a-zA-Z_]*
28 u_suffix        (u|U)
29 l_suffix        (l|L)
30 int     [0-9]+({l_suffix}?{u_suffix}?|{u_suffix}?{l_suffix}?)?
31 hexd    [0-9a-fA-F]
32 hex     0(x|X){hexd}+({l_suffix}?{u_suffix}?|{u_suffix}?{l_suffix}?)?
33 uuid    {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
34 double  [0-9]+\.[0-9]+([eE][+-]?[0-9]+)*
36 %x QUOTE
37 %x WSTRQUOTE
38 %x ATTR
39 %x PP_LINE
40 %x SQUOTE
44 #include "config.h"
45 #include "wine/port.h"
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include <assert.h>
52 #include <errno.h>
53 #include <limits.h>
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #else
58 #define YY_NO_UNISTD_H
59 #endif
61 #include "widl.h"
62 #include "utils.h"
63 #include "parser.h"
64 #include "wine/wpp.h"
66 #include "parser.tab.h"
68 static void addcchar(char c);
69 static char *get_buffered_cstring(void);
71 static char *cbuffer;
72 static int cbufidx;
73 static int cbufalloc = 0;
75 static int kw_token(const char *kw);
76 static int attr_token(const char *kw);
78 #define MAX_IMPORT_DEPTH 10
79 struct {
80   YY_BUFFER_STATE state;
81   char *input_name;
82   int   line_number;
83   char *temp_name;
84 } import_stack[MAX_IMPORT_DEPTH];
85 int import_stack_ptr = 0;
87 /* converts an integer in string form to an unsigned long and prints an error
88  * on overflow */
89 static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
91     unsigned long val;
93     errno = 0;
94     val = strtoul(nptr, endptr, base);
95     if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
96         error_loc("integer constant %s is too large\n", nptr);
97     return val;
100 UUID *parse_uuid(const char *u)
102   UUID* uuid = xmalloc(sizeof(UUID));
103   char b[3];
104   /* it would be nice to use UuidFromStringA */
105   uuid->Data1 = strtoul(u, NULL, 16);
106   uuid->Data2 = strtoul(u+9, NULL, 16);
107   uuid->Data3 = strtoul(u+14, NULL, 16);
108   b[2] = 0;
109   memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
110   memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
111   memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
112   memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
113   memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
114   memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
115   memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
116   memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
117   return uuid;
123  **************************************************************************
124  * The flexer starts here
125  **************************************************************************
126  */
128 <INITIAL,ATTR>^{ws}*\#{ws}*     yy_push_state(PP_LINE);
129 <PP_LINE>[^\n]*         {
130                             int lineno;
131                             char *cptr, *fname;
132                             yy_pop_state();
133                             lineno = (int)strtol(yytext, &cptr, 10);
134                             if(!lineno)
135                                 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
136                             fname = strchr(cptr, '"');
137                             if(!fname)
138                                 error_loc("Malformed '#...' line-directive; missing filename\n");
139                             fname++;
140                             cptr = strchr(fname, '"');
141                             if(!cptr)
142                                 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
143                             *cptr = '\0';
144                             line_number = lineno - 1;  /* We didn't read the newline */
145                             input_name = xstrdup(fname);
146                         }
147 <INITIAL,ATTR>\"        yy_push_state(QUOTE); cbufidx = 0;
148 <QUOTE>\"               {
149                                 yy_pop_state();
150                                 parser_lval.str = get_buffered_cstring();
151                                 return aSTRING;
152                         }
153 <INITIAL,ATTR>L\"       yy_push_state(WSTRQUOTE); cbufidx = 0;
154 <WSTRQUOTE>\"           {
155                                 yy_pop_state();
156                                 parser_lval.str = get_buffered_cstring();
157                                 return aWSTRING;
158                         }
159 <INITIAL,ATTR>\'        yy_push_state(SQUOTE); cbufidx = 0;
160 <SQUOTE>\'              {
161                                 yy_pop_state();
162                                 parser_lval.str = get_buffered_cstring();
163                                 return aSQSTRING;
164                         }
165 <QUOTE,WSTRQUOTE,SQUOTE>\\\\    |
166 <QUOTE,WSTRQUOTE>\\\"   addcchar(yytext[1]);
167 <SQUOTE>\\\'    addcchar(yytext[1]);
168 <QUOTE,WSTRQUOTE,SQUOTE>\\.     addcchar('\\'); addcchar(yytext[1]);
169 <QUOTE,WSTRQUOTE,SQUOTE>.       addcchar(yytext[0]);
170 <INITIAL,ATTR>\[        yy_push_state(ATTR); return '[';
171 <ATTR>\]                yy_pop_state(); return ']';
172 <ATTR>{cident}          return attr_token(yytext);
173 <ATTR>{uuid}                    {
174                                 parser_lval.uuid = parse_uuid(yytext);
175                                 return aUUID;
176                         }
177 <INITIAL,ATTR>{hex}     {
178                                 parser_lval.num = xstrtoul(yytext, NULL, 0);
179                                 return aHEXNUM;
180                         }
181 <INITIAL,ATTR>{int}     {
182                                 parser_lval.num = xstrtoul(yytext, NULL, 0);
183                                 return aNUM;
184                         }
185 <INITIAL>{double}       {
186                                 parser_lval.dbl = strtod(yytext, NULL);
187                                 return aDOUBLE;
188                         }
189 SAFEARRAY{ws}*/\(       return tSAFEARRAY;
190 {cident}                return kw_token(yytext);
191 <INITIAL,ATTR>\n        line_number++;
192 <INITIAL,ATTR>{ws}
193 <INITIAL,ATTR>\<\<      return SHL;
194 <INITIAL,ATTR>\>\>      return SHR;
195 <INITIAL,ATTR>\-\>      return MEMBERPTR;
196 <INITIAL,ATTR>==        return EQUALITY;
197 <INITIAL,ATTR>!=        return INEQUALITY;
198 <INITIAL,ATTR>\>=       return GREATEREQUAL;
199 <INITIAL,ATTR>\<=       return LESSEQUAL;
200 <INITIAL,ATTR>\|\|      return LOGICALOR;
201 <INITIAL,ATTR>&&        return LOGICALAND;
202 <INITIAL,ATTR>\.\.\.    return ELLIPSIS;
203 <INITIAL,ATTR>.         return yytext[0];
204 <<EOF>>                 {
205                                 if (import_stack_ptr)
206                                         return aEOF;
207                                 else yyterminate();
208                         }
211 #ifndef parser_wrap
212 int parser_wrap(void)
214         return 1;
216 #endif
218 struct keyword {
219         const char *kw;
220         int token;
223 /* This table MUST be alphabetically sorted on the kw field */
224 static const struct keyword keywords[] = {
225         {"FALSE",                       tFALSE},
226         {"NULL",                        tNULL},
227         {"TRUE",                        tTRUE},
228         {"__cdecl",                     tCDECL},
229         {"__fastcall",                  tFASTCALL},
230         {"__int3264",                   tINT3264},
231         {"__int64",                     tINT64},
232         {"__pascal",                    tPASCAL},
233         {"__stdcall",                   tSTDCALL},
234         {"_cdecl",                      tCDECL},
235         {"_fastcall",                   tFASTCALL},
236         {"_pascal",                     tPASCAL},
237         {"_stdcall",                    tSTDCALL},
238         {"boolean",                     tBOOLEAN},
239         {"byte",                        tBYTE},
240         {"case",                        tCASE},
241         {"cdecl",                       tCDECL},
242         {"char",                        tCHAR},
243         {"coclass",                     tCOCLASS},
244         {"const",                       tCONST},
245         {"cpp_quote",                   tCPPQUOTE},
246         {"default",                     tDEFAULT},
247         {"dispinterface",               tDISPINTERFACE},
248         {"double",                      tDOUBLE},
249         {"enum",                        tENUM},
250         {"error_status_t",              tERRORSTATUST},
251         {"extern",                      tEXTERN},
252         {"float",                       tFLOAT},
253         {"handle_t",                    tHANDLET},
254         {"hyper",                       tHYPER},
255         {"import",                      tIMPORT},
256         {"importlib",                   tIMPORTLIB},
257         {"inline",                      tINLINE},
258         {"int",                         tINT},
259         {"interface",                   tINTERFACE},
260         {"library",                     tLIBRARY},
261         {"long",                        tLONG},
262         {"methods",                     tMETHODS},
263         {"module",                      tMODULE},
264         {"namespace",                   tNAMESPACE},
265         {"pascal",                      tPASCAL},
266         {"properties",                  tPROPERTIES},
267         {"register",                    tREGISTER},
268         {"short",                       tSHORT},
269         {"signed",                      tSIGNED},
270         {"sizeof",                      tSIZEOF},
271         {"small",                       tSMALL},
272         {"static",                      tSTATIC},
273         {"stdcall",                     tSTDCALL},
274         {"struct",                      tSTRUCT},
275         {"switch",                      tSWITCH},
276         {"typedef",                     tTYPEDEF},
277         {"union",                       tUNION},
278         {"unsigned",                    tUNSIGNED},
279         {"void",                        tVOID},
280         {"wchar_t",                     tWCHAR},
282 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
284 /* keywords only recognized in attribute lists
285  * This table MUST be alphabetically sorted on the kw field
286  */
287 static const struct keyword attr_keywords[] =
289         {"aggregatable",                tAGGREGATABLE},
290         {"allocate",                    tALLOCATE},
291         {"annotation",                  tANNOTATION},
292         {"apartment",                   tAPARTMENT},
293         {"appobject",                   tAPPOBJECT},
294         {"async",                       tASYNC},
295         {"async_uuid",                  tASYNCUUID},
296         {"auto_handle",                 tAUTOHANDLE},
297         {"bindable",                    tBINDABLE},
298         {"both",                        tBOTH},
299         {"broadcast",                   tBROADCAST},
300         {"byte_count",                  tBYTECOUNT},
301         {"call_as",                     tCALLAS},
302         {"callback",                    tCALLBACK},
303         {"code",                        tCODE},
304         {"comm_status",                 tCOMMSTATUS},
305         {"context_handle",              tCONTEXTHANDLE},
306         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
307         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
308         {"control",                     tCONTROL},
309         {"decode",                      tDECODE},
310         {"defaultbind",                 tDEFAULTBIND},
311         {"defaultcollelem",             tDEFAULTCOLLELEM},
312         {"defaultvalue",                tDEFAULTVALUE},
313         {"defaultvtable",               tDEFAULTVTABLE},
314         {"disable_consistency_check",   tDISABLECONSISTENCYCHECK},
315         {"displaybind",                 tDISPLAYBIND},
316         {"dllname",                     tDLLNAME},
317         {"dual",                        tDUAL},
318         {"enable_allocate",             tENABLEALLOCATE},
319         {"encode",                      tENCODE},
320         {"endpoint",                    tENDPOINT},
321         {"entry",                       tENTRY},
322         {"explicit_handle",             tEXPLICITHANDLE},
323         {"fault_status",                tFAULTSTATUS},
324         {"force_allocate",              tFORCEALLOCATE},
325         {"free",                        tFREE},
326         {"handle",                      tHANDLE},
327         {"helpcontext",                 tHELPCONTEXT},
328         {"helpfile",                    tHELPFILE},
329         {"helpstring",                  tHELPSTRING},
330         {"helpstringcontext",           tHELPSTRINGCONTEXT},
331         {"helpstringdll",               tHELPSTRINGDLL},
332         {"hidden",                      tHIDDEN},
333         {"id",                          tID},
334         {"idempotent",                  tIDEMPOTENT},
335         {"ignore",                      tIGNORE},
336         {"iid_is",                      tIIDIS},
337         {"immediatebind",               tIMMEDIATEBIND},
338         {"implicit_handle",             tIMPLICITHANDLE},
339         {"in",                          tIN},
340         {"in_line",                     tIN_LINE},
341         {"input_sync",                  tINPUTSYNC},
342         {"lcid",                        tLCID},
343         {"length_is",                   tLENGTHIS},
344         {"licensed",                    tLICENSED},
345         {"local",                       tLOCAL},
346         {"maybe",                       tMAYBE},
347         {"message",                     tMESSAGE},
348         {"neutral",                     tNEUTRAL},
349         {"nocode",                      tNOCODE},
350         {"nonbrowsable",                tNONBROWSABLE},
351         {"noncreatable",                tNONCREATABLE},
352         {"nonextensible",               tNONEXTENSIBLE},
353         {"notify",                      tNOTIFY},
354         {"notify_flag",                 tNOTIFYFLAG},
355         {"object",                      tOBJECT},
356         {"odl",                         tODL},
357         {"oleautomation",               tOLEAUTOMATION},
358         {"optimize",                    tOPTIMIZE},
359         {"optional",                    tOPTIONAL},
360         {"out",                         tOUT},
361         {"partial_ignore",              tPARTIALIGNORE},
362         {"pointer_default",             tPOINTERDEFAULT},
363         {"progid",                      tPROGID},
364         {"propget",                     tPROPGET},
365         {"propput",                     tPROPPUT},
366         {"propputref",                  tPROPPUTREF},
367         {"proxy",                       tPROXY},
368         {"ptr",                         tPTR},
369         {"public",                      tPUBLIC},
370         {"range",                       tRANGE},
371         {"readonly",                    tREADONLY},
372         {"ref",                         tREF},
373         {"represent_as",                tREPRESENTAS},
374         {"requestedit",                 tREQUESTEDIT},
375         {"restricted",                  tRESTRICTED},
376         {"retval",                      tRETVAL},
377         {"single",                      tSINGLE},
378         {"size_is",                     tSIZEIS},
379         {"source",                      tSOURCE},
380         {"strict_context_handle",       tSTRICTCONTEXTHANDLE},
381         {"string",                      tSTRING},
382         {"switch_is",                   tSWITCHIS},
383         {"switch_type",                 tSWITCHTYPE},
384         {"threading",                   tTHREADING},
385         {"transmit_as",                 tTRANSMITAS},
386         {"uidefault",                   tUIDEFAULT},
387         {"unique",                      tUNIQUE},
388         {"user_marshal",                tUSERMARSHAL},
389         {"usesgetlasterror",            tUSESGETLASTERROR},
390         {"uuid",                        tUUID},
391         {"v1_enum",                     tV1ENUM},
392         {"vararg",                      tVARARG},
393         {"version",                     tVERSION},
394         {"vi_progid",                   tVIPROGID},
395         {"wire_marshal",                tWIREMARSHAL},
398 /* attributes TODO:
399     custom
400     first_is
401     last_is
402     max_is
403     min_is
406 #define KWP(p) ((const struct keyword *)(p))
408 static int kw_cmp_func(const void *s1, const void *s2)
410         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
413 static int kw_token(const char *kw)
415         struct keyword key, *kwp;
416         key.kw = kw;
417         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
418         if (kwp && (do_rt_extension || kwp->token != tNAMESPACE)) {
419                 parser_lval.str = xstrdup(kwp->kw);
420                 return kwp->token;
421         }
422         parser_lval.str = xstrdup(kw);
423         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
426 static int attr_token(const char *kw)
428         struct keyword key, *kwp;
429         key.kw = kw;
430         kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
431                       sizeof(attr_keywords[0]), kw_cmp_func);
432         if (kwp) {
433             parser_lval.str = xstrdup(kwp->kw);
434             return kwp->token;
435         }
436         return kw_token(kw);
439 static void addcchar(char c)
441         if(cbufidx >= cbufalloc)
442         {
443                 cbufalloc += 1024;
444                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
445                 if(cbufalloc > 65536)
446                         parser_warning("Reallocating string buffer larger than 64kB\n");
447         }
448         cbuffer[cbufidx++] = c;
451 static char *get_buffered_cstring(void)
453         addcchar(0);
454         return xstrdup(cbuffer);
457 void pop_import(void)
459         int ptr = import_stack_ptr-1;
461         fclose(yyin);
462         yy_delete_buffer( YY_CURRENT_BUFFER );
463         yy_switch_to_buffer( import_stack[ptr].state );
464         if (temp_name) {
465                 unlink(temp_name);
466                 free(temp_name);
467         }
468         temp_name = import_stack[ptr].temp_name;
469         input_name = import_stack[ptr].input_name;
470         line_number = import_stack[ptr].line_number;
471         import_stack_ptr--;
474 struct imports {
475         char *name;
476         struct imports *next;
477 } *first_import;
479 int do_import(char *fname)
481         FILE *f;
482         char *path, *name;
483         struct imports *import;
484         int ptr = import_stack_ptr;
485         int ret, fd;
487         import = first_import;
488         while (import && strcmp(import->name, fname))
489                 import = import->next;
490         if (import) return 0; /* already imported */
492         import = xmalloc(sizeof(struct imports));
493         import->name = xstrdup(fname);
494         import->next = first_import;
495         first_import = import;
497         /* don't search for a file name with a path in the include directories,
498          * for compatibility with MIDL */
499         if (strchr( fname, '/' ) || strchr( fname, '\\' ))
500             path = xstrdup( fname );
501         else if (!(path = wpp_find_include( fname, input_name )))
502             error_loc("Unable to open include file %s\n", fname);
504         import_stack[ptr].temp_name = temp_name;
505         import_stack[ptr].input_name = input_name;
506         import_stack[ptr].line_number = line_number;
507         import_stack_ptr++;
508         input_name = path;
509         line_number = 1;
511         name = xstrdup( "widl.XXXXXX" );
512         if((fd = mkstemps( name, 0 )) == -1)
513             error("Could not generate a temp name from %s\n", name);
515         temp_name = name;
516         if (!(f = fdopen(fd, "wt")))
517             error("Could not open fd %s for writing\n", name);
519         ret = wpp_parse( path, f );
520         fclose( f );
521         if (ret) exit(1);
523         if((f = fopen(temp_name, "r")) == NULL)
524                 error_loc("Unable to open %s\n", temp_name);
526         import_stack[ptr].state = YY_CURRENT_BUFFER;
527         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
528         return 1;
531 void abort_import(void)
533         int ptr;
535         for (ptr=0; ptr<import_stack_ptr; ptr++)
536                 unlink(import_stack[ptr].temp_name);