uuid: Remove a few iids that don't belong here.
[wine/winequartzdrv.git] / tools / widl / parser.l
blobaf9445bafbc058f504c0c6e167ebccde1b1f701a
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>
48 #ifndef HAVE_UNISTD_H
49 #define YY_NO_UNISTD_H
50 #endif
52 #include "widl.h"
53 #include "utils.h"
54 #include "parser.h"
55 #include "wine/wpp.h"
57 #include "parser.tab.h"
59 extern char *temp_name;
61 static void addcchar(char c);
62 static char *get_buffered_cstring(void);
64 static char *cbuffer;
65 static int cbufidx;
66 static int cbufalloc = 0;
68 static int kw_token(const char *kw);
69 static int attr_token(const char *kw);
71 #define MAX_IMPORT_DEPTH 10
72 struct {
73   YY_BUFFER_STATE state;
74   char *input_name;
75   int   line_number;
76   char *temp_name;
77 } import_stack[MAX_IMPORT_DEPTH];
78 int import_stack_ptr = 0;
80 static void pop_import(void);
82 UUID *parse_uuid(const char *u)
84   UUID* uuid = xmalloc(sizeof(UUID));
85   char b[3];
86   /* it would be nice to use UuidFromStringA */
87   uuid->Data1 = strtoul(u, NULL, 16);
88   uuid->Data2 = strtoul(u+9, NULL, 16);
89   uuid->Data3 = strtoul(u+14, NULL, 16);
90   b[2] = 0;
91   memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
92   memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
93   memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
94   memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
95   memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
96   memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
97   memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
98   memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
99   return uuid;
105  **************************************************************************
106  * The flexer starts here
107  **************************************************************************
108  */
110 <INITIAL,ATTR>^{ws}*\#{ws}*     yy_push_state(PP_LINE);
111 <PP_LINE>[^\n]*         {
112                             int lineno;
113                             char *cptr, *fname;
114                             yy_pop_state();
115                             lineno = (int)strtol(yytext, &cptr, 10);
116                             if(!lineno)
117                                 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
118                             fname = strchr(cptr, '"');
119                             if(!fname)
120                                 error_loc("Malformed '#...' line-directive; missing filename\n");
121                             fname++;
122                             cptr = strchr(fname, '"');
123                             if(!cptr)
124                                 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
125                             *cptr = '\0';
126                             line_number = lineno - 1;  /* We didn't read the newline */
127                             free( input_name );
128                             input_name = xstrdup(fname);
129                         }
130 <INITIAL,ATTR>\"        yy_push_state(QUOTE); cbufidx = 0;
131 <QUOTE>\"               {
132                                 yy_pop_state();
133                                 parser_lval.str = get_buffered_cstring();
134                                 return aSTRING;
135                         }
136 <QUOTE>\\\\             |
137 <QUOTE>\\\"             addcchar(yytext[1]);
138 <QUOTE>\\.              addcchar('\\'); addcchar(yytext[1]);
139 <QUOTE>.                addcchar(yytext[0]);
140 <INITIAL,ATTR>\[        yy_push_state(ATTR); return '[';
141 <ATTR>\]                yy_pop_state(); return ']';
142 <ATTR>{cident}          return attr_token(yytext);
143 <ATTR>{uuid}                    {
144                                 parser_lval.uuid = parse_uuid(yytext);
145                                 return aUUID;
146                         }
147 <INITIAL,ATTR>{hex}     {
148                                 parser_lval.num = strtoul(yytext, NULL, 0);
149                                 return aHEXNUM;
150                         }
151 <INITIAL,ATTR>{int}     {
152                                 parser_lval.num = strtoul(yytext, NULL, 0);
153                                 return aNUM;
154                         }
155 <INITIAL>{double}       {
156                                 parser_lval.dbl = strtod(yytext, NULL);
157                                 return aDOUBLE;
158                         }
159 SAFEARRAY{ws}*/\(       return tSAFEARRAY;
160 {cident}                return kw_token(yytext);
161 <INITIAL,ATTR>\n        line_number++;
162 <INITIAL,ATTR>{ws}
163 <INITIAL,ATTR>\<\<      return SHL;
164 <INITIAL,ATTR>\>\>      return SHR;
165 <INITIAL,ATTR>.         return yytext[0];
166 <<EOF>>                 {
167                                 if (import_stack_ptr) {
168                                         pop_import();
169                                         return aEOF;
170                                 }
171                                 else yyterminate();
172                         }
175 #ifndef parser_wrap
176 int parser_wrap(void)
178         return 1;
180 #endif
182 struct keyword {
183         const char *kw;
184         int token;
187 static const struct keyword keywords[] = {
188         {"FALSE",                       tFALSE},
189         {"TRUE",                        tTRUE},
190         {"__cdecl",                     tCDECL},
191         {"__int64",                     tINT64},
192         {"__stdcall",                   tSTDCALL},
193         {"_stdcall",                    tSTDCALL},
194         {"boolean",                     tBOOLEAN},
195         {"byte",                        tBYTE},
196         {"callback",                    tCALLBACK},
197         {"case",                        tCASE},
198         {"char",                        tCHAR},
199         {"coclass",                     tCOCLASS},
200         {"code",                        tCODE},
201         {"comm_status",                 tCOMMSTATUS},
202         {"const",                       tCONST},
203         {"cpp_quote",                   tCPPQUOTE},
204         {"default",                     tDEFAULT},
205         {"dispinterface",               tDISPINTERFACE},
206         {"double",                      tDOUBLE},
207         {"enum",                        tENUM},
208         {"error_status_t",              tERRORSTATUST},
209         {"extern",                      tEXTERN},
210         {"float",                       tFLOAT},
211         {"handle_t",                    tHANDLET},
212         {"hyper",                       tHYPER},
213         {"import",                      tIMPORT},
214         {"importlib",                   tIMPORTLIB},
215         {"in_line",                     tINLINE},
216         {"int",                         tINT},
217         {"interface",                   tINTERFACE},
218         {"library",                     tLIBRARY},
219         {"long",                        tLONG},
220         {"methods",                     tMETHODS},
221         {"module",                      tMODULE},
222         {"properties",                  tPROPERTIES},
223         {"short",                       tSHORT},
224         {"signed",                      tSIGNED},
225         {"sizeof",                      tSIZEOF},
226         {"small",                       tSMALL},
227         {"struct",                      tSTRUCT},
228         {"switch",                      tSWITCH},
229         {"typedef",                     tTYPEDEF},
230         {"union",                       tUNION},
231         {"unsigned",                    tUNSIGNED},
232         {"void",                        tVOID},
233         {"wchar_t",                     tWCHAR},
235 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
237 /* keywords only recognized in attribute lists */
238 static const struct keyword attr_keywords[] =
240         {"aggregatable",                tAGGREGATABLE},
241         {"allocate",                    tALLOCATE},
242         {"appobject",                   tAPPOBJECT},
243         {"async",                       tASYNC},
244         {"async_uuid",                  tASYNCUUID},
245         {"auto_handle",                 tAUTOHANDLE},
246         {"bindable",                    tBINDABLE},
247         {"broadcast",                   tBROADCAST},
248         {"byte_count",                  tBYTECOUNT},
249         {"call_as",                     tCALLAS},
250         {"context_handle",              tCONTEXTHANDLE},
251         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
252         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
253         {"control",                     tCONTROL},
254         {"defaultcollelem",             tDEFAULTCOLLELEM},
255         {"defaultvalue",                tDEFAULTVALUE},
256         {"defaultvtable",               tDEFAULTVTABLE},
257         {"displaybind",                 tDISPLAYBIND},
258         {"dllname",                     tDLLNAME},
259         {"dual",                        tDUAL},
260         {"endpoint",                    tENDPOINT},
261         {"entry",                       tENTRY},
262         {"explicit_handle",             tEXPLICITHANDLE},
263         {"handle",                      tHANDLE},
264         {"helpcontext",                 tHELPCONTEXT},
265         {"helpfile",                    tHELPFILE},
266         {"helpstring",                  tHELPSTRING},
267         {"helpstringcontext",           tHELPSTRINGCONTEXT},
268         {"helpstringdll",               tHELPSTRINGDLL},
269         {"hidden",                      tHIDDEN},
270         {"id",                          tID},
271         {"idempotent",                  tIDEMPOTENT},
272         {"iid_is",                      tIIDIS},
273         {"immediatebind",               tIMMEDIATEBIND},
274         {"implicit_handle",             tIMPLICITHANDLE},
275         {"in",                          tIN},
276         {"input_sync",                  tINPUTSYNC},
277         {"lcid",                        tLCID},
278         {"length_is",                   tLENGTHIS},
279         {"local",                       tLOCAL},
280         {"nonbrowsable",                tNONBROWSABLE},
281         {"noncreatable",                tNONCREATABLE},
282         {"nonextensible",               tNONEXTENSIBLE},
283         {"object",                      tOBJECT},
284         {"odl",                         tODL},
285         {"oleautomation",               tOLEAUTOMATION},
286         {"optional",                    tOPTIONAL},
287         {"out",                         tOUT},
288         {"pointer_default",             tPOINTERDEFAULT},
289         {"propget",                     tPROPGET},
290         {"propput",                     tPROPPUT},
291         {"propputref",                  tPROPPUTREF},
292         {"ptr",                         tPTR},
293         {"public",                      tPUBLIC},
294         {"range",                       tRANGE},
295         {"readonly",                    tREADONLY},
296         {"ref",                         tREF},
297         {"requestedit",                 tREQUESTEDIT},
298         {"restricted",                  tRESTRICTED},
299         {"retval",                      tRETVAL},
300         {"single",                      tSINGLE},
301         {"size_is",                     tSIZEIS},
302         {"source",                      tSOURCE},
303         {"strict_context_handle",       tSTRICTCONTEXTHANDLE},
304         {"string",                      tSTRING},
305         {"switch_is",                   tSWITCHIS},
306         {"switch_type",                 tSWITCHTYPE},
307         {"transmit_as",                 tTRANSMITAS},
308         {"unique",                      tUNIQUE},
309         {"uuid",                        tUUID},
310         {"v1_enum",                     tV1ENUM},
311         {"vararg",                      tVARARG},
312         {"version",                     tVERSION},
313         {"wire_marshal",                tWIREMARSHAL},
317 #define KWP(p) ((const struct keyword *)(p))
319 static int kw_cmp_func(const void *s1, const void *s2)
321         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
324 static int kw_token(const char *kw)
326         struct keyword key, *kwp;
327         key.kw = kw;
328         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
329         if (kwp) {
330                 parser_lval.str = xstrdup(kwp->kw);
331                 return kwp->token;
332         }
333         parser_lval.str = xstrdup(kw);
334         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
337 static int attr_token(const char *kw)
339         struct keyword key, *kwp;
340         key.kw = kw;
341         kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
342                       sizeof(attr_keywords[0]), kw_cmp_func);
343         if (kwp) {
344             parser_lval.str = xstrdup(kwp->kw);
345             return kwp->token;
346         }
347         return kw_token(kw);
350 static void addcchar(char c)
352         if(cbufidx >= cbufalloc)
353         {
354                 cbufalloc += 1024;
355                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
356                 if(cbufalloc > 65536)
357                         parser_warning("Reallocating string buffer larger than 64kB\n");
358         }
359         cbuffer[cbufidx++] = c;
362 static char *get_buffered_cstring(void)
364         addcchar(0);
365         return xstrdup(cbuffer);
368 static void pop_import(void)
370         int ptr = import_stack_ptr-1;
372         fclose(yyin);
373         yy_delete_buffer( YY_CURRENT_BUFFER );
374         yy_switch_to_buffer( import_stack[ptr].state );
375         if (temp_name) {
376                 unlink(temp_name);
377                 free(temp_name);
378         }
379         temp_name = import_stack[ptr].temp_name;
380         free( input_name );
381         input_name = import_stack[ptr].input_name;
382         line_number = import_stack[ptr].line_number;
383         import_stack_ptr--;
386 struct imports {
387         char *name;
388         struct imports *next;
389 } *first_import;
391 int do_import(char *fname)
393         FILE *f;
394         char *hname, *path, *p;
395         struct imports *import;
396         int ptr = import_stack_ptr;
397         int ret;
399         if (!parse_only && do_header) {
400                 hname = dup_basename(fname, ".idl");
401                 p = hname + strlen(hname) - 2;
402                 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
404                 fprintf(header, "#include <%s>\n", hname);
405                 free(hname);
406         }
408         import = first_import;
409         while (import && strcmp(import->name, fname))
410                 import = import->next;
411         if (import) return 0; /* already imported */
413         import = xmalloc(sizeof(struct imports));
414         import->name = xstrdup(fname);
415         import->next = first_import;
416         first_import = import;
418         /* don't search for a file name with a path in the include directories,
419          * for compatibility with MIDL */
420         if (strchr( fname, '/' ) || strchr( fname, '\\' ))
421             path = strdup( fname );
422         else if (!(path = wpp_find_include( fname, input_name )))
423             error_loc("Unable to open include file %s\n", fname);
425         import_stack[ptr].temp_name = temp_name;
426         import_stack[ptr].input_name = input_name;
427         import_stack[ptr].line_number = line_number;
428         import_stack_ptr++;
429         input_name = path;
430         line_number = 1;
432         ret = wpp_parse_temp( path, NULL, &temp_name );
433         if (ret) exit(1);
435         if((f = fopen(temp_name, "r")) == NULL)
436                 error_loc("Unable to open %s\n", temp_name);
438         import_stack[ptr].state = YY_CURRENT_BUFFER;
439         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
440         return 1;
443 void abort_import(void)
445         int ptr;
447         for (ptr=0; ptr<import_stack_ptr; ptr++)
448                 unlink(import_stack[ptr].temp_name);