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