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