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]+)*
45 #include "wine/port.h"
58 #define YY_NO_UNISTD_H
66 #include "parser.tab.h"
68 static void addcchar(char c);
69 static char *get_buffered_cstring(void);
73 static int cbufalloc = 0;
75 static int kw_token(const char *kw);
76 static int attr_token(const char *kw);
78 #define MAX_IMPORT_DEPTH 10
80 YY_BUFFER_STATE state;
84 } import_stack[MAX_IMPORT_DEPTH];
85 int import_stack_ptr = 0;
87 /* converts an integer in string form to an unsigned long and prints an error
89 static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
94 val = strtoul(nptr, endptr, base);
95 if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
96 error_loc("integer constant %s is too large\n", nptr);
100 UUID *parse_uuid(const char *u)
102 UUID* uuid = xmalloc(sizeof(UUID));
104 /* it would be nice to use UuidFromStringA */
105 uuid->Data1 = strtoul(u, NULL, 16);
106 uuid->Data2 = strtoul(u+9, NULL, 16);
107 uuid->Data3 = strtoul(u+14, NULL, 16);
109 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
110 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
111 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
112 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
113 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
114 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
115 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
116 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
123 **************************************************************************
124 * The flexer starts here
125 **************************************************************************
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 */
146 input_name = xstrdup(fname);
148 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
151 parser_lval.str = get_buffered_cstring();
154 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE); cbufidx = 0;
157 parser_lval.str = get_buffered_cstring();
160 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
163 parser_lval.str = get_buffered_cstring();
166 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
167 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
168 <SQUOTE>\\\' addcchar(yytext[1]);
169 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
170 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
171 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
172 <ATTR>\] yy_pop_state(); return ']';
173 <ATTR>{cident} return attr_token(yytext);
175 parser_lval.uuid = parse_uuid(yytext);
178 <INITIAL,ATTR>{hex} {
179 parser_lval.num = xstrtoul(yytext, NULL, 0);
182 <INITIAL,ATTR>{int} {
183 parser_lval.num = xstrtoul(yytext, NULL, 0);
187 parser_lval.dbl = strtod(yytext, NULL);
190 SAFEARRAY{ws}*/\( return tSAFEARRAY;
191 {cident} return kw_token(yytext);
192 <INITIAL,ATTR>\n line_number++;
194 <INITIAL,ATTR>\<\< return SHL;
195 <INITIAL,ATTR>\>\> return SHR;
196 <INITIAL,ATTR>\-\> return MEMBERPTR;
197 <INITIAL,ATTR>== return EQUALITY;
198 <INITIAL,ATTR>!= return INEQUALITY;
199 <INITIAL,ATTR>\>= return GREATEREQUAL;
200 <INITIAL,ATTR>\<= return LESSEQUAL;
201 <INITIAL,ATTR>\|\| return LOGICALOR;
202 <INITIAL,ATTR>&& return LOGICALAND;
203 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
204 <INITIAL,ATTR>. return yytext[0];
206 if (import_stack_ptr)
213 int parser_wrap(void)
224 /* This table MUST be alphabetically sorted on the kw field */
225 static const struct keyword keywords[] = {
230 {"__fastcall", tFASTCALL},
231 {"__int3264", tINT3264},
233 {"__pascal", tPASCAL},
234 {"__stdcall", tSTDCALL},
236 {"_fastcall", tFASTCALL},
237 {"_pascal", tPASCAL},
238 {"_stdcall", tSTDCALL},
239 {"boolean", tBOOLEAN},
244 {"coclass", tCOCLASS},
246 {"cpp_quote", tCPPQUOTE},
247 {"default", tDEFAULT},
248 {"dispinterface", tDISPINTERFACE},
251 {"error_status_t", tERRORSTATUST},
254 {"handle_t", tHANDLET},
257 {"importlib", tIMPORTLIB},
260 {"interface", tINTERFACE},
261 {"library", tLIBRARY},
263 {"methods", tMETHODS},
266 {"properties", tPROPERTIES},
267 {"register", tREGISTER},
273 {"stdcall", tSTDCALL},
276 {"typedef", tTYPEDEF},
278 {"unsigned", tUNSIGNED},
282 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
284 /* keywords only recognized in attribute lists
285 * This table MUST be alphabetically sorted on the kw field
287 static const struct keyword attr_keywords[] =
289 {"aggregatable", tAGGREGATABLE},
290 {"allocate", tALLOCATE},
291 {"annotation", tANNOTATION},
292 {"appobject", tAPPOBJECT},
294 {"async_uuid", tASYNCUUID},
295 {"auto_handle", tAUTOHANDLE},
296 {"bindable", tBINDABLE},
297 {"broadcast", tBROADCAST},
298 {"byte_count", tBYTECOUNT},
299 {"call_as", tCALLAS},
300 {"callback", tCALLBACK},
302 {"comm_status", tCOMMSTATUS},
303 {"context_handle", tCONTEXTHANDLE},
304 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
305 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
306 {"control", tCONTROL},
307 {"defaultcollelem", tDEFAULTCOLLELEM},
308 {"defaultvalue", tDEFAULTVALUE},
309 {"defaultvtable", tDEFAULTVTABLE},
310 {"displaybind", tDISPLAYBIND},
311 {"dllname", tDLLNAME},
313 {"endpoint", tENDPOINT},
315 {"explicit_handle", tEXPLICITHANDLE},
317 {"helpcontext", tHELPCONTEXT},
318 {"helpfile", tHELPFILE},
319 {"helpstring", tHELPSTRING},
320 {"helpstringcontext", tHELPSTRINGCONTEXT},
321 {"helpstringdll", tHELPSTRINGDLL},
324 {"idempotent", tIDEMPOTENT},
326 {"immediatebind", tIMMEDIATEBIND},
327 {"implicit_handle", tIMPLICITHANDLE},
329 {"in_line", tIN_LINE},
330 {"input_sync", tINPUTSYNC},
332 {"length_is", tLENGTHIS},
334 {"nonbrowsable", tNONBROWSABLE},
335 {"noncreatable", tNONCREATABLE},
336 {"nonextensible", tNONEXTENSIBLE},
339 {"oleautomation", tOLEAUTOMATION},
340 {"optional", tOPTIONAL},
342 {"pointer_default", tPOINTERDEFAULT},
343 {"propget", tPROPGET},
344 {"propput", tPROPPUT},
345 {"propputref", tPROPPUTREF},
349 {"readonly", tREADONLY},
351 {"requestedit", tREQUESTEDIT},
352 {"restricted", tRESTRICTED},
354 {"size_is", tSIZEIS},
356 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
358 {"switch_is", tSWITCHIS},
359 {"switch_type", tSWITCHTYPE},
360 {"transmit_as", tTRANSMITAS},
363 {"v1_enum", tV1ENUM},
365 {"version", tVERSION},
366 {"wire_marshal", tWIREMARSHAL},
370 #define KWP(p) ((const struct keyword *)(p))
372 static int kw_cmp_func(const void *s1, const void *s2)
374 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
377 static int kw_token(const char *kw)
379 struct keyword key, *kwp;
381 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
383 parser_lval.str = xstrdup(kwp->kw);
386 parser_lval.str = xstrdup(kw);
387 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
390 static int attr_token(const char *kw)
392 struct keyword key, *kwp;
394 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
395 sizeof(attr_keywords[0]), kw_cmp_func);
397 parser_lval.str = xstrdup(kwp->kw);
403 static void addcchar(char c)
405 if(cbufidx >= cbufalloc)
408 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
409 if(cbufalloc > 65536)
410 parser_warning("Reallocating string buffer larger than 64kB\n");
412 cbuffer[cbufidx++] = c;
415 static char *get_buffered_cstring(void)
418 return xstrdup(cbuffer);
421 void pop_import(void)
423 int ptr = import_stack_ptr-1;
426 yy_delete_buffer( YY_CURRENT_BUFFER );
427 yy_switch_to_buffer( import_stack[ptr].state );
432 temp_name = import_stack[ptr].temp_name;
433 input_name = import_stack[ptr].input_name;
434 line_number = import_stack[ptr].line_number;
440 struct imports *next;
443 int do_import(char *fname)
447 struct imports *import;
448 int ptr = import_stack_ptr;
451 import = first_import;
452 while (import && strcmp(import->name, fname))
453 import = import->next;
454 if (import) return 0; /* already imported */
456 import = xmalloc(sizeof(struct imports));
457 import->name = xstrdup(fname);
458 import->next = first_import;
459 first_import = import;
461 /* don't search for a file name with a path in the include directories,
462 * for compatibility with MIDL */
463 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
464 path = xstrdup( fname );
465 else if (!(path = wpp_find_include( fname, input_name )))
466 error_loc("Unable to open include file %s\n", fname);
468 import_stack[ptr].temp_name = temp_name;
469 import_stack[ptr].input_name = input_name;
470 import_stack[ptr].line_number = line_number;
475 name = xstrdup( "widl.XXXXXX" );
476 if((fd = mkstemps( name, 0 )) == -1)
477 error("Could not generate a temp name from %s\n", name);
480 if (!(f = fdopen(fd, "wt")))
481 error("Could not open fd %s for writing\n", name);
483 ret = wpp_parse( path, f );
487 if((f = fopen(temp_name, "r")) == NULL)
488 error_loc("Unable to open %s\n", temp_name);
490 import_stack[ptr].state = YY_CURRENT_BUFFER;
491 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
495 void abort_import(void)
499 for (ptr=0; ptr<import_stack_ptr; ptr++)
500 unlink(import_stack[ptr].temp_name);