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]+)*
57 #define YY_NO_UNISTD_H
65 #include "parser.tab.h"
67 static void addcchar(char c);
68 static char *get_buffered_cstring(void);
72 static int cbufalloc = 0;
74 static int kw_token(const char *kw);
75 static int attr_token(const char *kw);
77 #define MAX_IMPORT_DEPTH 10
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 long xstrtoul(const char *nptr, char **endptr, int base)
93 l = strtoul(nptr, endptr, base);
94 if (l == ULONG_MAX && errno == ERANGE)
95 error_loc("integer constant %s is too large\n", nptr);
99 UUID *parse_uuid(const char *u)
101 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,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
132 lineno = (int)strtol(yytext, &cptr, 10);
134 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
135 fname = strchr(cptr, '"');
137 error_loc("Malformed '#...' line-directive; missing filename\n");
139 cptr = strchr(fname, '"');
141 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
143 line_number = lineno - 1; /* We didn't read the newline */
145 input_name = xstrdup(fname);
147 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
150 parser_lval.str = get_buffered_cstring();
153 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE);
156 parser_lval.str = get_buffered_cstring();
159 <INITIAL,ATTR>\' yy_push_state(SQUOTE); cbufidx = 0;
162 parser_lval.str = get_buffered_cstring();
165 <QUOTE,WSTRQUOTE,SQUOTE>\\\\ |
166 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
167 <SQUOTE>\\\' addcchar(yytext[1]);
168 <QUOTE,WSTRQUOTE,SQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
169 <QUOTE,WSTRQUOTE,SQUOTE>. addcchar(yytext[0]);
170 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
171 <ATTR>\] yy_pop_state(); return ']';
172 <ATTR>{cident} return attr_token(yytext);
174 parser_lval.uuid = parse_uuid(yytext);
177 <INITIAL,ATTR>{hex} {
178 parser_lval.num = xstrtoul(yytext, NULL, 0);
181 <INITIAL,ATTR>{int} {
182 parser_lval.num = xstrtoul(yytext, NULL, 0);
186 parser_lval.dbl = strtod(yytext, NULL);
189 SAFEARRAY{ws}*/\( return tSAFEARRAY;
190 {cident} return kw_token(yytext);
191 <INITIAL,ATTR>\n line_number++;
193 <INITIAL,ATTR>\<\< return SHL;
194 <INITIAL,ATTR>\>\> return SHR;
195 <INITIAL,ATTR>\-\> return MEMBERPTR;
196 <INITIAL,ATTR>== return EQUALITY;
197 <INITIAL,ATTR>!= return INEQUALITY;
198 <INITIAL,ATTR>\>= return GREATEREQUAL;
199 <INITIAL,ATTR>\<= return LESSEQUAL;
200 <INITIAL,ATTR>\|\| return LOGICALOR;
201 <INITIAL,ATTR>&& return LOGICALAND;
202 <INITIAL,ATTR>\.\.\. return ELLIPSIS;
203 <INITIAL,ATTR>. return yytext[0];
205 if (import_stack_ptr)
212 int parser_wrap(void)
223 /* This table MUST be alphabetically sorted on the kw field */
224 static const struct keyword keywords[] = {
229 {"__fastcall", tFASTCALL},
230 {"__int3264", tINT3264},
232 {"__pascal", tPASCAL},
233 {"__stdcall", tSTDCALL},
235 {"_fastcall", tFASTCALL},
236 {"_pascal", tPASCAL},
237 {"_stdcall", tSTDCALL},
238 {"boolean", tBOOLEAN},
243 {"coclass", tCOCLASS},
245 {"cpp_quote", tCPPQUOTE},
246 {"default", tDEFAULT},
247 {"dispinterface", tDISPINTERFACE},
250 {"error_status_t", tERRORSTATUST},
253 {"handle_t", tHANDLET},
256 {"importlib", tIMPORTLIB},
259 {"interface", tINTERFACE},
260 {"library", tLIBRARY},
262 {"methods", tMETHODS},
265 {"properties", tPROPERTIES},
266 {"register", tREGISTER},
272 {"stdcall", tSTDCALL},
275 {"typedef", tTYPEDEF},
277 {"unsigned", tUNSIGNED},
281 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
283 /* keywords only recognized in attribute lists
284 * This table MUST be alphabetically sorted on the kw field
286 static const struct keyword attr_keywords[] =
288 {"aggregatable", tAGGREGATABLE},
289 {"allocate", tALLOCATE},
290 {"annotation", tANNOTATION},
291 {"appobject", tAPPOBJECT},
293 {"async_uuid", tASYNCUUID},
294 {"auto_handle", tAUTOHANDLE},
295 {"bindable", tBINDABLE},
296 {"broadcast", tBROADCAST},
297 {"byte_count", tBYTECOUNT},
298 {"call_as", tCALLAS},
299 {"callback", tCALLBACK},
301 {"comm_status", tCOMMSTATUS},
302 {"context_handle", tCONTEXTHANDLE},
303 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
304 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
305 {"control", tCONTROL},
306 {"defaultcollelem", tDEFAULTCOLLELEM},
307 {"defaultvalue", tDEFAULTVALUE},
308 {"defaultvtable", tDEFAULTVTABLE},
309 {"displaybind", tDISPLAYBIND},
310 {"dllname", tDLLNAME},
312 {"endpoint", tENDPOINT},
314 {"explicit_handle", tEXPLICITHANDLE},
316 {"helpcontext", tHELPCONTEXT},
317 {"helpfile", tHELPFILE},
318 {"helpstring", tHELPSTRING},
319 {"helpstringcontext", tHELPSTRINGCONTEXT},
320 {"helpstringdll", tHELPSTRINGDLL},
323 {"idempotent", tIDEMPOTENT},
325 {"immediatebind", tIMMEDIATEBIND},
326 {"implicit_handle", tIMPLICITHANDLE},
328 {"in_line", tIN_LINE},
329 {"input_sync", tINPUTSYNC},
331 {"length_is", tLENGTHIS},
333 {"nonbrowsable", tNONBROWSABLE},
334 {"noncreatable", tNONCREATABLE},
335 {"nonextensible", tNONEXTENSIBLE},
338 {"oleautomation", tOLEAUTOMATION},
339 {"optional", tOPTIONAL},
341 {"pointer_default", tPOINTERDEFAULT},
342 {"propget", tPROPGET},
343 {"propput", tPROPPUT},
344 {"propputref", tPROPPUTREF},
348 {"readonly", tREADONLY},
350 {"requestedit", tREQUESTEDIT},
351 {"restricted", tRESTRICTED},
353 {"size_is", tSIZEIS},
355 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
357 {"switch_is", tSWITCHIS},
358 {"switch_type", tSWITCHTYPE},
359 {"transmit_as", tTRANSMITAS},
362 {"v1_enum", tV1ENUM},
364 {"version", tVERSION},
365 {"wire_marshal", tWIREMARSHAL},
369 #define KWP(p) ((const struct keyword *)(p))
371 static int kw_cmp_func(const void *s1, const void *s2)
373 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
376 static int kw_token(const char *kw)
378 struct keyword key, *kwp;
380 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
382 parser_lval.str = xstrdup(kwp->kw);
385 parser_lval.str = xstrdup(kw);
386 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
389 static int attr_token(const char *kw)
391 struct keyword key, *kwp;
393 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
394 sizeof(attr_keywords[0]), kw_cmp_func);
396 parser_lval.str = xstrdup(kwp->kw);
402 static void addcchar(char c)
404 if(cbufidx >= cbufalloc)
407 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
408 if(cbufalloc > 65536)
409 parser_warning("Reallocating string buffer larger than 64kB\n");
411 cbuffer[cbufidx++] = c;
414 static char *get_buffered_cstring(void)
417 return xstrdup(cbuffer);
420 void pop_import(void)
422 int ptr = import_stack_ptr-1;
425 yy_delete_buffer( YY_CURRENT_BUFFER );
426 yy_switch_to_buffer( import_stack[ptr].state );
431 temp_name = import_stack[ptr].temp_name;
432 input_name = import_stack[ptr].input_name;
433 line_number = import_stack[ptr].line_number;
439 struct imports *next;
442 int do_import(char *fname)
446 struct imports *import;
447 int ptr = import_stack_ptr;
450 import = first_import;
451 while (import && strcmp(import->name, fname))
452 import = import->next;
453 if (import) return 0; /* already imported */
455 import = xmalloc(sizeof(struct imports));
456 import->name = xstrdup(fname);
457 import->next = first_import;
458 first_import = import;
460 /* don't search for a file name with a path in the include directories,
461 * for compatibility with MIDL */
462 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
463 path = strdup( fname );
464 else if (!(path = wpp_find_include( fname, input_name )))
465 error_loc("Unable to open include file %s\n", fname);
467 import_stack[ptr].temp_name = temp_name;
468 import_stack[ptr].input_name = input_name;
469 import_stack[ptr].line_number = line_number;
474 ret = wpp_parse_temp( path, NULL, &temp_name );
477 if((f = fopen(temp_name, "r")) == NULL)
478 error_loc("Unable to open %s\n", temp_name);
480 import_stack[ptr].state = YY_CURRENT_BUFFER;
481 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
485 void abort_import(void)
489 for (ptr=0; ptr<import_stack_ptr; ptr++)
490 unlink(import_stack[ptr].temp_name);