d3dx8: Fix number of parameter of functions D3DXVec4Cross and D3DXVec?CatmullRom.
[wine/multimedia.git] / tools / widl / parser.l
blobd2fd3c1248cbc4d9e44e2e7a59af5749e2e5789d
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 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 int     [0-9]+
29 hexd    [0-9a-fA-F]
30 hex     0x{hexd}+
31 uuid    {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
32 double  [0-9]+\.[0-9]+([eE][+-]?[0-9]+)*
34 %x QUOTE
35 %x ATTR
36 %x PP_LINE
40 #include "config.h"
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include <assert.h>
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
51 #include "widl.h"
52 #include "utils.h"
53 #include "parser.h"
54 #include "wine/wpp.h"
56 #include "parser.tab.h"
58 extern char *temp_name;
60 static void addcchar(char c);
61 static char *get_buffered_cstring(void);
63 static char *cbuffer;
64 static int cbufidx;
65 static int cbufalloc = 0;
67 static int kw_token(const char *kw);
68 static int attr_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 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,ATTR>^{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                                 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
117                             fname = strchr(cptr, '"');
118                             if(!fname)
119                                 error_loc("Malformed '#...' line-directive; missing filename\n");
120                             fname++;
121                             cptr = strchr(fname, '"');
122                             if(!cptr)
123                                 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
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 <INITIAL,ATTR>\"        yy_push_state(QUOTE); cbufidx = 0;
130 <QUOTE>\"               {
131                                 yy_pop_state();
132                                 parser_lval.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 <INITIAL,ATTR>\[        yy_push_state(ATTR); return '[';
140 <ATTR>\]                yy_pop_state(); return ']';
141 <ATTR>{cident}          return attr_token(yytext);
142 <ATTR>{uuid}                    {
143                                 parser_lval.uuid = parse_uuid(yytext);
144                                 return aUUID;
145                         }
146 <INITIAL,ATTR>{hex}     {
147                                 parser_lval.num = strtoul(yytext, NULL, 0);
148                                 return aHEXNUM;
149                         }
150 <INITIAL,ATTR>{int}     {
151                                 parser_lval.num = strtoul(yytext, NULL, 0);
152                                 return aNUM;
153                         }
154 <INITIAL>{double}       {
155                                 parser_lval.dbl = strtod(yytext, NULL);
156                                 return aDOUBLE;
157                         }
158 SAFEARRAY{ws}*/\(       return tSAFEARRAY;
159 {cident}                return kw_token(yytext);
160 <INITIAL,ATTR>\n        line_number++;
161 <INITIAL,ATTR>{ws}
162 <INITIAL,ATTR>\<\<      return SHL;
163 <INITIAL,ATTR>\>\>      return SHR;
164 <INITIAL,ATTR>.         return yytext[0];
165 <<EOF>>                 {
166                                 if (import_stack_ptr) {
167                                         pop_import();
168                                         return aEOF;
169                                 }
170                                 else yyterminate();
171                         }
174 #ifndef parser_wrap
175 int parser_wrap(void)
177         return 1;
179 #endif
181 struct keyword {
182         const char *kw;
183         int token;
186 static const struct keyword keywords[] = {
187         {"FALSE",                       tFALSE},
188         {"TRUE",                        tTRUE},
189         {"__cdecl",                     tCDECL},
190         {"__int64",                     tINT64},
191         {"__stdcall",                   tSTDCALL},
192         {"_stdcall",                    tSTDCALL},
193         {"boolean",                     tBOOLEAN},
194         {"byte",                        tBYTE},
195         {"callback",                    tCALLBACK},
196         {"case",                        tCASE},
197         {"char",                        tCHAR},
198         {"coclass",                     tCOCLASS},
199         {"code",                        tCODE},
200         {"comm_status",                 tCOMMSTATUS},
201         {"const",                       tCONST},
202         {"cpp_quote",                   tCPPQUOTE},
203         {"default",                     tDEFAULT},
204         {"dispinterface",               tDISPINTERFACE},
205         {"double",                      tDOUBLE},
206         {"enum",                        tENUM},
207         {"error_status_t",              tERRORSTATUST},
208         {"extern",                      tEXTERN},
209         {"float",                       tFLOAT},
210         {"handle_t",                    tHANDLET},
211         {"hyper",                       tHYPER},
212         {"import",                      tIMPORT},
213         {"importlib",                   tIMPORTLIB},
214         {"in_line",                     tINLINE},
215         {"int",                         tINT},
216         {"interface",                   tINTERFACE},
217         {"library",                     tLIBRARY},
218         {"long",                        tLONG},
219         {"methods",                     tMETHODS},
220         {"module",                      tMODULE},
221         {"properties",                  tPROPERTIES},
222         {"short",                       tSHORT},
223         {"signed",                      tSIGNED},
224         {"sizeof",                      tSIZEOF},
225         {"small",                       tSMALL},
226         {"struct",                      tSTRUCT},
227         {"switch",                      tSWITCH},
228         {"typedef",                     tTYPEDEF},
229         {"union",                       tUNION},
230         {"unsigned",                    tUNSIGNED},
231         {"void",                        tVOID},
232         {"wchar_t",                     tWCHAR},
234 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
236 /* keywords only recognized in attribute lists */
237 static const struct keyword attr_keywords[] =
239         {"aggregatable",                tAGGREGATABLE},
240         {"allocate",                    tALLOCATE},
241         {"appobject",                   tAPPOBJECT},
242         {"async",                       tASYNC},
243         {"async_uuid",                  tASYNCUUID},
244         {"auto_handle",                 tAUTOHANDLE},
245         {"bindable",                    tBINDABLE},
246         {"broadcast",                   tBROADCAST},
247         {"byte_count",                  tBYTECOUNT},
248         {"call_as",                     tCALLAS},
249         {"context_handle",              tCONTEXTHANDLE},
250         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
251         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
252         {"control",                     tCONTROL},
253         {"defaultcollelem",             tDEFAULTCOLLELEM},
254         {"defaultvalue",                tDEFAULTVALUE},
255         {"defaultvtable",               tDEFAULTVTABLE},
256         {"displaybind",                 tDISPLAYBIND},
257         {"dllname",                     tDLLNAME},
258         {"dual",                        tDUAL},
259         {"endpoint",                    tENDPOINT},
260         {"entry",                       tENTRY},
261         {"explicit_handle",             tEXPLICITHANDLE},
262         {"handle",                      tHANDLE},
263         {"helpcontext",                 tHELPCONTEXT},
264         {"helpfile",                    tHELPFILE},
265         {"helpstring",                  tHELPSTRING},
266         {"helpstringcontext",           tHELPSTRINGCONTEXT},
267         {"helpstringdll",               tHELPSTRINGDLL},
268         {"hidden",                      tHIDDEN},
269         {"id",                          tID},
270         {"idempotent",                  tIDEMPOTENT},
271         {"iid_is",                      tIIDIS},
272         {"immediatebind",               tIMMEDIATEBIND},
273         {"implicit_handle",             tIMPLICITHANDLE},
274         {"in",                          tIN},
275         {"input_sync",                  tINPUTSYNC},
276         {"lcid",                        tLCID},
277         {"length_is",                   tLENGTHIS},
278         {"local",                       tLOCAL},
279         {"nonbrowsable",                tNONBROWSABLE},
280         {"noncreatable",                tNONCREATABLE},
281         {"nonextensible",               tNONEXTENSIBLE},
282         {"object",                      tOBJECT},
283         {"odl",                         tODL},
284         {"oleautomation",               tOLEAUTOMATION},
285         {"optional",                    tOPTIONAL},
286         {"out",                         tOUT},
287         {"pointer_default",             tPOINTERDEFAULT},
288         {"propget",                     tPROPGET},
289         {"propput",                     tPROPPUT},
290         {"propputref",                  tPROPPUTREF},
291         {"ptr",                         tPTR},
292         {"public",                      tPUBLIC},
293         {"range",                       tRANGE},
294         {"readonly",                    tREADONLY},
295         {"ref",                         tREF},
296         {"requestedit",                 tREQUESTEDIT},
297         {"restricted",                  tRESTRICTED},
298         {"retval",                      tRETVAL},
299         {"single",                      tSINGLE},
300         {"size_is",                     tSIZEIS},
301         {"source",                      tSOURCE},
302         {"string",                      tSTRING},
303         {"switch_is",                   tSWITCHIS},
304         {"switch_type",                 tSWITCHTYPE},
305         {"transmit_as",                 tTRANSMITAS},
306         {"unique",                      tUNIQUE},
307         {"uuid",                        tUUID},
308         {"v1_enum",                     tV1ENUM},
309         {"vararg",                      tVARARG},
310         {"version",                     tVERSION},
311         {"wire_marshal",                tWIREMARSHAL},
315 #define KWP(p) ((const struct keyword *)(p))
317 static int kw_cmp_func(const void *s1, const void *s2)
319         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
322 static int kw_token(const char *kw)
324         struct keyword key, *kwp;
325         key.kw = kw;
326         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
327         if (kwp) {
328                 parser_lval.str = xstrdup(kwp->kw);
329                 return kwp->token;
330         }
331         parser_lval.str = xstrdup(kw);
332         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
335 static int attr_token(const char *kw)
337         struct keyword key, *kwp;
338         key.kw = kw;
339         kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
340                       sizeof(attr_keywords[0]), kw_cmp_func);
341         if (kwp) {
342             parser_lval.str = xstrdup(kwp->kw);
343             return kwp->token;
344         }
345         return kw_token(kw);
348 static void addcchar(char c)
350         if(cbufidx >= cbufalloc)
351         {
352                 cbufalloc += 1024;
353                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
354                 if(cbufalloc > 65536)
355                         parser_warning("Reallocating string buffer larger than 64kB\n");
356         }
357         cbuffer[cbufidx++] = c;
360 static char *get_buffered_cstring(void)
362         addcchar(0);
363         return xstrdup(cbuffer);
366 static void pop_import(void)
368         int ptr = import_stack_ptr-1;
370         fclose(yyin);
371         yy_delete_buffer( YY_CURRENT_BUFFER );
372         yy_switch_to_buffer( import_stack[ptr].state );
373         if (temp_name) {
374                 unlink(temp_name);
375                 free(temp_name);
376         }
377         temp_name = import_stack[ptr].temp_name;
378         free( input_name );
379         input_name = import_stack[ptr].input_name;
380         line_number = import_stack[ptr].line_number;
381         import_stack_ptr--;
384 struct imports {
385         char *name;
386         struct imports *next;
387 } *first_import;
389 int do_import(char *fname)
391         FILE *f;
392         char *hname, *path, *p;
393         struct imports *import;
394         int ptr = import_stack_ptr;
395         int ret;
397         if (!parse_only && do_header) {
398                 hname = dup_basename(fname, ".idl");
399                 p = hname + strlen(hname) - 2;
400                 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
402                 fprintf(header, "#include <%s>\n", hname);
403                 free(hname);
404         }
406         import = first_import;
407         while (import && strcmp(import->name, fname))
408                 import = import->next;
409         if (import) return 0; /* already imported */
411         import = xmalloc(sizeof(struct imports));
412         import->name = xstrdup(fname);
413         import->next = first_import;
414         first_import = import;
416         if (!(path = wpp_find_include( fname, input_name )))
417             error_loc("Unable to open include file %s\n", fname);
419         import_stack[ptr].temp_name = temp_name;
420         import_stack[ptr].input_name = input_name;
421         import_stack[ptr].line_number = line_number;
422         import_stack_ptr++;
423         input_name = path;
424         line_number = 1;
426         ret = wpp_parse_temp( path, NULL, &temp_name );
427         if (ret) exit(1);
429         if((f = fopen(temp_name, "r")) == NULL)
430                 error_loc("Unable to open %s\n", temp_name);
432         import_stack[ptr].state = YY_CURRENT_BUFFER;
433         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
434         return 1;
437 void abort_import(void)
439         int ptr;
441         for (ptr=0; ptr<import_stack_ptr; ptr++)
442                 unlink(import_stack[ptr].temp_name);