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 #define MAX_IMPORT_DEPTH 10
81 YY_BUFFER_STATE state;
85 } import_stack[MAX_IMPORT_DEPTH];
86 int import_stack_ptr = 0;
88 /* converts an integer in string form to an unsigned long and prints an error
90 static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
95 val = strtoul(nptr, endptr, base);
96 if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
97 error_loc("integer constant %s is too large\n", nptr);
101 UUID *parse_uuid(const char *u)
103 UUID* uuid = xmalloc(sizeof(UUID));
105 /* it would be nice to use UuidFromStringA */
106 uuid->Data1 = strtoul(u, NULL, 16);
107 uuid->Data2 = strtoul(u+9, NULL, 16);
108 uuid->Data3 = strtoul(u+14, NULL, 16);
110 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
111 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
112 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
113 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
114 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
115 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
116 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
117 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
124 **************************************************************************
125 * The flexer starts here
126 **************************************************************************
129 <INITIAL>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(PP_PRAGMA);
130 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
135 lineno = (int)strtol(yytext, &cptr, 10);
137 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
138 fname = strchr(cptr, '"');
140 error_loc("Malformed '#...' line-directive; missing filename\n");
142 cptr = strchr(fname, '"');
144 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
146 line_number = lineno - 1; /* We didn't read the newline */
147 input_name = xstrdup(fname);
149 <PP_PRAGMA>midl_echo[^\n]* yyless(9); yy_pop_state(); return tCPPQUOTE;
150 <PP_PRAGMA>winrt[^\n]* {
151 if(import_stack_ptr) {
153 error_loc("winrt IDL file imported in non-winrt mode\n");
155 const char *ptr = yytext+5;
161 if(!strncmp(ptr, "ns_prefix", 9) && (!*(ptr += 9) || isspace(*ptr)))
162 use_abi_namespace = TRUE;
166 <PP_PRAGMA>[^\n]* parser_lval.str = xstrdup(yytext); yy_pop_state(); return aPRAGMA;
167 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
170 parser_lval.str = get_buffered_cstring();
173 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE); cbufidx = 0;
176 parser_lval.str = get_buffered_cstring();
179 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
182 parser_lval.str = get_buffered_cstring();
185 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
186 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
187 <SQUOTE>\\\' addcchar(yytext[1]);
188 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
189 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
190 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
191 <ATTR>\] yy_pop_state(); return ']';
192 <ATTR>{cident} return attr_token(yytext);
194 parser_lval.uuid = parse_uuid(yytext);
197 <INITIAL,ATTR>{hex} {
198 parser_lval.num = xstrtoul(yytext, NULL, 0);
201 <INITIAL,ATTR>{int} {
202 parser_lval.num = xstrtoul(yytext, NULL, 0);
206 parser_lval.dbl = strtod(yytext, NULL);
209 SAFEARRAY{ws}*/\( return tSAFEARRAY;
210 {cident} return kw_token(yytext);
211 <INITIAL,ATTR>\n line_number++;
213 <INITIAL,ATTR>\<\< return SHL;
214 <INITIAL,ATTR>\>\> return SHR;
215 <INITIAL,ATTR>\-\> return MEMBERPTR;
216 <INITIAL,ATTR>== return EQUALITY;
217 <INITIAL,ATTR>!= return INEQUALITY;
218 <INITIAL,ATTR>\>= return GREATEREQUAL;
219 <INITIAL,ATTR>\<= return LESSEQUAL;
220 <INITIAL,ATTR>\|\| return LOGICALOR;
221 <INITIAL,ATTR>&& return LOGICALAND;
222 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
223 <INITIAL,ATTR>. return yytext[0];
225 if (import_stack_ptr)
232 int parser_wrap(void)
243 /* This table MUST be alphabetically sorted on the kw field */
244 static const struct keyword keywords[] = {
249 {"__fastcall", tFASTCALL},
250 {"__int3264", tINT3264},
252 {"__pascal", tPASCAL},
253 {"__stdcall", tSTDCALL},
255 {"_fastcall", tFASTCALL},
256 {"_pascal", tPASCAL},
257 {"_stdcall", tSTDCALL},
258 {"boolean", tBOOLEAN},
263 {"coclass", tCOCLASS},
265 {"cpp_quote", tCPPQUOTE},
266 {"default", tDEFAULT},
267 {"dispinterface", tDISPINTERFACE},
270 {"error_status_t", tERRORSTATUST},
273 {"handle_t", tHANDLET},
276 {"importlib", tIMPORTLIB},
279 {"interface", tINTERFACE},
280 {"library", tLIBRARY},
282 {"methods", tMETHODS},
284 {"namespace", tNAMESPACE},
286 {"properties", tPROPERTIES},
287 {"register", tREGISTER},
293 {"stdcall", tSTDCALL},
296 {"typedef", tTYPEDEF},
298 {"unsigned", tUNSIGNED},
302 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
304 /* keywords only recognized in attribute lists
305 * This table MUST be alphabetically sorted on the kw field
307 static const struct keyword attr_keywords[] =
309 {"aggregatable", tAGGREGATABLE},
310 {"allocate", tALLOCATE},
311 {"annotation", tANNOTATION},
312 {"apartment", tAPARTMENT},
313 {"appobject", tAPPOBJECT},
315 {"async_uuid", tASYNCUUID},
316 {"auto_handle", tAUTOHANDLE},
317 {"bindable", tBINDABLE},
319 {"broadcast", tBROADCAST},
320 {"byte_count", tBYTECOUNT},
321 {"call_as", tCALLAS},
322 {"callback", tCALLBACK},
324 {"comm_status", tCOMMSTATUS},
325 {"context_handle", tCONTEXTHANDLE},
326 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
327 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
328 {"control", tCONTROL},
330 {"defaultbind", tDEFAULTBIND},
331 {"defaultcollelem", tDEFAULTCOLLELEM},
332 {"defaultvalue", tDEFAULTVALUE},
333 {"defaultvtable", tDEFAULTVTABLE},
334 {"disable_consistency_check", tDISABLECONSISTENCYCHECK},
335 {"displaybind", tDISPLAYBIND},
336 {"dllname", tDLLNAME},
338 {"enable_allocate", tENABLEALLOCATE},
340 {"endpoint", tENDPOINT},
342 {"explicit_handle", tEXPLICITHANDLE},
343 {"fault_status", tFAULTSTATUS},
344 {"force_allocate", tFORCEALLOCATE},
347 {"helpcontext", tHELPCONTEXT},
348 {"helpfile", tHELPFILE},
349 {"helpstring", tHELPSTRING},
350 {"helpstringcontext", tHELPSTRINGCONTEXT},
351 {"helpstringdll", tHELPSTRINGDLL},
354 {"idempotent", tIDEMPOTENT},
357 {"immediatebind", tIMMEDIATEBIND},
358 {"implicit_handle", tIMPLICITHANDLE},
360 {"in_line", tIN_LINE},
361 {"input_sync", tINPUTSYNC},
363 {"length_is", tLENGTHIS},
364 {"licensed", tLICENSED},
367 {"message", tMESSAGE},
368 {"neutral", tNEUTRAL},
370 {"nonbrowsable", tNONBROWSABLE},
371 {"noncreatable", tNONCREATABLE},
372 {"nonextensible", tNONEXTENSIBLE},
374 {"notify_flag", tNOTIFYFLAG},
377 {"oleautomation", tOLEAUTOMATION},
378 {"optimize", tOPTIMIZE},
379 {"optional", tOPTIONAL},
381 {"partial_ignore", tPARTIALIGNORE},
382 {"pointer_default", tPOINTERDEFAULT},
384 {"propget", tPROPGET},
385 {"propput", tPROPPUT},
386 {"propputref", tPROPPUTREF},
391 {"readonly", tREADONLY},
393 {"represent_as", tREPRESENTAS},
394 {"requestedit", tREQUESTEDIT},
395 {"restricted", tRESTRICTED},
398 {"size_is", tSIZEIS},
400 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
402 {"switch_is", tSWITCHIS},
403 {"switch_type", tSWITCHTYPE},
404 {"threading", tTHREADING},
405 {"transmit_as", tTRANSMITAS},
406 {"uidefault", tUIDEFAULT},
408 {"user_marshal", tUSERMARSHAL},
409 {"usesgetlasterror", tUSESGETLASTERROR},
411 {"v1_enum", tV1ENUM},
413 {"version", tVERSION},
414 {"vi_progid", tVIPROGID},
415 {"wire_marshal", tWIREMARSHAL},
426 #define KWP(p) ((const struct keyword *)(p))
428 static int kw_cmp_func(const void *s1, const void *s2)
430 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
433 static int kw_token(const char *kw)
435 struct keyword key, *kwp;
437 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
438 if (kwp && (winrt_mode || kwp->token != tNAMESPACE)) {
439 parser_lval.str = xstrdup(kwp->kw);
442 parser_lval.str = xstrdup(kw);
443 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
446 static int attr_token(const char *kw)
448 struct keyword key, *kwp;
450 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
451 sizeof(attr_keywords[0]), kw_cmp_func);
453 parser_lval.str = xstrdup(kwp->kw);
459 static void addcchar(char c)
461 if(cbufidx >= cbufalloc)
464 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
465 if(cbufalloc > 65536)
466 parser_warning("Reallocating string buffer larger than 64kB\n");
468 cbuffer[cbufidx++] = c;
471 static char *get_buffered_cstring(void)
474 return xstrdup(cbuffer);
477 void pop_import(void)
479 int ptr = import_stack_ptr-1;
482 yy_delete_buffer( YY_CURRENT_BUFFER );
483 yy_switch_to_buffer( import_stack[ptr].state );
488 temp_name = import_stack[ptr].temp_name;
489 input_name = import_stack[ptr].input_name;
490 line_number = import_stack[ptr].line_number;
496 struct imports *next;
499 int do_import(char *fname)
503 struct imports *import;
504 int ptr = import_stack_ptr;
507 import = first_import;
508 while (import && strcmp(import->name, fname))
509 import = import->next;
510 if (import) return 0; /* already imported */
512 import = xmalloc(sizeof(struct imports));
513 import->name = xstrdup(fname);
514 import->next = first_import;
515 first_import = import;
517 /* don't search for a file name with a path in the include directories,
518 * for compatibility with MIDL */
519 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
520 path = xstrdup( fname );
521 else if (!(path = wpp_find_include( fname, input_name )))
522 error_loc("Unable to open include file %s\n", fname);
524 import_stack[ptr].temp_name = temp_name;
525 import_stack[ptr].input_name = input_name;
526 import_stack[ptr].line_number = line_number;
531 name = xstrdup( "widl.XXXXXX" );
532 if((fd = mkstemps( name, 0 )) == -1)
533 error("Could not generate a temp name from %s\n", name);
536 if (!(f = fdopen(fd, "wt")))
537 error("Could not open fd %s for writing\n", name);
539 ret = wpp_parse( path, f );
543 if((f = fopen(temp_name, "r")) == NULL)
544 error_loc("Unable to open %s\n", temp_name);
546 import_stack[ptr].state = YY_CURRENT_BUFFER;
547 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
551 void abort_import(void)
555 for (ptr=0; ptr<import_stack_ptr; ptr++)
556 unlink(import_stack[ptr].temp_name);