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 warning_list_t *disabled_warnings = NULL;
81 #define MAX_IMPORT_DEPTH 10
83 YY_BUFFER_STATE state;
87 } import_stack[MAX_IMPORT_DEPTH];
88 int import_stack_ptr = 0;
90 /* converts an integer in string form to an unsigned long and prints an error
92 static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
97 val = strtoul(nptr, endptr, base);
98 if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
99 error_loc("integer constant %s is too large\n", nptr);
103 UUID *parse_uuid(const char *u)
105 UUID* uuid = xmalloc(sizeof(UUID));
107 /* it would be nice to use UuidFromStringA */
108 uuid->Data1 = strtoul(u, NULL, 16);
109 uuid->Data2 = strtoul(u+9, NULL, 16);
110 uuid->Data3 = strtoul(u+14, NULL, 16);
112 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
113 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
114 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
115 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
116 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
117 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
118 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
119 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
126 **************************************************************************
127 * The flexer starts here
128 **************************************************************************
131 <INITIAL>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(PP_PRAGMA);
132 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
137 lineno = (int)strtol(yytext, &cptr, 10);
139 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
140 fname = strchr(cptr, '"');
142 error_loc("Malformed '#...' line-directive; missing filename\n");
144 cptr = strchr(fname, '"');
146 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
148 line_number = lineno - 1; /* We didn't read the newline */
149 input_name = xstrdup(fname);
151 <PP_PRAGMA>midl_echo[^\n]* yyless(9); yy_pop_state(); return tCPPQUOTE;
152 <PP_PRAGMA>winrt[^\n]* {
153 if(import_stack_ptr) {
155 error_loc("winrt IDL file imported in non-winrt mode\n");
157 const char *ptr = yytext+5;
163 if(!strncmp(ptr, "ns_prefix", 9) && (!*(ptr += 9) || isspace(*ptr)))
164 use_abi_namespace = TRUE;
168 <PP_PRAGMA>[^\n]* parser_lval.str = xstrdup(yytext); yy_pop_state(); return aPRAGMA;
169 <INITIAL>^{ws}*midl_pragma{ws}+warning return tPRAGMA_WARNING;
170 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
173 parser_lval.str = get_buffered_cstring();
176 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE); cbufidx = 0;
179 parser_lval.str = get_buffered_cstring();
182 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
185 parser_lval.str = get_buffered_cstring();
188 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
189 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
190 <SQUOTE>\\\' addcchar(yytext[1]);
191 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
192 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
193 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
194 <ATTR>\] yy_pop_state(); return ']';
195 <ATTR>{cident} return attr_token(yytext);
197 parser_lval.uuid = parse_uuid(yytext);
200 <INITIAL,ATTR>{hex} {
201 parser_lval.num = xstrtoul(yytext, NULL, 0);
204 <INITIAL,ATTR>{int} {
205 parser_lval.num = xstrtoul(yytext, NULL, 0);
209 parser_lval.dbl = strtod(yytext, NULL);
212 SAFEARRAY{ws}*/\( return tSAFEARRAY;
213 {cident} return kw_token(yytext);
214 <INITIAL,ATTR>\n line_number++;
216 <INITIAL,ATTR>\<\< return SHL;
217 <INITIAL,ATTR>\>\> return SHR;
218 <INITIAL,ATTR>\-\> return MEMBERPTR;
219 <INITIAL,ATTR>== return EQUALITY;
220 <INITIAL,ATTR>!= return INEQUALITY;
221 <INITIAL,ATTR>\>= return GREATEREQUAL;
222 <INITIAL,ATTR>\<= return LESSEQUAL;
223 <INITIAL,ATTR>\|\| return LOGICALOR;
224 <INITIAL,ATTR>&& return LOGICALAND;
225 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
226 <INITIAL,ATTR>. return yytext[0];
228 if (import_stack_ptr)
235 int parser_wrap(void)
246 /* This table MUST be alphabetically sorted on the kw field */
247 static const struct keyword keywords[] = {
252 {"__fastcall", tFASTCALL},
253 {"__int3264", tINT3264},
255 {"__pascal", tPASCAL},
256 {"__stdcall", tSTDCALL},
258 {"_fastcall", tFASTCALL},
259 {"_pascal", tPASCAL},
260 {"_stdcall", tSTDCALL},
261 {"boolean", tBOOLEAN},
266 {"coclass", tCOCLASS},
268 {"cpp_quote", tCPPQUOTE},
269 {"default", tDEFAULT},
270 {"dispinterface", tDISPINTERFACE},
273 {"error_status_t", tERRORSTATUST},
276 {"handle_t", tHANDLET},
279 {"importlib", tIMPORTLIB},
282 {"interface", tINTERFACE},
283 {"library", tLIBRARY},
285 {"methods", tMETHODS},
287 {"namespace", tNAMESPACE},
289 {"properties", tPROPERTIES},
290 {"register", tREGISTER},
296 {"stdcall", tSTDCALL},
299 {"typedef", tTYPEDEF},
301 {"unsigned", tUNSIGNED},
305 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
307 /* keywords only recognized in attribute lists
308 * This table MUST be alphabetically sorted on the kw field
310 static const struct keyword attr_keywords[] =
312 {"aggregatable", tAGGREGATABLE},
313 {"allocate", tALLOCATE},
314 {"annotation", tANNOTATION},
315 {"apartment", tAPARTMENT},
316 {"appobject", tAPPOBJECT},
318 {"async_uuid", tASYNCUUID},
319 {"auto_handle", tAUTOHANDLE},
320 {"bindable", tBINDABLE},
322 {"broadcast", tBROADCAST},
323 {"byte_count", tBYTECOUNT},
324 {"call_as", tCALLAS},
325 {"callback", tCALLBACK},
327 {"comm_status", tCOMMSTATUS},
328 {"context_handle", tCONTEXTHANDLE},
329 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
330 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
331 {"control", tCONTROL},
333 {"defaultbind", tDEFAULTBIND},
334 {"defaultcollelem", tDEFAULTCOLLELEM},
335 {"defaultvalue", tDEFAULTVALUE},
336 {"defaultvtable", tDEFAULTVTABLE},
337 {"disable_consistency_check", tDISABLECONSISTENCYCHECK},
338 {"displaybind", tDISPLAYBIND},
339 {"dllname", tDLLNAME},
341 {"enable_allocate", tENABLEALLOCATE},
343 {"endpoint", tENDPOINT},
345 {"explicit_handle", tEXPLICITHANDLE},
346 {"fault_status", tFAULTSTATUS},
347 {"force_allocate", tFORCEALLOCATE},
350 {"helpcontext", tHELPCONTEXT},
351 {"helpfile", tHELPFILE},
352 {"helpstring", tHELPSTRING},
353 {"helpstringcontext", tHELPSTRINGCONTEXT},
354 {"helpstringdll", tHELPSTRINGDLL},
357 {"idempotent", tIDEMPOTENT},
360 {"immediatebind", tIMMEDIATEBIND},
361 {"implicit_handle", tIMPLICITHANDLE},
363 {"in_line", tIN_LINE},
364 {"input_sync", tINPUTSYNC},
366 {"length_is", tLENGTHIS},
367 {"licensed", tLICENSED},
370 {"message", tMESSAGE},
371 {"neutral", tNEUTRAL},
373 {"nonbrowsable", tNONBROWSABLE},
374 {"noncreatable", tNONCREATABLE},
375 {"nonextensible", tNONEXTENSIBLE},
377 {"notify_flag", tNOTIFYFLAG},
380 {"oleautomation", tOLEAUTOMATION},
381 {"optimize", tOPTIMIZE},
382 {"optional", tOPTIONAL},
384 {"partial_ignore", tPARTIALIGNORE},
385 {"pointer_default", tPOINTERDEFAULT},
387 {"propget", tPROPGET},
388 {"propput", tPROPPUT},
389 {"propputref", tPROPPUTREF},
394 {"readonly", tREADONLY},
396 {"represent_as", tREPRESENTAS},
397 {"requestedit", tREQUESTEDIT},
398 {"restricted", tRESTRICTED},
401 {"size_is", tSIZEIS},
403 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
405 {"switch_is", tSWITCHIS},
406 {"switch_type", tSWITCHTYPE},
407 {"threading", tTHREADING},
408 {"transmit_as", tTRANSMITAS},
409 {"uidefault", tUIDEFAULT},
411 {"user_marshal", tUSERMARSHAL},
412 {"usesgetlasterror", tUSESGETLASTERROR},
414 {"v1_enum", tV1ENUM},
416 {"version", tVERSION},
417 {"vi_progid", tVIPROGID},
418 {"wire_marshal", tWIREMARSHAL},
429 #define KWP(p) ((const struct keyword *)(p))
431 static int kw_cmp_func(const void *s1, const void *s2)
433 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
436 static int kw_token(const char *kw)
438 struct keyword key, *kwp;
440 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
441 if (kwp && (winrt_mode || kwp->token != tNAMESPACE)) {
442 parser_lval.str = xstrdup(kwp->kw);
445 parser_lval.str = xstrdup(kw);
446 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
449 static int attr_token(const char *kw)
451 struct keyword key, *kwp;
453 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
454 sizeof(attr_keywords[0]), kw_cmp_func);
456 parser_lval.str = xstrdup(kwp->kw);
462 static void addcchar(char c)
464 if(cbufidx >= cbufalloc)
467 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
468 if(cbufalloc > 65536)
469 parser_warning("Reallocating string buffer larger than 64kB\n");
471 cbuffer[cbufidx++] = c;
474 static char *get_buffered_cstring(void)
477 return xstrdup(cbuffer);
480 void pop_import(void)
482 int ptr = import_stack_ptr-1;
485 yy_delete_buffer( YY_CURRENT_BUFFER );
486 yy_switch_to_buffer( import_stack[ptr].state );
491 temp_name = import_stack[ptr].temp_name;
492 input_name = import_stack[ptr].input_name;
493 line_number = import_stack[ptr].line_number;
499 struct imports *next;
502 int do_import(char *fname)
506 struct imports *import;
507 int ptr = import_stack_ptr;
510 import = first_import;
511 while (import && strcmp(import->name, fname))
512 import = import->next;
513 if (import) return 0; /* already imported */
515 import = xmalloc(sizeof(struct imports));
516 import->name = xstrdup(fname);
517 import->next = first_import;
518 first_import = import;
520 /* don't search for a file name with a path in the include directories,
521 * for compatibility with MIDL */
522 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
523 path = xstrdup( fname );
524 else if (!(path = wpp_find_include( fname, input_name )))
525 error_loc("Unable to open include file %s\n", fname);
527 import_stack[ptr].temp_name = temp_name;
528 import_stack[ptr].input_name = input_name;
529 import_stack[ptr].line_number = line_number;
534 name = xstrdup( "widl.XXXXXX" );
535 if((fd = mkstemps( name, 0 )) == -1)
536 error("Could not generate a temp name from %s\n", name);
539 if (!(f = fdopen(fd, "wt")))
540 error("Could not open fd %s for writing\n", name);
542 ret = wpp_parse( path, f );
546 if((f = fopen(temp_name, "r")) == NULL)
547 error_loc("Unable to open %s\n", temp_name);
549 import_stack[ptr].state = YY_CURRENT_BUFFER;
550 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
554 void abort_import(void)
558 for (ptr=0; ptr<import_stack_ptr; ptr++)
559 unlink(import_stack[ptr].temp_name);
562 static void warning_disable(int warning)
564 warning_t *warning_entry;
565 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
566 if(warning_entry->num == warning)
568 warning_entry = xmalloc( sizeof(*warning_entry) );
569 warning_entry->num = warning;
570 list_add_tail(disabled_warnings, &warning_entry->entry);
573 static void warning_enable(int warning)
575 warning_t *warning_entry;
576 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
577 if(warning_entry->num == warning)
579 list_remove(&warning_entry->entry);
585 int do_warning(char *toggle, warning_list_t *wnum)
587 warning_t *warning, *next;
589 if(!disabled_warnings)
591 disabled_warnings = xmalloc( sizeof(*disabled_warnings) );
592 list_init( disabled_warnings );
595 if(!strcmp(toggle, "disable"))
596 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
597 warning_disable(warning->num);
598 else if(!strcmp(toggle, "enable"))
599 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
600 warning_enable(warning->num);
604 LIST_FOR_EACH_ENTRY_SAFE(warning, next, wnum, warning_t, entry)
609 int is_warning_enabled(int warning)
611 warning_t *warning_entry;
612 if(!disabled_warnings)
614 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
615 if(warning_entry->num == warning)