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)
253 /* This table MUST be alphabetically sorted on the kw field */
254 static const struct keyword keywords[] = {
259 {"__fastcall", tFASTCALL},
261 {"__int3264", tINT3264},
263 {"__pascal", tPASCAL},
264 {"__stdcall", tSTDCALL},
266 {"_fastcall", tFASTCALL},
267 {"_pascal", tPASCAL},
268 {"_stdcall", tSTDCALL},
269 {"boolean", tBOOLEAN},
274 {"coclass", tCOCLASS},
276 {"cpp_quote", tCPPQUOTE},
277 {"default", tDEFAULT},
278 {"dispinterface", tDISPINTERFACE},
281 {"error_status_t", tERRORSTATUST},
284 {"handle_t", tHANDLET},
287 {"importlib", tIMPORTLIB},
290 {"interface", tINTERFACE},
291 {"library", tLIBRARY},
293 {"methods", tMETHODS},
295 {"namespace", tNAMESPACE},
297 {"properties", tPROPERTIES},
298 {"register", tREGISTER},
304 {"stdcall", tSTDCALL},
307 {"typedef", tTYPEDEF},
309 {"unsigned", tUNSIGNED},
313 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
315 /* keywords only recognized in attribute lists
316 * This table MUST be alphabetically sorted on the kw field
318 static const struct keyword attr_keywords[] =
320 {"aggregatable", tAGGREGATABLE},
321 {"allocate", tALLOCATE},
322 {"annotation", tANNOTATION},
323 {"apartment", tAPARTMENT},
324 {"appobject", tAPPOBJECT},
326 {"async_uuid", tASYNCUUID},
327 {"auto_handle", tAUTOHANDLE},
328 {"bindable", tBINDABLE},
330 {"broadcast", tBROADCAST},
331 {"byte_count", tBYTECOUNT},
332 {"call_as", tCALLAS},
333 {"callback", tCALLBACK},
335 {"comm_status", tCOMMSTATUS},
336 {"context_handle", tCONTEXTHANDLE},
337 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
338 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
339 {"control", tCONTROL},
341 {"defaultbind", tDEFAULTBIND},
342 {"defaultcollelem", tDEFAULTCOLLELEM},
343 {"defaultvalue", tDEFAULTVALUE},
344 {"defaultvtable", tDEFAULTVTABLE},
345 {"disable_consistency_check", tDISABLECONSISTENCYCHECK},
346 {"displaybind", tDISPLAYBIND},
347 {"dllname", tDLLNAME},
349 {"enable_allocate", tENABLEALLOCATE},
351 {"endpoint", tENDPOINT},
353 {"explicit_handle", tEXPLICITHANDLE},
354 {"fault_status", tFAULTSTATUS},
355 {"force_allocate", tFORCEALLOCATE},
358 {"helpcontext", tHELPCONTEXT},
359 {"helpfile", tHELPFILE},
360 {"helpstring", tHELPSTRING},
361 {"helpstringcontext", tHELPSTRINGCONTEXT},
362 {"helpstringdll", tHELPSTRINGDLL},
365 {"idempotent", tIDEMPOTENT},
368 {"immediatebind", tIMMEDIATEBIND},
369 {"implicit_handle", tIMPLICITHANDLE},
371 {"in_line", tIN_LINE},
372 {"input_sync", tINPUTSYNC},
374 {"length_is", tLENGTHIS},
375 {"licensed", tLICENSED},
378 {"message", tMESSAGE},
379 {"neutral", tNEUTRAL},
381 {"nonbrowsable", tNONBROWSABLE},
382 {"noncreatable", tNONCREATABLE},
383 {"nonextensible", tNONEXTENSIBLE},
385 {"notify_flag", tNOTIFYFLAG},
388 {"oleautomation", tOLEAUTOMATION},
389 {"optimize", tOPTIMIZE},
390 {"optional", tOPTIONAL},
392 {"partial_ignore", tPARTIALIGNORE},
393 {"pointer_default", tPOINTERDEFAULT},
395 {"propget", tPROPGET},
396 {"propput", tPROPPUT},
397 {"propputref", tPROPPUTREF},
402 {"readonly", tREADONLY},
404 {"represent_as", tREPRESENTAS},
405 {"requestedit", tREQUESTEDIT},
406 {"restricted", tRESTRICTED},
409 {"size_is", tSIZEIS},
411 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
413 {"switch_is", tSWITCHIS},
414 {"switch_type", tSWITCHTYPE},
415 {"threading", tTHREADING},
416 {"transmit_as", tTRANSMITAS},
417 {"uidefault", tUIDEFAULT},
419 {"user_marshal", tUSERMARSHAL},
420 {"usesgetlasterror", tUSESGETLASTERROR},
422 {"v1_enum", tV1ENUM},
424 {"version", tVERSION},
425 {"vi_progid", tVIPROGID},
426 {"wire_marshal", tWIREMARSHAL},
437 #define KWP(p) ((const struct keyword *)(p))
439 static int kw_cmp_func(const void *s1, const void *s2)
441 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
444 static int kw_token(const char *kw)
446 struct keyword key, *kwp;
448 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
449 if (kwp && (winrt_mode || kwp->token != tNAMESPACE)) {
450 parser_lval.str = xstrdup(kwp->kw);
453 parser_lval.str = xstrdup(kw);
454 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
457 static int attr_token(const char *kw)
459 struct keyword key, *kwp;
461 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
462 sizeof(attr_keywords[0]), kw_cmp_func);
464 parser_lval.str = xstrdup(kwp->kw);
470 static void addcchar(char c)
472 if(cbufidx >= cbufalloc)
475 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
476 if(cbufalloc > 65536)
477 parser_warning("Reallocating string buffer larger than 64kB\n");
479 cbuffer[cbufidx++] = c;
482 static char *get_buffered_cstring(void)
485 return xstrdup(cbuffer);
488 void pop_import(void)
490 int ptr = import_stack_ptr-1;
493 yy_delete_buffer( YY_CURRENT_BUFFER );
494 yy_switch_to_buffer( import_stack[ptr].state );
499 temp_name = import_stack[ptr].temp_name;
500 input_name = import_stack[ptr].input_name;
501 line_number = import_stack[ptr].line_number;
507 struct imports *next;
510 int do_import(char *fname)
514 struct imports *import;
515 int ptr = import_stack_ptr;
518 import = first_import;
519 while (import && strcmp(import->name, fname))
520 import = import->next;
521 if (import) return 0; /* already imported */
523 import = xmalloc(sizeof(struct imports));
524 import->name = xstrdup(fname);
525 import->next = first_import;
526 first_import = import;
528 /* don't search for a file name with a path in the include directories,
529 * for compatibility with MIDL */
530 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
531 path = xstrdup( fname );
532 else if (!(path = wpp_find_include( fname, input_name )))
533 error_loc("Unable to open include file %s\n", fname);
535 if (import_stack_ptr == MAX_IMPORT_DEPTH)
536 error_loc("Exceeded max import depth\n");
538 import_stack[ptr].temp_name = temp_name;
539 import_stack[ptr].input_name = input_name;
540 import_stack[ptr].line_number = line_number;
545 name = xstrdup( "widl.XXXXXX" );
546 if((fd = mkstemps( name, 0 )) == -1)
547 error("Could not generate a temp name from %s\n", name);
550 if (!(f = fdopen(fd, "wt")))
551 error("Could not open fd %s for writing\n", name);
553 ret = wpp_parse( path, f );
557 if((f = fopen(temp_name, "r")) == NULL)
558 error_loc("Unable to open %s\n", temp_name);
560 import_stack[ptr].state = YY_CURRENT_BUFFER;
561 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
565 void abort_import(void)
569 for (ptr=0; ptr<import_stack_ptr; ptr++)
570 unlink(import_stack[ptr].temp_name);
573 static void switch_to_acf(void)
575 int ptr = import_stack_ptr;
580 assert(import_stack_ptr == 0);
582 input_name = acf_name;
586 name = xstrdup( "widl.XXXXXX" );
587 if((fd = mkstemps( name, 0 )) == -1)
588 error("Could not generate a temp name from %s\n", name);
591 if (!(f = fdopen(fd, "wt")))
592 error("Could not open fd %s for writing\n", name);
594 ret = wpp_parse(input_name, f);
598 if((f = fopen(temp_name, "r")) == NULL)
599 error_loc("Unable to open %s\n", temp_name);
601 import_stack[ptr].state = YY_CURRENT_BUFFER;
602 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
605 static void warning_disable(int warning)
607 warning_t *warning_entry;
608 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
609 if(warning_entry->num == warning)
611 warning_entry = xmalloc( sizeof(*warning_entry) );
612 warning_entry->num = warning;
613 list_add_tail(disabled_warnings, &warning_entry->entry);
616 static void warning_enable(int warning)
618 warning_t *warning_entry;
619 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
620 if(warning_entry->num == warning)
622 list_remove(&warning_entry->entry);
628 int do_warning(char *toggle, warning_list_t *wnum)
630 warning_t *warning, *next;
632 if(!disabled_warnings)
634 disabled_warnings = xmalloc( sizeof(*disabled_warnings) );
635 list_init( disabled_warnings );
638 if(!strcmp(toggle, "disable"))
639 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
640 warning_disable(warning->num);
641 else if(!strcmp(toggle, "enable"))
642 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
643 warning_enable(warning->num);
647 LIST_FOR_EACH_ENTRY_SAFE(warning, next, wnum, warning_t, entry)
652 int is_warning_enabled(int warning)
654 warning_t *warning_entry;
655 if(!disabled_warnings)
657 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
658 if(warning_entry->num == warning)