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