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>[^\n]* parser_lval.str = xstrdup(yytext); yy_pop_state(); return aPRAGMA;
151 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
154 parser_lval.str = get_buffered_cstring();
157 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE); cbufidx = 0;
160 parser_lval.str = get_buffered_cstring();
163 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
166 parser_lval.str = get_buffered_cstring();
169 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
170 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
171 <SQUOTE>\\\' addcchar(yytext[1]);
172 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
173 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
174 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
175 <ATTR>\] yy_pop_state(); return ']';
176 <ATTR>{cident} return attr_token(yytext);
178 parser_lval.uuid = parse_uuid(yytext);
181 <INITIAL,ATTR>{hex} {
182 parser_lval.num = xstrtoul(yytext, NULL, 0);
185 <INITIAL,ATTR>{int} {
186 parser_lval.num = xstrtoul(yytext, NULL, 0);
190 parser_lval.dbl = strtod(yytext, NULL);
193 SAFEARRAY{ws}*/\( return tSAFEARRAY;
194 {cident} return kw_token(yytext);
195 <INITIAL,ATTR>\n line_number++;
197 <INITIAL,ATTR>\<\< return SHL;
198 <INITIAL,ATTR>\>\> return SHR;
199 <INITIAL,ATTR>\-\> return MEMBERPTR;
200 <INITIAL,ATTR>== return EQUALITY;
201 <INITIAL,ATTR>!= return INEQUALITY;
202 <INITIAL,ATTR>\>= return GREATEREQUAL;
203 <INITIAL,ATTR>\<= return LESSEQUAL;
204 <INITIAL,ATTR>\|\| return LOGICALOR;
205 <INITIAL,ATTR>&& return LOGICALAND;
206 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
207 <INITIAL,ATTR>. return yytext[0];
209 if (import_stack_ptr)
216 int parser_wrap(void)
227 /* This table MUST be alphabetically sorted on the kw field */
228 static const struct keyword keywords[] = {
233 {"__fastcall", tFASTCALL},
234 {"__int3264", tINT3264},
236 {"__pascal", tPASCAL},
237 {"__stdcall", tSTDCALL},
239 {"_fastcall", tFASTCALL},
240 {"_pascal", tPASCAL},
241 {"_stdcall", tSTDCALL},
242 {"boolean", tBOOLEAN},
247 {"coclass", tCOCLASS},
249 {"cpp_quote", tCPPQUOTE},
250 {"default", tDEFAULT},
251 {"dispinterface", tDISPINTERFACE},
254 {"error_status_t", tERRORSTATUST},
257 {"handle_t", tHANDLET},
260 {"importlib", tIMPORTLIB},
263 {"interface", tINTERFACE},
264 {"library", tLIBRARY},
266 {"methods", tMETHODS},
268 {"namespace", tNAMESPACE},
270 {"properties", tPROPERTIES},
271 {"register", tREGISTER},
277 {"stdcall", tSTDCALL},
280 {"typedef", tTYPEDEF},
282 {"unsigned", tUNSIGNED},
286 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
288 /* keywords only recognized in attribute lists
289 * This table MUST be alphabetically sorted on the kw field
291 static const struct keyword attr_keywords[] =
293 {"aggregatable", tAGGREGATABLE},
294 {"allocate", tALLOCATE},
295 {"annotation", tANNOTATION},
296 {"apartment", tAPARTMENT},
297 {"appobject", tAPPOBJECT},
299 {"async_uuid", tASYNCUUID},
300 {"auto_handle", tAUTOHANDLE},
301 {"bindable", tBINDABLE},
303 {"broadcast", tBROADCAST},
304 {"byte_count", tBYTECOUNT},
305 {"call_as", tCALLAS},
306 {"callback", tCALLBACK},
308 {"comm_status", tCOMMSTATUS},
309 {"context_handle", tCONTEXTHANDLE},
310 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
311 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
312 {"control", tCONTROL},
314 {"defaultbind", tDEFAULTBIND},
315 {"defaultcollelem", tDEFAULTCOLLELEM},
316 {"defaultvalue", tDEFAULTVALUE},
317 {"defaultvtable", tDEFAULTVTABLE},
318 {"disable_consistency_check", tDISABLECONSISTENCYCHECK},
319 {"displaybind", tDISPLAYBIND},
320 {"dllname", tDLLNAME},
322 {"enable_allocate", tENABLEALLOCATE},
324 {"endpoint", tENDPOINT},
326 {"explicit_handle", tEXPLICITHANDLE},
327 {"fault_status", tFAULTSTATUS},
328 {"force_allocate", tFORCEALLOCATE},
331 {"helpcontext", tHELPCONTEXT},
332 {"helpfile", tHELPFILE},
333 {"helpstring", tHELPSTRING},
334 {"helpstringcontext", tHELPSTRINGCONTEXT},
335 {"helpstringdll", tHELPSTRINGDLL},
338 {"idempotent", tIDEMPOTENT},
341 {"immediatebind", tIMMEDIATEBIND},
342 {"implicit_handle", tIMPLICITHANDLE},
344 {"in_line", tIN_LINE},
345 {"input_sync", tINPUTSYNC},
347 {"length_is", tLENGTHIS},
348 {"licensed", tLICENSED},
351 {"message", tMESSAGE},
352 {"neutral", tNEUTRAL},
354 {"nonbrowsable", tNONBROWSABLE},
355 {"noncreatable", tNONCREATABLE},
356 {"nonextensible", tNONEXTENSIBLE},
358 {"notify_flag", tNOTIFYFLAG},
361 {"oleautomation", tOLEAUTOMATION},
362 {"optimize", tOPTIMIZE},
363 {"optional", tOPTIONAL},
365 {"partial_ignore", tPARTIALIGNORE},
366 {"pointer_default", tPOINTERDEFAULT},
368 {"propget", tPROPGET},
369 {"propput", tPROPPUT},
370 {"propputref", tPROPPUTREF},
375 {"readonly", tREADONLY},
377 {"represent_as", tREPRESENTAS},
378 {"requestedit", tREQUESTEDIT},
379 {"restricted", tRESTRICTED},
382 {"size_is", tSIZEIS},
384 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
386 {"switch_is", tSWITCHIS},
387 {"switch_type", tSWITCHTYPE},
388 {"threading", tTHREADING},
389 {"transmit_as", tTRANSMITAS},
390 {"uidefault", tUIDEFAULT},
392 {"user_marshal", tUSERMARSHAL},
393 {"usesgetlasterror", tUSESGETLASTERROR},
395 {"v1_enum", tV1ENUM},
397 {"version", tVERSION},
398 {"vi_progid", tVIPROGID},
399 {"wire_marshal", tWIREMARSHAL},
410 #define KWP(p) ((const struct keyword *)(p))
412 static int kw_cmp_func(const void *s1, const void *s2)
414 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
417 static int kw_token(const char *kw)
419 struct keyword key, *kwp;
421 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
422 if (kwp && (do_rt_extension || kwp->token != tNAMESPACE)) {
423 parser_lval.str = xstrdup(kwp->kw);
426 parser_lval.str = xstrdup(kw);
427 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
430 static int attr_token(const char *kw)
432 struct keyword key, *kwp;
434 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
435 sizeof(attr_keywords[0]), kw_cmp_func);
437 parser_lval.str = xstrdup(kwp->kw);
443 static void addcchar(char c)
445 if(cbufidx >= cbufalloc)
448 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
449 if(cbufalloc > 65536)
450 parser_warning("Reallocating string buffer larger than 64kB\n");
452 cbuffer[cbufidx++] = c;
455 static char *get_buffered_cstring(void)
458 return xstrdup(cbuffer);
461 void pop_import(void)
463 int ptr = import_stack_ptr-1;
466 yy_delete_buffer( YY_CURRENT_BUFFER );
467 yy_switch_to_buffer( import_stack[ptr].state );
472 temp_name = import_stack[ptr].temp_name;
473 input_name = import_stack[ptr].input_name;
474 line_number = import_stack[ptr].line_number;
480 struct imports *next;
483 int do_import(char *fname)
487 struct imports *import;
488 int ptr = import_stack_ptr;
491 import = first_import;
492 while (import && strcmp(import->name, fname))
493 import = import->next;
494 if (import) return 0; /* already imported */
496 import = xmalloc(sizeof(struct imports));
497 import->name = xstrdup(fname);
498 import->next = first_import;
499 first_import = import;
501 /* don't search for a file name with a path in the include directories,
502 * for compatibility with MIDL */
503 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
504 path = xstrdup( fname );
505 else if (!(path = wpp_find_include( fname, input_name )))
506 error_loc("Unable to open include file %s\n", fname);
508 import_stack[ptr].temp_name = temp_name;
509 import_stack[ptr].input_name = input_name;
510 import_stack[ptr].line_number = line_number;
515 name = xstrdup( "widl.XXXXXX" );
516 if((fd = mkstemps( name, 0 )) == -1)
517 error("Could not generate a temp name from %s\n", name);
520 if (!(f = fdopen(fd, "wt")))
521 error("Could not open fd %s for writing\n", name);
523 ret = wpp_parse( path, f );
527 if((f = fopen(temp_name, "r")) == NULL)
528 error_loc("Unable to open %s\n", temp_name);
530 import_stack[ptr].state = YY_CURRENT_BUFFER;
531 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
535 void abort_import(void)
539 for (ptr=0; ptr<import_stack_ptr; ptr++)
540 unlink(import_stack[ptr].temp_name);