widl: Add support for parsing the annotation attribute.
[wine] / tools / widl / parser.l
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  */
20
21 %option stack
22 %option noinput nounput noyy_top_state
23 %option 8bit never-interactive prefix="parser_"
24
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]+)*
35
36 %x QUOTE
37 %x WSTRQUOTE
38 %x ATTR
39 %x PP_LINE
40
41 %{
42
43 #include "config.h"
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <assert.h>
50 #include <errno.h>
51 #include <limits.h>
52
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #else
56 #define YY_NO_UNISTD_H
57 #endif
58
59 #include "widl.h"
60 #include "utils.h"
61 #include "parser.h"
62 #include "wine/wpp.h"
63
64 #include "parser.tab.h"
65
66 extern char *temp_name;
67
68 static void addcchar(char c);
69 static char *get_buffered_cstring(void);
70
71 static char *cbuffer;
72 static int cbufidx;
73 static int cbufalloc = 0;
74
75 static int kw_token(const char *kw);
76 static int attr_token(const char *kw);
77
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;
86
87 /* converts an integer in string form to an unsigned long and prints an error
88  * on overflow */
89 static unsigned long xstrtoul(const char *nptr, char **endptr, int base)
90 {
91     unsigned long l;
92
93     errno = 0;
94     l = strtoul(nptr, endptr, base);
95     if (l == ULONG_MAX && errno == ERANGE)
96         error_loc("integer constant %s is too large\n", nptr);
97     return l;
98 }
99
100 UUID *parse_uuid(const char *u)
101 {
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;
118 }
119
120 %}
121
122 /*
123  **************************************************************************
124  * The flexer starts here
125  **************************************************************************
126  */
127 %%
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);
155 <WSTRQUOTE>\"           {
156                                 yy_pop_state();
157                                 parser_lval.str = get_buffered_cstring();
158                                 return aWSTRING;
159                         }
160 <QUOTE,WSTRQUOTE>\\\\   |
161 <QUOTE,WSTRQUOTE>\\\"   addcchar(yytext[1]);
162 <QUOTE,WSTRQUOTE>\\.    addcchar('\\'); addcchar(yytext[1]);
163 <QUOTE,WSTRQUOTE>.      addcchar(yytext[0]);
164 <INITIAL,ATTR>\[        yy_push_state(ATTR); return '[';
165 <ATTR>\]                yy_pop_state(); return ']';
166 <ATTR>{cident}          return attr_token(yytext);
167 <ATTR>{uuid}                    {
168                                 parser_lval.uuid = parse_uuid(yytext);
169                                 return aUUID;
170                         }
171 <INITIAL,ATTR>{hex}     {
172                                 parser_lval.num = xstrtoul(yytext, NULL, 0);
173                                 return aHEXNUM;
174                         }
175 <INITIAL,ATTR>{int}     {
176                                 parser_lval.num = xstrtoul(yytext, NULL, 0);
177                                 return aNUM;
178                         }
179 <INITIAL>{double}       {
180                                 parser_lval.dbl = strtod(yytext, NULL);
181                                 return aDOUBLE;
182                         }
183 SAFEARRAY{ws}*/\(       return tSAFEARRAY;
184 {cident}                return kw_token(yytext);
185 <INITIAL,ATTR>\n        line_number++;
186 <INITIAL,ATTR>{ws}
187 <INITIAL,ATTR>\<\<      return SHL;
188 <INITIAL,ATTR>\>\>      return SHR;
189 <INITIAL,ATTR>\-\>      return MEMBERPTR;
190 <INITIAL,ATTR>==        return EQUALITY;
191 <INITIAL,ATTR>!=        return INEQUALITY;
192 <INITIAL,ATTR>\>=       return GREATEREQUAL;
193 <INITIAL,ATTR>\<=       return LESSEQUAL;
194 <INITIAL,ATTR>\|\|      return LOGICALOR;
195 <INITIAL,ATTR>&&        return LOGICALAND;
196 <INITIAL,ATTR>.         return yytext[0];
197 <<EOF>>                 {
198                                 if (import_stack_ptr)
199                                         return aEOF;
200                                 else yyterminate();
201                         }
202 %%
203
204 #ifndef parser_wrap
205 int parser_wrap(void)
206 {
207         return 1;
208 }
209 #endif
210
211 struct keyword {
212         const char *kw;
213         int token;
214 };
215
216 /* This table MUST be alphabetically sorted on the kw field */
217 static const struct keyword keywords[] = {
218         {"FALSE",                       tFALSE},
219         {"NULL",                        tNULL},
220         {"TRUE",                        tTRUE},
221         {"__cdecl",                     tCDECL},
222         {"__fastcall",                  tFASTCALL},
223         {"__int64",                     tINT64},
224         {"__pascal",                    tPASCAL},
225         {"__stdcall",                   tSTDCALL},
226         {"_cdecl",                      tCDECL},
227         {"_fastcall",                   tFASTCALL},
228         {"_pascal",                     tPASCAL},
229         {"_stdcall",                    tSTDCALL},
230         {"boolean",                     tBOOLEAN},
231         {"byte",                        tBYTE},
232         {"case",                        tCASE},
233         {"cdecl",                       tCDECL},
234         {"char",                        tCHAR},
235         {"coclass",                     tCOCLASS},
236         {"const",                       tCONST},
237         {"cpp_quote",                   tCPPQUOTE},
238         {"default",                     tDEFAULT},
239         {"dispinterface",               tDISPINTERFACE},
240         {"double",                      tDOUBLE},
241         {"enum",                        tENUM},
242         {"error_status_t",              tERRORSTATUST},
243         {"extern",                      tEXTERN},
244         {"float",                       tFLOAT},
245         {"handle_t",                    tHANDLET},
246         {"hyper",                       tHYPER},
247         {"import",                      tIMPORT},
248         {"importlib",                   tIMPORTLIB},
249         {"inline",                      tINLINE},
250         {"int",                         tINT},
251         {"interface",                   tINTERFACE},
252         {"library",                     tLIBRARY},
253         {"long",                        tLONG},
254         {"methods",                     tMETHODS},
255         {"module",                      tMODULE},
256         {"pascal",                      tPASCAL},
257         {"properties",                  tPROPERTIES},
258         {"register",                    tREGISTER},
259         {"short",                       tSHORT},
260         {"signed",                      tSIGNED},
261         {"sizeof",                      tSIZEOF},
262         {"small",                       tSMALL},
263         {"static",                      tSTATIC},
264         {"stdcall",                     tSTDCALL},
265         {"struct",                      tSTRUCT},
266         {"switch",                      tSWITCH},
267         {"typedef",                     tTYPEDEF},
268         {"union",                       tUNION},
269         {"unsigned",                    tUNSIGNED},
270         {"void",                        tVOID},
271         {"wchar_t",                     tWCHAR},
272 };
273 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
274
275 /* keywords only recognized in attribute lists
276  * This table MUST be alphabetically sorted on the kw field
277  */
278 static const struct keyword attr_keywords[] =
279 {
280         {"aggregatable",                tAGGREGATABLE},
281         {"allocate",                    tALLOCATE},
282         {"annotation",                  tANNOTATION},
283         {"appobject",                   tAPPOBJECT},
284         {"async",                       tASYNC},
285         {"async_uuid",                  tASYNCUUID},
286         {"auto_handle",                 tAUTOHANDLE},
287         {"bindable",                    tBINDABLE},
288         {"broadcast",                   tBROADCAST},
289         {"byte_count",                  tBYTECOUNT},
290         {"call_as",                     tCALLAS},
291         {"callback",                    tCALLBACK},
292         {"code",                        tCODE},
293         {"comm_status",                 tCOMMSTATUS},
294         {"context_handle",              tCONTEXTHANDLE},
295         {"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE},
296         {"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE},
297         {"control",                     tCONTROL},
298         {"defaultcollelem",             tDEFAULTCOLLELEM},
299         {"defaultvalue",                tDEFAULTVALUE},
300         {"defaultvtable",               tDEFAULTVTABLE},
301         {"displaybind",                 tDISPLAYBIND},
302         {"dllname",                     tDLLNAME},
303         {"dual",                        tDUAL},
304         {"endpoint",                    tENDPOINT},
305         {"entry",                       tENTRY},
306         {"explicit_handle",             tEXPLICITHANDLE},
307         {"handle",                      tHANDLE},
308         {"helpcontext",                 tHELPCONTEXT},
309         {"helpfile",                    tHELPFILE},
310         {"helpstring",                  tHELPSTRING},
311         {"helpstringcontext",           tHELPSTRINGCONTEXT},
312         {"helpstringdll",               tHELPSTRINGDLL},
313         {"hidden",                      tHIDDEN},
314         {"id",                          tID},
315         {"idempotent",                  tIDEMPOTENT},
316         {"iid_is",                      tIIDIS},
317         {"immediatebind",               tIMMEDIATEBIND},
318         {"implicit_handle",             tIMPLICITHANDLE},
319         {"in",                          tIN},
320         {"in_line",                     tIN_LINE},
321         {"input_sync",                  tINPUTSYNC},
322         {"lcid",                        tLCID},
323         {"length_is",                   tLENGTHIS},
324         {"local",                       tLOCAL},
325         {"nonbrowsable",                tNONBROWSABLE},
326         {"noncreatable",                tNONCREATABLE},
327         {"nonextensible",               tNONEXTENSIBLE},
328         {"object",                      tOBJECT},
329         {"odl",                         tODL},
330         {"oleautomation",               tOLEAUTOMATION},
331         {"optional",                    tOPTIONAL},
332         {"out",                         tOUT},
333         {"pointer_default",             tPOINTERDEFAULT},
334         {"propget",                     tPROPGET},
335         {"propput",                     tPROPPUT},
336         {"propputref",                  tPROPPUTREF},
337         {"ptr",                         tPTR},
338         {"public",                      tPUBLIC},
339         {"range",                       tRANGE},
340         {"readonly",                    tREADONLY},
341         {"ref",                         tREF},
342         {"requestedit",                 tREQUESTEDIT},
343         {"restricted",                  tRESTRICTED},
344         {"retval",                      tRETVAL},
345         {"size_is",                     tSIZEIS},
346         {"source",                      tSOURCE},
347         {"strict_context_handle",       tSTRICTCONTEXTHANDLE},
348         {"string",                      tSTRING},
349         {"switch_is",                   tSWITCHIS},
350         {"switch_type",                 tSWITCHTYPE},
351         {"transmit_as",                 tTRANSMITAS},
352         {"unique",                      tUNIQUE},
353         {"uuid",                        tUUID},
354         {"v1_enum",                     tV1ENUM},
355         {"vararg",                      tVARARG},
356         {"version",                     tVERSION},
357         {"wire_marshal",                tWIREMARSHAL},
358 };
359
360
361 #define KWP(p) ((const struct keyword *)(p))
362
363 static int kw_cmp_func(const void *s1, const void *s2)
364 {
365         return strcmp(KWP(s1)->kw, KWP(s2)->kw);
366 }
367
368 static int kw_token(const char *kw)
369 {
370         struct keyword key, *kwp;
371         key.kw = kw;
372         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
373         if (kwp) {
374                 parser_lval.str = xstrdup(kwp->kw);
375                 return kwp->token;
376         }
377         parser_lval.str = xstrdup(kw);
378         return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
379 }
380
381 static int attr_token(const char *kw)
382 {
383         struct keyword key, *kwp;
384         key.kw = kw;
385         kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
386                       sizeof(attr_keywords[0]), kw_cmp_func);
387         if (kwp) {
388             parser_lval.str = xstrdup(kwp->kw);
389             return kwp->token;
390         }
391         return kw_token(kw);
392 }
393
394 static void addcchar(char c)
395 {
396         if(cbufidx >= cbufalloc)
397         {
398                 cbufalloc += 1024;
399                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
400                 if(cbufalloc > 65536)
401                         parser_warning("Reallocating string buffer larger than 64kB\n");
402         }
403         cbuffer[cbufidx++] = c;
404 }
405
406 static char *get_buffered_cstring(void)
407 {
408         addcchar(0);
409         return xstrdup(cbuffer);
410 }
411
412 void pop_import(void)
413 {
414         int ptr = import_stack_ptr-1;
415
416         fclose(yyin);
417         yy_delete_buffer( YY_CURRENT_BUFFER );
418         yy_switch_to_buffer( import_stack[ptr].state );
419         if (temp_name) {
420                 unlink(temp_name);
421                 free(temp_name);
422         }
423         temp_name = import_stack[ptr].temp_name;
424         input_name = import_stack[ptr].input_name;
425         line_number = import_stack[ptr].line_number;
426         import_stack_ptr--;
427 }
428
429 struct imports {
430         char *name;
431         struct imports *next;
432 } *first_import;
433
434 int do_import(char *fname)
435 {
436         FILE *f;
437         char *path;
438         struct imports *import;
439         int ptr = import_stack_ptr;
440         int ret;
441
442         import = first_import;
443         while (import && strcmp(import->name, fname))
444                 import = import->next;
445         if (import) return 0; /* already imported */
446
447         import = xmalloc(sizeof(struct imports));
448         import->name = xstrdup(fname);
449         import->next = first_import;
450         first_import = import;
451
452         /* don't search for a file name with a path in the include directories,
453          * for compatibility with MIDL */
454         if (strchr( fname, '/' ) || strchr( fname, '\\' ))
455             path = strdup( fname );
456         else if (!(path = wpp_find_include( fname, input_name )))
457             error_loc("Unable to open include file %s\n", fname);
458
459         import_stack[ptr].temp_name = temp_name;
460         import_stack[ptr].input_name = input_name;
461         import_stack[ptr].line_number = line_number;
462         import_stack_ptr++;
463         input_name = path;
464         line_number = 1;
465
466         ret = wpp_parse_temp( path, NULL, &temp_name );
467         if (ret) exit(1);
468
469         if((f = fopen(temp_name, "r")) == NULL)
470                 error_loc("Unable to open %s\n", temp_name);
471
472         import_stack[ptr].state = YY_CURRENT_BUFFER;
473         yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
474         return 1;
475 }
476
477 void abort_import(void)
478 {
479         int ptr;
480
481         for (ptr=0; ptr<import_stack_ptr; ptr++)
482                 unlink(import_stack[ptr].temp_name);
483 }