Release 1.3.7.
[wine/gsoc-2012-control.git] / tools / widl / parser.l
blob2938fb688587f271c48f3269d557ff519c0da4a7
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                             free( input_name );
146                             input_name = xstrdup(fname);
147                         }
148 <INITIAL,ATTR>\"        yy_push_state(QUOTE); cbufidx = 0;
149 <QUOTE>\"               {
150                                 yy_pop_state();
151                                 parser_lval.str = get_buffered_cstring();
152                                 return aSTRING;
153                         }
154 <INITIAL,ATTR>L\"       yy_push_state(WSTRQUOTE); cbufidx = 0;
155 <WSTRQUOTE>\"           {
156                                 yy_pop_state();
157                                 parser_lval.str = get_buffered_cstring();
158                                 return aWSTRING;
159                         }
160 <INITIAL,ATTR>\'        yy_push_state(SQUOTE); cbufidx = 0;
161 <SQUOTE>\'              {
162                                 yy_pop_state();
163                                 parser_lval.str = get_buffered_cstring();
164                                 return aSQSTRING;
165                         }
166 <QUOTE,WSTRQUOTE,SQUOTE>\\\\    |
167 <QUOTE,WSTRQUOTE>\\\"   addcchar(yytext[1]);
168 <SQUOTE>\\\'    addcchar(yytext[1]);
169 <QUOTE,WSTRQUOTE,SQUOTE>\\.     addcchar('\\'); addcchar(yytext[1]);
170 <QUOTE,WSTRQUOTE,SQUOTE>.       addcchar(yytext[0]);
171 <INITIAL,ATTR>\[        yy_push_state(ATTR); return '[';
172 <ATTR>\]                yy_pop_state(); return ']';
173 <ATTR>{cident}          return attr_token(yytext);
174 <ATTR>{uuid}                    {
175                                 parser_lval.uuid = parse_uuid(yytext);
176                                 return aUUID;
177                         }
178 <INITIAL,ATTR>{hex}     {
179                                 parser_lval.num = xstrtoul(yytext, NULL, 0);
180                                 return aHEXNUM;
181                         }
182 <INITIAL,ATTR>{int}     {
183                                 parser_lval.num = xstrtoul(yytext, NULL, 0);
184                                 return aNUM;
185                         }
186 <INITIAL>{double}       {
187                                 parser_lval.dbl = strtod(yytext, NULL);
188                                 return aDOUBLE;
189                         }
190 SAFEARRAY{ws}*/\(       return tSAFEARRAY;
191 {cident}                return kw_token(yytext);
192 <INITIAL,ATTR>\n        line_number++;
193 <INITIAL,ATTR>{ws}
194 <INITIAL,ATTR>\<\<      return SHL;
195 <INITIAL,ATTR>\>\>      return SHR;
196 <INITIAL,ATTR>\-\>      return MEMBERPTR;
197 <INITIAL,ATTR>==        return EQUALITY;
198 <INITIAL,ATTR>!=        return INEQUALITY;
199 <INITIAL,ATTR>\>=       return GREATEREQUAL;
200 <INITIAL,ATTR>\<=       return LESSEQUAL;
201 <INITIAL,ATTR>\|\|      return LOGICALOR;
202 <INITIAL,ATTR>&&        return LOGICALAND;
203 <INITIAL,ATTR>\.\.\.    return ELLIPSIS;
204 <INITIAL,ATTR>.         return yytext[0];
205 <<EOF>>                 {
206                                 if (import_stack_ptr)
207                                         return aEOF;
208                                 else yyterminate();
209                         }
212 #ifndef parser_wrap
213 int parser_wrap(void)
215         return 1;
217 #endif
219 struct keyword {
220         const char *kw;
221         int token;
224 /* This table MUST be alphabetically sorted on the kw field */
225 static const struct keyword keywords[] = {
226         {"FALSE",                       tFALSE},
227         {"NULL",                        tNULL},
228         {"TRUE",                        tTRUE},
229         {"__cdecl",                     tCDECL},
230         {"__fastcall",                  tFASTCALL},
231         {"__int3264",                   tINT3264},
232         {"__int64",                     tINT64},
233         {"__pascal",                    tPASCAL},
234         {"__stdcall",                   tSTDCALL},
235         {"_cdecl",                      tCDECL},
236         {"_fastcall",                   tFASTCALL},
237         {"_pascal",                     tPASCAL},
238         {"_stdcall",                    tSTDCALL},
239         {"boolean",                     tBOOLEAN},
240         {"byte",                        tBYTE},
241         {"case",                        tCASE},
242         {"cdecl",                       tCDECL},
243         {"char",                        tCHAR},
244         {"coclass",                     tCOCLASS},
245         {"const",                       tCONST},
246         {"cpp_quote",                   tCPPQUOTE},
247         {"default",                     tDEFAULT},
248         {"dispinterface",               tDISPINTERFACE},
249         {"double",                      tDOUBLE},
250         {"enum",                        tENUM},
251         {"error_status_t",              tERRORSTATUST},
252         {"extern",                      tEXTERN},
253         {"float",                       tFLOAT},
254         {"handle_t",                    tHANDLET},
255         {"hyper",                       tHYPER},
256         {"import",                      tIMPORT},
257         {"importlib",                   tIMPORTLIB},
258         {"inline",                      tINLINE},
259         {"int",                         tINT},
260         {"interface",                   tINTERFACE},
261         {"library",                     tLIBRARY},
262         {"long",                        tLONG},
263         {"methods",                     tMETHODS},
264         {"module",                      tMODULE},
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         {"appobject",                   tAPPOBJECT},
293         {"async",                       tASYNC},
294         {"async_uuid",                  tASYNCUUID},
295         {"auto_handle",                 tAUTOHANDLE},
296         {"bindable",                    tBINDABLE},
297         {"broadcast",                   tBROADCAST},
298         {"byte_count",                  tBYTECOUNT},
299         {"call_as",                     tCALLAS},
300         {"callback",                    tCALLBACK},
301         {"code",                        tCODE},
302         {"comm_status",                 tCOMMSTATUS},
303         {"context_handle",              tCONTEXTHANDLE},
304         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
305         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
306         {"control",                     tCONTROL},
307         {"decode",                      tDECODE},
308         {"defaultbind",                 tDEFAULTBIND},
309         {"defaultcollelem",             tDEFAULTCOLLELEM},
310         {"defaultvalue",                tDEFAULTVALUE},
311         {"defaultvtable",               tDEFAULTVTABLE},
312         {"disable_consistency_check",   tDISABLECONSISTENCYCHECK},
313         {"displaybind",                 tDISPLAYBIND},
314         {"dllname",                     tDLLNAME},
315         {"dual",                        tDUAL},
316         {"enable_allocate",             tENABLEALLOCATE},
317         {"encode",                      tENCODE},
318         {"endpoint",                    tENDPOINT},
319         {"entry",                       tENTRY},
320         {"explicit_handle",             tEXPLICITHANDLE},
321         {"fault_status",                tFAULTSTATUS},
322         {"force_allocate",              tFORCEALLOCATE},
323         {"handle",                      tHANDLE},
324         {"helpcontext",                 tHELPCONTEXT},
325         {"helpfile",                    tHELPFILE},
326         {"helpstring",                  tHELPSTRING},
327         {"helpstringcontext",           tHELPSTRINGCONTEXT},
328         {"helpstringdll",               tHELPSTRINGDLL},
329         {"hidden",                      tHIDDEN},
330         {"id",                          tID},
331         {"idempotent",                  tIDEMPOTENT},
332         {"ignore",                      tIGNORE},
333         {"iid_is",                      tIIDIS},
334         {"immediatebind",               tIMMEDIATEBIND},
335         {"implicit_handle",             tIMPLICITHANDLE},
336         {"in",                          tIN},
337         {"in_line",                     tIN_LINE},
338         {"input_sync",                  tINPUTSYNC},
339         {"lcid",                        tLCID},
340         {"length_is",                   tLENGTHIS},
341         {"licensed",                    tLICENSED},
342         {"local",                       tLOCAL},
343         {"maybe",                       tMAYBE},
344         {"message",                     tMESSAGE},
345         {"nocode",                      tNOCODE},
346         {"nonbrowsable",                tNONBROWSABLE},
347         {"noncreatable",                tNONCREATABLE},
348         {"nonextensible",               tNONEXTENSIBLE},
349         {"notify",                      tNOTIFY},
350         {"notify_flag",                 tNOTIFYFLAG},
351         {"object",                      tOBJECT},
352         {"odl",                         tODL},
353         {"oleautomation",               tOLEAUTOMATION},
354         {"optimize",                    tOPTIMIZE},
355         {"optional",                    tOPTIONAL},
356         {"out",                         tOUT},
357         {"partial_ignore",              tPARTIALIGNORE},
358         {"pointer_default",             tPOINTERDEFAULT},
359         {"propget",                     tPROPGET},
360         {"propput",                     tPROPPUT},
361         {"propputref",                  tPROPPUTREF},
362         {"proxy",                       tPROXY},
363         {"ptr",                         tPTR},
364         {"public",                      tPUBLIC},
365         {"range",                       tRANGE},
366         {"readonly",                    tREADONLY},
367         {"ref",                         tREF},
368         {"represent_as",                tREPRESENTAS},
369         {"requestedit",                 tREQUESTEDIT},
370         {"restricted",                  tRESTRICTED},
371         {"retval",                      tRETVAL},
372         {"size_is",                     tSIZEIS},
373         {"source",                      tSOURCE},
374         {"strict_context_handle",       tSTRICTCONTEXTHANDLE},
375         {"string",                      tSTRING},
376         {"switch_is",                   tSWITCHIS},
377         {"switch_type",                 tSWITCHTYPE},
378         {"transmit_as",                 tTRANSMITAS},
379         {"uidefault",                   tUIDEFAULT},
380         {"unique",                      tUNIQUE},
381         {"user_marshal",                tUSERMARSHAL},
382         {"usesgetlasterror",            tUSESGETLASTERROR},
383         {"uuid",                        tUUID},
384         {"v1_enum",                     tV1ENUM},
385         {"vararg",                      tVARARG},
386         {"version",                     tVERSION},
387         {"wire_marshal",                tWIREMARSHAL},
390 /* attributes TODO:
391     custom
392     first_is
393     last_is
394     max_is
395     min_is
398 #define KWP(p) ((const struct keyword *)(p))
400 static int kw_cmp_func(const void *s1, const void *s2)
402         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
405 static int kw_token(const char *kw)
407         struct keyword key, *kwp;
408         key.kw = kw;
409         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
410         if (kwp) {
411                 parser_lval.str = xstrdup(kwp->kw);
412                 return kwp->token;
413         }
414         parser_lval.str = xstrdup(kw);
415         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
418 static int attr_token(const char *kw)
420         struct keyword key, *kwp;
421         key.kw = kw;
422         kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
423                       sizeof(attr_keywords[0]), kw_cmp_func);
424         if (kwp) {
425             parser_lval.str = xstrdup(kwp->kw);
426             return kwp->token;
427         }
428         return kw_token(kw);
431 static void addcchar(char c)
433         if(cbufidx >= cbufalloc)
434         {
435                 cbufalloc += 1024;
436                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
437                 if(cbufalloc > 65536)
438                         parser_warning("Reallocating string buffer larger than 64kB\n");
439         }
440         cbuffer[cbufidx++] = c;
443 static char *get_buffered_cstring(void)
445         addcchar(0);
446         return xstrdup(cbuffer);
449 void pop_import(void)
451         int ptr = import_stack_ptr-1;
453         fclose(yyin);
454         yy_delete_buffer( YY_CURRENT_BUFFER );
455         yy_switch_to_buffer( import_stack[ptr].state );
456         if (temp_name) {
457                 unlink(temp_name);
458                 free(temp_name);
459         }
460         temp_name = import_stack[ptr].temp_name;
461         input_name = import_stack[ptr].input_name;
462         line_number = import_stack[ptr].line_number;
463         import_stack_ptr--;
466 struct imports {
467         char *name;
468         struct imports *next;
469 } *first_import;
471 int do_import(char *fname)
473         FILE *f;
474         char *path, *name;
475         struct imports *import;
476         int ptr = import_stack_ptr;
477         int ret, fd;
479         import = first_import;
480         while (import && strcmp(import->name, fname))
481                 import = import->next;
482         if (import) return 0; /* already imported */
484         import = xmalloc(sizeof(struct imports));
485         import->name = xstrdup(fname);
486         import->next = first_import;
487         first_import = import;
489         /* don't search for a file name with a path in the include directories,
490          * for compatibility with MIDL */
491         if (strchr( fname, '/' ) || strchr( fname, '\\' ))
492             path = xstrdup( fname );
493         else if (!(path = wpp_find_include( fname, input_name )))
494             error_loc("Unable to open include file %s\n", fname);
496         import_stack[ptr].temp_name = temp_name;
497         import_stack[ptr].input_name = input_name;
498         import_stack[ptr].line_number = line_number;
499         import_stack_ptr++;
500         input_name = path;
501         line_number = 1;
503         name = xstrdup( "widl.XXXXXX" );
504         if((fd = mkstemps( name, 0 )) == -1)
505             error("Could not generate a temp name from %s\n", name);
507         temp_name = name;
508         if (!(f = fdopen(fd, "wt")))
509             error("Could not open fd %s for writing\n", name);
511         ret = wpp_parse( path, f );
512         fclose( f );
513         if (ret) exit(1);
515         if((f = fopen(temp_name, "r")) == NULL)
516                 error_loc("Unable to open %s\n", temp_name);
518         import_stack[ptr].state = YY_CURRENT_BUFFER;
519         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
520         return 1;
523 void abort_import(void)
525         int ptr;
527         for (ptr=0; ptr<import_stack_ptr; ptr++)
528                 unlink(import_stack[ptr].temp_name);