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]+)*
54 #define YY_NO_UNISTD_H
59 #include "wpp_private.h"
61 #include "parser.tab.h"
63 static void addcchar(char c);
64 static char *get_buffered_cstring(void);
68 static int cbufalloc = 0;
70 static int kw_token(const char *kw);
71 static int attr_token(const char *kw);
73 static void switch_to_acf(void);
75 static warning_list_t *disabled_warnings = NULL;
77 #define MAX_IMPORT_DEPTH 20
79 YY_BUFFER_STATE state;
83 } import_stack[MAX_IMPORT_DEPTH];
84 int import_stack_ptr = 0;
86 /* converts an integer in string form to an unsigned long and prints an error
88 static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
93 val = strtoul(nptr, endptr, base);
94 if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
95 error_loc("integer constant %s is too large\n", nptr);
99 struct uuid *parse_uuid(const char *u)
101 struct uuid* uuid = xmalloc(sizeof(*uuid));
103 /* it would be nice to use UuidFromStringA */
104 uuid->Data1 = strtoul(u, NULL, 16);
105 uuid->Data2 = strtoul(u+9, NULL, 16);
106 uuid->Data3 = strtoul(u+14, NULL, 16);
108 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
109 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
110 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
111 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
112 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
113 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
114 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
115 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
122 **************************************************************************
123 * The flexer starts here
124 **************************************************************************
127 <INITIAL>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(PP_PRAGMA);
128 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
133 lineno = (int)strtol(yytext, &cptr, 10);
135 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
136 fname = strchr(cptr, '"');
138 error_loc("Malformed '#...' line-directive; missing filename\n");
140 cptr = strchr(fname, '"');
142 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
144 line_number = lineno - 1; /* We didn't read the newline */
145 input_name = xstrdup(fname);
147 <PP_PRAGMA>midl_echo[^\n]* yyless(9); yy_pop_state(); return tCPPQUOTE;
148 <PP_PRAGMA>winrt[^\n]* {
149 if(import_stack_ptr) {
151 error_loc("winrt IDL file imported in non-winrt mode\n");
153 const char *ptr = yytext+5;
159 if(!strncmp(ptr, "ns_prefix", 9) && (!*(ptr += 9) || isspace(*ptr)))
160 use_abi_namespace = TRUE;
164 <PP_PRAGMA>[^\n]* parser_lval.str = xstrdup(yytext); yy_pop_state(); return aPRAGMA;
165 <INITIAL>^{ws}*midl_pragma{ws}+warning return tPRAGMA_WARNING;
166 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
169 parser_lval.str = get_buffered_cstring();
172 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE); cbufidx = 0;
175 parser_lval.str = get_buffered_cstring();
178 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
181 parser_lval.str = get_buffered_cstring();
184 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
185 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
186 <SQUOTE>\\\' addcchar(yytext[1]);
187 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
188 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
189 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
190 <ATTR>\] yy_pop_state(); return ']';
191 <ATTR>{cident} return attr_token(yytext);
193 parser_lval.uuid = parse_uuid(yytext);
196 <INITIAL,ATTR>{hex} {
197 parser_lval.num = xstrtoul(yytext, NULL, 0);
200 <INITIAL,ATTR>{int} {
201 parser_lval.num = xstrtoul(yytext, NULL, 0);
205 parser_lval.dbl = strtod(yytext, NULL);
208 SAFEARRAY{ws}*/\( return tSAFEARRAY;
209 {cident} return kw_token(yytext);
210 <INITIAL,ATTR>\n line_number++;
212 <INITIAL,ATTR>\<\< return SHL;
213 <INITIAL,ATTR>\>\> return SHR;
214 <INITIAL,ATTR>\-\> return MEMBERPTR;
215 <INITIAL,ATTR>== return EQUALITY;
216 <INITIAL,ATTR>!= return INEQUALITY;
217 <INITIAL,ATTR>\>= return GREATEREQUAL;
218 <INITIAL,ATTR>\<= return LESSEQUAL;
219 <INITIAL,ATTR>\|\| return LOGICALOR;
220 <INITIAL,ATTR>&& return LOGICALAND;
221 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
222 <INITIAL,ATTR>. return yytext[0];
224 if (import_stack_ptr)
236 int parser_wrap(void)
248 /* This table MUST be alphabetically sorted on the kw field */
249 static const struct keyword keywords[] = {
250 {"FALSE", tFALSE, 0},
253 {"__cdecl", tCDECL, 0},
254 {"__fastcall", tFASTCALL, 0},
255 {"__int32", tINT32, 0},
256 {"__int3264", tINT3264, 0},
257 {"__int64", tINT64, 0},
258 {"__pascal", tPASCAL, 0},
259 {"__stdcall", tSTDCALL, 0},
260 {"_cdecl", tCDECL, 0},
261 {"_fastcall", tFASTCALL, 0},
262 {"_pascal", tPASCAL, 0},
263 {"_stdcall", tSTDCALL, 0},
264 {"apicontract", tAPICONTRACT, 1},
265 {"boolean", tBOOLEAN, 0},
268 {"cdecl", tCDECL, 0},
270 {"coclass", tCOCLASS, 0},
271 {"const", tCONST, 0},
272 {"cpp_quote", tCPPQUOTE, 0},
273 {"declare", tDECLARE, 1},
274 {"default", tDEFAULT, 0},
275 {"delegate", tDELEGATE, 1},
276 {"dispinterface", tDISPINTERFACE, 0},
277 {"double", tDOUBLE, 0},
279 {"error_status_t", tERRORSTATUST, 0},
280 {"extern", tEXTERN, 0},
281 {"float", tFLOAT, 0},
282 {"handle_t", tHANDLET, 0},
283 {"hyper", tHYPER, 0},
284 {"import", tIMPORT, 0},
285 {"importlib", tIMPORTLIB, 0},
286 {"inline", tINLINE, 0},
288 {"interface", tINTERFACE, 0},
289 {"library", tLIBRARY, 0},
291 {"methods", tMETHODS, 0},
292 {"module", tMODULE, 0},
293 {"namespace", tNAMESPACE, 1},
294 {"pascal", tPASCAL, 0},
295 {"properties", tPROPERTIES, 0},
296 {"register", tREGISTER, 0},
297 {"requires", tREQUIRES, 1},
298 {"runtimeclass", tRUNTIMECLASS, 1},
299 {"short", tSHORT, 0},
300 {"signed", tSIGNED, 0},
301 {"sizeof", tSIZEOF, 0},
302 {"small", tSMALL, 0},
303 {"static", tSTATIC, 0},
304 {"stdcall", tSTDCALL, 0},
305 {"struct", tSTRUCT, 0},
306 {"switch", tSWITCH, 0},
307 {"typedef", tTYPEDEF, 0},
308 {"union", tUNION, 0},
309 {"unsigned", tUNSIGNED, 0},
311 {"wchar_t", tWCHAR, 0},
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 {"activatable", tACTIVATABLE, 1},
321 {"aggregatable", tAGGREGATABLE, 0},
322 {"agile", tAGILE, 1},
323 {"all_nodes", tALLNODES, 0},
324 {"allocate", tALLOCATE, 0},
325 {"annotation", tANNOTATION, 0},
326 {"apartment", tAPARTMENT, 0},
327 {"appobject", tAPPOBJECT, 0},
328 {"async", tASYNC, 0},
329 {"async_uuid", tASYNCUUID, 0},
330 {"auto_handle", tAUTOHANDLE, 0},
331 {"bindable", tBINDABLE, 0},
333 {"broadcast", tBROADCAST, 0},
334 {"byte_count", tBYTECOUNT, 0},
335 {"call_as", tCALLAS, 0},
336 {"callback", tCALLBACK, 0},
338 {"comm_status", tCOMMSTATUS, 0},
339 {"context_handle", tCONTEXTHANDLE, 0},
340 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE, 0},
341 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE, 0},
342 {"contract", tCONTRACT, 1},
343 {"contractversion", tCONTRACTVERSION, 1},
344 {"control", tCONTROL, 0},
345 {"custom", tCUSTOM, 0},
346 {"decode", tDECODE, 0},
347 {"defaultbind", tDEFAULTBIND, 0},
348 {"defaultcollelem", tDEFAULTCOLLELEM, 0},
349 {"defaultvalue", tDEFAULTVALUE, 0},
350 {"defaultvtable", tDEFAULTVTABLE, 0},
351 {"disable_consistency_check", tDISABLECONSISTENCYCHECK, 0},
352 {"displaybind", tDISPLAYBIND, 0},
353 {"dllname", tDLLNAME, 0},
354 {"dont_free", tDONTFREE, 0},
356 {"enable_allocate", tENABLEALLOCATE, 0},
357 {"encode", tENCODE, 0},
358 {"endpoint", tENDPOINT, 0},
359 {"entry", tENTRY, 0},
360 {"eventadd", tEVENTADD, 1},
361 {"eventremove", tEVENTREMOVE, 1},
362 {"exclusiveto", tEXCLUSIVETO, 1},
363 {"explicit_handle", tEXPLICITHANDLE, 0},
364 {"fault_status", tFAULTSTATUS, 0},
365 {"flags", tFLAGS, 1},
366 {"force_allocate", tFORCEALLOCATE, 0},
368 {"handle", tHANDLE, 0},
369 {"helpcontext", tHELPCONTEXT, 0},
370 {"helpfile", tHELPFILE, 0},
371 {"helpstring", tHELPSTRING, 0},
372 {"helpstringcontext", tHELPSTRINGCONTEXT, 0},
373 {"helpstringdll", tHELPSTRINGDLL, 0},
374 {"hidden", tHIDDEN, 0},
376 {"idempotent", tIDEMPOTENT, 0},
377 {"ignore", tIGNORE, 0},
378 {"iid_is", tIIDIS, 0},
379 {"immediatebind", tIMMEDIATEBIND, 0},
380 {"implicit_handle", tIMPLICITHANDLE, 0},
382 {"in_line", tIN_LINE, 0},
383 {"input_sync", tINPUTSYNC, 0},
385 {"length_is", tLENGTHIS, 0},
386 {"licensed", tLICENSED, 0},
387 {"local", tLOCAL, 0},
388 {"marshaling_behavior", tMARSHALINGBEHAVIOR, 1},
389 {"maybe", tMAYBE, 0},
390 {"message", tMESSAGE, 0},
392 {"neutral", tNEUTRAL, 0},
393 {"nocode", tNOCODE, 0},
394 {"nonbrowsable", tNONBROWSABLE, 0},
395 {"noncreatable", tNONCREATABLE, 0},
397 {"nonextensible", tNONEXTENSIBLE, 0},
398 {"notify", tNOTIFY, 0},
399 {"notify_flag", tNOTIFYFLAG, 0},
400 {"object", tOBJECT, 0},
402 {"oleautomation", tOLEAUTOMATION, 0},
403 {"optimize", tOPTIMIZE, 0},
404 {"optional", tOPTIONAL, 0},
406 {"overload", tOVERLOAD, 0},
407 {"partial_ignore", tPARTIALIGNORE, 0},
408 {"pointer_default", tPOINTERDEFAULT, 0},
409 {"progid", tPROGID, 0},
410 {"propget", tPROPGET, 0},
411 {"propput", tPROPPUT, 0},
412 {"propputref", tPROPPUTREF, 0},
413 {"proxy", tPROXY, 0},
415 {"public", tPUBLIC, 0},
416 {"range", tRANGE, 0},
417 {"readonly", tREADONLY, 0},
419 {"represent_as", tREPRESENTAS, 0},
420 {"requestedit", tREQUESTEDIT, 0},
421 {"restricted", tRESTRICTED, 0},
422 {"retval", tRETVAL, 0},
423 {"single", tSINGLE, 0},
424 {"single_node", tSINGLENODE, 0},
425 {"size_is", tSIZEIS, 0},
426 {"source", tSOURCE, 0},
427 {"standard", tSTANDARD, 1},
428 {"static", tSTATIC, 1},
429 {"strict_context_handle", tSTRICTCONTEXTHANDLE, 0},
430 {"string", tSTRING, 0},
431 {"switch_is", tSWITCHIS, 0},
432 {"switch_type", tSWITCHTYPE, 0},
433 {"threading", tTHREADING, 0},
434 {"transmit_as", tTRANSMITAS, 0},
435 {"uidefault", tUIDEFAULT, 0},
436 {"unique", tUNIQUE, 0},
437 {"user_marshal", tUSERMARSHAL, 0},
438 {"usesgetlasterror", tUSESGETLASTERROR, 0},
440 {"v1_enum", tV1ENUM, 0},
441 {"vararg", tVARARG, 0},
442 {"version", tVERSION, 0},
443 {"vi_progid", tVIPROGID, 0},
444 {"wire_marshal", tWIREMARSHAL, 0},
454 #define KWP(p) ((const struct keyword *)(p))
456 static int kw_cmp_func(const void *s1, const void *s2)
458 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
461 static int kw_token(const char *kw)
463 struct keyword key, *kwp;
465 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
466 if (kwp && (!kwp->winrt_only || winrt_mode)) {
467 parser_lval.str = xstrdup(kwp->kw);
470 parser_lval.str = xstrdup(kw);
471 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
474 static int attr_token(const char *kw)
476 struct keyword key, *kwp;
478 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
479 sizeof(attr_keywords[0]), kw_cmp_func);
480 if (kwp && (!kwp->winrt_only || winrt_mode)) {
481 parser_lval.str = xstrdup(kwp->kw);
487 static void addcchar(char c)
489 if(cbufidx >= cbufalloc)
492 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
493 if(cbufalloc > 65536)
494 parser_warning("Reallocating string buffer larger than 64kB\n");
496 cbuffer[cbufidx++] = c;
499 static char *get_buffered_cstring(void)
502 return xstrdup(cbuffer);
505 void pop_import(void)
507 int ptr = import_stack_ptr-1;
510 yy_delete_buffer( YY_CURRENT_BUFFER );
511 yy_switch_to_buffer( import_stack[ptr].state );
512 input_name = import_stack[ptr].input_name;
513 line_number = import_stack[ptr].line_number;
519 struct imports *next;
522 int do_import(char *fname)
526 struct imports *import;
527 int ptr = import_stack_ptr;
530 import = first_import;
531 while (import && strcmp(import->name, fname))
532 import = import->next;
533 if (import) return 0; /* already imported */
535 import = xmalloc(sizeof(struct imports));
536 import->name = xstrdup(fname);
537 import->next = first_import;
538 first_import = import;
540 /* don't search for a file name with a path in the include directories,
541 * for compatibility with MIDL */
542 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
543 path = xstrdup( fname );
544 else if (!(path = wpp_find_include( fname, input_name )))
545 error_loc("Unable to open include file %s\n", fname);
547 if (import_stack_ptr == MAX_IMPORT_DEPTH)
548 error_loc("Exceeded max import depth\n");
550 import_stack[ptr].input_name = input_name;
551 import_stack[ptr].line_number = line_number;
556 name = make_temp_file( "widl-pp", NULL );
557 if (!(f = fopen(name, "wt")))
558 error("Could not open fd %s for writing\n", name);
560 ret = wpp_parse( path, f );
564 if((f = fopen(name, "r")) == NULL)
565 error_loc("Unable to open %s\n", name);
567 import_stack[ptr].state = YY_CURRENT_BUFFER;
568 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
572 void abort_import(void)
576 for (ptr=0; ptr<import_stack_ptr; ptr++)
577 unlink(import_stack[ptr].temp_name);
580 static void switch_to_acf(void)
582 int ptr = import_stack_ptr;
587 assert(import_stack_ptr == 0);
589 input_name = acf_name;
593 name = make_temp_file( "widl-acf", NULL );
594 if (!(f = fopen(name, "wt")))
595 error("Could not open fd %s for writing\n", name);
597 ret = wpp_parse(input_name, f);
601 if((f = fopen(name, "r")) == NULL)
602 error_loc("Unable to open %s\n", name);
604 import_stack[ptr].state = YY_CURRENT_BUFFER;
605 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
608 static void warning_disable(int warning)
610 warning_t *warning_entry;
611 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
612 if(warning_entry->num == warning)
614 warning_entry = xmalloc( sizeof(*warning_entry) );
615 warning_entry->num = warning;
616 list_add_tail(disabled_warnings, &warning_entry->entry);
619 static void warning_enable(int warning)
621 warning_t *warning_entry;
622 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
623 if(warning_entry->num == warning)
625 list_remove(&warning_entry->entry);
631 int do_warning(const char *toggle, warning_list_t *wnum)
633 warning_t *warning, *next;
635 if(!disabled_warnings)
637 disabled_warnings = xmalloc( sizeof(*disabled_warnings) );
638 list_init( disabled_warnings );
641 if(!strcmp(toggle, "disable"))
642 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
643 warning_disable(warning->num);
644 else if(!strcmp(toggle, "enable") || !strcmp(toggle, "default"))
645 LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
646 warning_enable(warning->num);
650 LIST_FOR_EACH_ENTRY_SAFE(warning, next, wnum, warning_t, entry)
655 int is_warning_enabled(int warning)
657 warning_t *warning_entry;
658 if(!disabled_warnings)
660 LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
661 if(warning_entry->num == warning)