4 * Copyright 2002 Ove Kaaven
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.
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.
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
22 %option noinput nounput noyy_top_state
23 %option 8bit never-interactive prefix="parser_"
27 cident [a-zA-Z_][0-9a-zA-Z_]*
30 int [0-9]+({l_suffix}?{u_suffix}?|{u_suffix}?{l_suffix}?)?
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]+)*
46 #include "wine/port.h"
59 #define YY_NO_UNISTD_H
67 #include "parser.tab.h"
69 static void addcchar(char c);
70 static char *get_buffered_cstring(void);
74 static int cbufalloc = 0;
76 static int kw_token(const char *kw);
77 static int attr_token(const char *kw);
79 static void switch_to_acf(void);
81 static warning_list_t *disabled_warnings = NULL;
83 #define MAX_IMPORT_DEPTH 20
85 YY_BUFFER_STATE state;
89 } import_stack[MAX_IMPORT_DEPTH];
90 int import_stack_ptr = 0;
92 /* converts an integer in string form to an unsigned long and prints an error
94 static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
99 val = strtoul(nptr, endptr, base);
100 if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
101 error_loc("integer constant %s is too large\n", nptr);
105 UUID *parse_uuid(const char *u)
107 UUID* uuid = xmalloc(sizeof(UUID));
109 /* it would be nice to use UuidFromStringA */
110 uuid->Data1 = strtoul(u, NULL, 16);
111 uuid->Data2 = strtoul(u+9, NULL, 16);
112 uuid->Data3 = strtoul(u+14, NULL, 16);
114 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
115 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
116 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
117 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
118 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
119 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
120 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
121 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
128 **************************************************************************
129 * The flexer starts here
130 **************************************************************************
133 <INITIAL>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(PP_PRAGMA);
134 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
139 lineno = (int)strtol(yytext, &cptr, 10);
141 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
142 fname = strchr(cptr, '"');
144 error_loc("Malformed '#...' line-directive; missing filename\n");
146 cptr = strchr(fname, '"');
148 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
150 line_number = lineno - 1; /* We didn't read the newline */
151 input_name = xstrdup(fname);
153 <PP_PRAGMA>midl_echo[^\n]* yyless(9); yy_pop_state(); return tCPPQUOTE;
154 <PP_PRAGMA>winrt[^\n]* {
155 if(import_stack_ptr) {
157 error_loc("winrt IDL file imported in non-winrt mode\n");
159 const char *ptr = yytext+5;
165 if(!strncmp(ptr, "ns_prefix", 9) && (!*(ptr += 9) || isspace(*ptr)))
166 use_abi_namespace = TRUE;
170 <PP_PRAGMA>[^\n]* parser_lval.str = xstrdup(yytext); yy_pop_state(); return aPRAGMA;
171 <INITIAL>^{ws}*midl_pragma{ws}+warning return tPRAGMA_WARNING;
172 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
175 parser_lval.str = get_buffered_cstring();
178 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE); cbufidx = 0;
181 parser_lval.str = get_buffered_cstring();
184 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
187 parser_lval.str = get_buffered_cstring();
190 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
191 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
192 <SQUOTE>\\\' addcchar(yytext[1]);
193 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
194 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
195 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
196 <ATTR>\] yy_pop_state(); return ']';
197 <ATTR>{cident} return attr_token(yytext);
199 parser_lval.uuid = parse_uuid(yytext);
202 <INITIAL,ATTR>{hex} {
203 parser_lval.num = xstrtoul(yytext, NULL, 0);
206 <INITIAL,ATTR>{int} {
207 parser_lval.num = xstrtoul(yytext, NULL, 0);
211 parser_lval.dbl = strtod(yytext, NULL);
214 SAFEARRAY{ws}*/\( return tSAFEARRAY;
215 {cident} return kw_token(yytext);
216 <INITIAL,ATTR>\n line_number++;
218 <INITIAL,ATTR>\<\< return SHL;
219 <INITIAL,ATTR>\>\> return SHR;
220 <INITIAL,ATTR>\-\> return MEMBERPTR;
221 <INITIAL,ATTR>== return EQUALITY;
222 <INITIAL,ATTR>!= return INEQUALITY;
223 <INITIAL,ATTR>\>= return GREATEREQUAL;
224 <INITIAL,ATTR>\<= return LESSEQUAL;
225 <INITIAL,ATTR>\|\| return LOGICALOR;
226 <INITIAL,ATTR>&& return LOGICALAND;
227 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
228 <INITIAL,ATTR>. return yytext[0];
230 if (import_stack_ptr)
242 int parser_wrap(void)
254 /* This table MUST be alphabetically sorted on the kw field */
255 static const struct keyword keywords[] = {
256 {"FALSE", tFALSE, 0},
259 {"__cdecl", tCDECL, 0},
260 {"__fastcall", tFASTCALL, 0},
261 {"__int32", tINT32, 0},
262 {"__int3264", tINT3264, 0},
263 {"__int64", tINT64, 0},
264 {"__pascal", tPASCAL, 0},
265 {"__stdcall", tSTDCALL, 0},
266 {"_cdecl", tCDECL, 0},
267 {"_fastcall", tFASTCALL, 0},
268 {"_pascal", tPASCAL, 0},
269 {"_stdcall", tSTDCALL, 0},
270 {"apicontract", tAPICONTRACT, 1},
271 {"boolean", tBOOLEAN, 0},
274 {"cdecl", tCDECL, 0},
276 {"coclass", tCOCLASS, 0},
277 {"const", tCONST, 0},
278 {"cpp_quote", tCPPQUOTE, 0},
279 {"declare", tDECLARE, 1},
280 {"default", tDEFAULT, 0},
281 {"delegate", tDELEGATE, 1},
282 {"dispinterface", tDISPINTERFACE, 0},
283 {"double", tDOUBLE, 0},
285 {"error_status_t", tERRORSTATUST, 0},
286 {"extern", tEXTERN, 0},
287 {"float", tFLOAT, 0},
288 {"handle_t", tHANDLET, 0},
289 {"hyper", tHYPER, 0},
290 {"import", tIMPORT, 0},
291 {"importlib", tIMPORTLIB, 0},
292 {"inline", tINLINE, 0},
294 {"interface", tINTERFACE, 0},
295 {"library", tLIBRARY, 0},
297 {"methods", tMETHODS, 0},
298 {"module", tMODULE, 0},
299 {"namespace", tNAMESPACE, 1},
300 {"pascal", tPASCAL, 0},
301 {"properties", tPROPERTIES, 0},
302 {"register", tREGISTER, 0},
303 {"requires", tREQUIRES, 1},
304 {"runtimeclass", tRUNTIMECLASS, 1},
305 {"short", tSHORT, 0},
306 {"signed", tSIGNED, 0},
307 {"sizeof", tSIZEOF, 0},
308 {"small", tSMALL, 0},
309 {"static", tSTATIC, 0},
310 {"stdcall", tSTDCALL, 0},
311 {"struct", tSTRUCT, 0},
312 {"switch", tSWITCH, 0},
313 {"typedef", tTYPEDEF, 0},
314 {"union", tUNION, 0},
315 {"unsigned", tUNSIGNED, 0},
317 {"wchar_t", tWCHAR, 0},
319 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
321 /* keywords only recognized in attribute lists
322 * This table MUST be alphabetically sorted on the kw field
324 static const struct keyword attr_keywords[] =
326 {"activatable", tACTIVATABLE, 1},
327 {"aggregatable", tAGGREGATABLE, 0},
328 {"agile", tAGILE, 1},
329 {"all_nodes", tALLNODES, 0},
330 {"allocate", tALLOCATE, 0},
331 {"annotation", tANNOTATION, 0},
332 {"apartment", tAPARTMENT, 0},
333 {"appobject", tAPPOBJECT, 0},
334 {"async", tASYNC, 0},
335 {"async_uuid", tASYNCUUID, 0},
336 {"auto_handle", tAUTOHANDLE, 0},
337 {"bindable", tBINDABLE, 0},
339 {"broadcast", tBROADCAST, 0},
340 {"byte_count", tBYTECOUNT, 0},
341 {"call_as", tCALLAS, 0},
342 {"callback", tCALLBACK, 0},
344 {"comm_status", tCOMMSTATUS, 0},
345 {"context_handle", tCONTEXTHANDLE, 0},
346 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE, 0},
347 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE, 0},
348 {"contract", tCONTRACT, 1},
349 {"contractversion", tCONTRACTVERSION, 1},
350 {"control", tCONTROL, 0},
351 {"custom", tCUSTOM, 0},
352 {"decode", tDECODE, 0},
353 {"defaultbind", tDEFAULTBIND, 0},
354 {"defaultcollelem", tDEFAULTCOLLELEM, 0},
355 {"defaultvalue", tDEFAULTVALUE, 0},
356 {"defaultvtable", tDEFAULTVTABLE, 0},
357 {"disable_consistency_check", tDISABLECONSISTENCYCHECK, 0},
358 {"displaybind", tDISPLAYBIND, 0},
359 {"dllname", tDLLNAME, 0},
360 {"dont_free", tDONTFREE, 0},
362 {"enable_allocate", tENABLEALLOCATE, 0},
363 {"encode", tENCODE, 0},
364 {"endpoint", tENDPOINT, 0},
365 {"entry", tENTRY, 0},
366 {"eventadd", tEVENTADD, 1},
367 {"eventremove", tEVENTREMOVE, 1},
368 {"exclusiveto", tEXCLUSIVETO, 1},
369 {"explicit_handle", tEXPLICITHANDLE, 0},
370 {"fault_status", tFAULTSTATUS, 0},
371 {"flags", tFLAGS, 1},
372 {"force_allocate", tFORCEALLOCATE, 0},
374 {"handle", tHANDLE, 0},
375 {"helpcontext", tHELPCONTEXT, 0},
376 {"helpfile", tHELPFILE, 0},
377 {"helpstring", tHELPSTRING, 0},
378 {"helpstringcontext", tHELPSTRINGCONTEXT, 0},
379 {"helpstringdll", tHELPSTRINGDLL, 0},
380 {"hidden", tHIDDEN, 0},
382 {"idempotent", tIDEMPOTENT, 0},
383 {"ignore", tIGNORE, 0},
384 {"iid_is", tIIDIS, 0},
385 {"immediatebind", tIMMEDIATEBIND, 0},
386 {"implicit_handle", tIMPLICITHANDLE, 0},
388 {"in_line", tIN_LINE, 0},
389 {"input_sync", tINPUTSYNC, 0},
391 {"length_is", tLENGTHIS, 0},
392 {"licensed", tLICENSED, 0},
393 {"local", tLOCAL, 0},
394 {"marshaling_behavior", tMARSHALINGBEHAVIOR, 1},
395 {"maybe", tMAYBE, 0},
396 {"message", tMESSAGE, 0},
398 {"neutral", tNEUTRAL, 0},
399 {"nocode", tNOCODE, 0},
400 {"nonbrowsable", tNONBROWSABLE, 0},
401 {"noncreatable", tNONCREATABLE, 0},
403 {"nonextensible", tNONEXTENSIBLE, 0},
404 {"notify", tNOTIFY, 0},
405 {"notify_flag", tNOTIFYFLAG, 0},
406 {"object", tOBJECT, 0},
408 {"oleautomation", tOLEAUTOMATION, 0},
409 {"optimize", tOPTIMIZE, 0},
410 {"optional", tOPTIONAL, 0},
412 {"partial_ignore", tPARTIALIGNORE, 0},
413 {"pointer_default", tPOINTERDEFAULT, 0},
414 {"progid", tPROGID, 0},
415 {"propget", tPROPGET, 0},
416 {"propput", tPROPPUT, 0},
417 {"propputref", tPROPPUTREF, 0},
418 {"proxy", tPROXY, 0},
420 {"public", tPUBLIC, 0},
421 {"range", tRANGE, 0},
422 {"readonly", tREADONLY, 0},
424 {"represent_as", tREPRESENTAS, 0},
425 {"requestedit", tREQUESTEDIT, 0},
426 {"restricted", tRESTRICTED, 0},
427 {"retval", tRETVAL, 0},
428 {"single", tSINGLE, 0},
429 {"single_node", tSINGLENODE, 0},
430 {"size_is", tSIZEIS, 0},
431 {"source", tSOURCE, 0},
432 {"standard", tSTANDARD, 1},
433 {"static", tSTATIC, 1},
434 {"strict_context_handle", tSTRICTCONTEXTHANDLE, 0},
435 {"string", tSTRING, 0},
436 {"switch_is", tSWITCHIS, 0},
437 {"switch_type", tSWITCHTYPE, 0},
438 {"threading", tTHREADING, 0},
439 {"transmit_as", tTRANSMITAS, 0},
440 {"uidefault", tUIDEFAULT, 0},
441 {"unique", tUNIQUE, 0},
442 {"user_marshal", tUSERMARSHAL, 0},
443 {"usesgetlasterror", tUSESGETLASTERROR, 0},
445 {"v1_enum", tV1ENUM, 0},
446 {"vararg", tVARARG, 0},
447 {"version", tVERSION, 0},
448 {"vi_progid", tVIPROGID, 0},
449 {"wire_marshal", tWIREMARSHAL, 0},
459 #define KWP(p) ((const struct keyword *)(p))
461 static int kw_cmp_func(const void *s1, const void *s2)
463 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
466 static int kw_token(const char *kw)
468 struct keyword key, *kwp;
470 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
471 if (kwp && (!kwp->winrt_only || winrt_mode)) {
472 parser_lval.str = xstrdup(kwp->kw);
475 parser_lval.str = xstrdup(kw);
476 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
479 static int attr_token(const char *kw)
481 struct keyword key, *kwp;
483 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
484 sizeof(attr_keywords[0]), kw_cmp_func);
485 if (kwp && (!kwp->winrt_only || winrt_mode)) {
486 parser_lval.str = xstrdup(kwp->kw);
492 static void addcchar(char c)
494 if(cbufidx >= cbufalloc)
497 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
498 if(cbufalloc > 65536)
499 parser_warning("Reallocating string buffer larger than 64kB\n");
501 cbuffer[cbufidx++] = c;
504 static char *get_buffered_cstring(void)
507 return xstrdup(cbuffer);
510 void pop_import(void)
512 int ptr = import_stack_ptr-1;
515 yy_delete_buffer( YY_CURRENT_BUFFER );
516 yy_switch_to_buffer( import_stack[ptr].state );
521 temp_name = import_stack[ptr].temp_name;
522 input_name = import_stack[ptr].input_name;
523 line_number = import_stack[ptr].line_number;
529 struct imports *next;
532 int do_import(char *fname)
536 struct imports *import;
537 int ptr = import_stack_ptr;
540 import = first_import;
541 while (import && strcmp(import->name, fname))
542 import = import->next;
543 if (import) return 0; /* already imported */
545 import = xmalloc(sizeof(struct imports));
546 import->name = xstrdup(fname);
547 import->next = first_import;
548 first_import = import;
550 /* don't search for a file name with a path in the include directories,
551 * for compatibility with MIDL */
552 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
553 path = xstrdup( fname );
554 else if (!(path = wpp_find_include( fname, input_name )))
555 error_loc("Unable to open include file %s\n", fname);
557 if (import_stack_ptr == MAX_IMPORT_DEPTH)
558 error_loc("Exceeded max import depth\n");
560 import_stack[ptr].temp_name = temp_name;
561 import_stack[ptr].input_name = input_name;
562 import_stack[ptr].line_number = line_number;
567 name = xstrdup( "widl.XXXXXX" );
568 if((fd = mkstemps( name, 0 )) == -1)
569 error("Could not generate a temp name from %s\n", name);
572 if (!(f = fdopen(fd, "wt")))
573 error("Could not open fd %s for writing\n", name);
575 ret = wpp_parse( path, f );
579 if((f = fopen(temp_name, "r")) == NULL)
580 error_loc("Unable to open %s\n", temp_name);
582 import_stack[ptr].state = YY_CURRENT_BUFFER;
583 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
587 void abort_import(void)
591 for (ptr=0; ptr<import_stack_ptr; ptr++)
592 unlink(import_stack[ptr].temp_name);
595 static void switch_to_acf(void)
597 int ptr = import_stack_ptr;
602 assert(import_stack_ptr == 0);
604 input_name = acf_name;
608 name = xstrdup( "widl.XXXXXX" );
609 if((fd = mkstemps( name, 0 )) == -1)
610 error("Could not generate a temp name from %s\n", name);
613 if (!(f = fdopen(fd, "wt")))
614 error("Could not open fd %s for writing\n", name);
616 ret = wpp_parse(input_name, f);
620 if((f = fopen(temp_name, "r")) == NULL)
621 error_loc("Unable to open %s\n", temp_name);
623 import_stack[ptr].state = YY_CURRENT_BUFFER;
624 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
627 static void warning_disable(int warning)
629 warning_t *warning_entry;
630 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
631 if(warning_entry->num == warning)
633 warning_entry = xmalloc( sizeof(*warning_entry) );
634 warning_entry->num = warning;
635 list_add_tail(disabled_warnings, &warning_entry->entry);
638 static void warning_enable(int warning)
640 warning_t *warning_entry;
641 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
642 if(warning_entry->num == warning)
644 list_remove(&warning_entry->entry);
650 int do_warning(const char *toggle, warning_list_t *wnum)
652 warning_t *warning, *next;
654 if(!disabled_warnings)
656 disabled_warnings = xmalloc( sizeof(*disabled_warnings) );
657 list_init( disabled_warnings );
660 if(!strcmp(toggle, "disable"))
661 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
662 warning_disable(warning->num);
663 else if(!strcmp(toggle, "enable") || !strcmp(toggle, "default"))
664 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
665 warning_enable(warning->num);
669 LIST_FOR_EACH_ENTRY_SAFE(warning, next, wnum, warning_t, entry)
674 int is_warning_enabled(int warning)
676 warning_t *warning_entry;
677 if(!disabled_warnings)
679 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
680 if(warning_entry->num == warning)