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]+)*
56 #define YY_NO_UNISTD_H
64 #include "parser.tab.h"
66 extern char *temp_name;
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 long xstrtoul(const char *nptr, char **endptr, int base)
94 l = strtoul(nptr, endptr, base);
95 if (l == ULONG_MAX && errno == ERANGE)
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);
157 parser_lval.str = get_buffered_cstring();
160 <QUOTE,WSTRQUOTE>\\\\ |
161 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
162 <QUOTE,WSTRQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
163 <QUOTE,WSTRQUOTE>. addcchar(yytext[0]);
164 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
165 <ATTR>\] yy_pop_state(); return ']';
166 <ATTR>{cident} return attr_token(yytext);
168 parser_lval.uuid = parse_uuid(yytext);
171 <INITIAL,ATTR>{hex} {
172 parser_lval.num = xstrtoul(yytext, NULL, 0);
175 <INITIAL,ATTR>{int} {
176 parser_lval.num = xstrtoul(yytext, NULL, 0);
180 parser_lval.dbl = strtod(yytext, NULL);
183 SAFEARRAY{ws}*/\( return tSAFEARRAY;
184 {cident} return kw_token(yytext);
185 <INITIAL,ATTR>\n line_number++;
187 <INITIAL,ATTR>\<\< return SHL;
188 <INITIAL,ATTR>\>\> return SHR;
189 <INITIAL,ATTR>\-\> return MEMBERPTR;
190 <INITIAL,ATTR>== return EQUALITY;
191 <INITIAL,ATTR>!= return INEQUALITY;
192 <INITIAL,ATTR>\>= return GREATEREQUAL;
193 <INITIAL,ATTR>\<= return LESSEQUAL;
194 <INITIAL,ATTR>\|\| return LOGICALOR;
195 <INITIAL,ATTR>&& return LOGICALAND;
196 <INITIAL,ATTR>. return yytext[0];
198 if (import_stack_ptr)
205 int parser_wrap(void)
216 /* This table MUST be alphabetically sorted on the kw field */
217 static const struct keyword keywords[] = {
222 {"__fastcall", tFASTCALL},
224 {"__pascal", tPASCAL},
225 {"__stdcall", tSTDCALL},
227 {"_fastcall", tFASTCALL},
228 {"_pascal", tPASCAL},
229 {"_stdcall", tSTDCALL},
230 {"boolean", tBOOLEAN},
235 {"coclass", tCOCLASS},
237 {"cpp_quote", tCPPQUOTE},
238 {"default", tDEFAULT},
239 {"dispinterface", tDISPINTERFACE},
242 {"error_status_t", tERRORSTATUST},
245 {"handle_t", tHANDLET},
248 {"importlib", tIMPORTLIB},
251 {"interface", tINTERFACE},
252 {"library", tLIBRARY},
254 {"methods", tMETHODS},
257 {"properties", tPROPERTIES},
258 {"register", tREGISTER},
264 {"stdcall", tSTDCALL},
267 {"typedef", tTYPEDEF},
269 {"unsigned", tUNSIGNED},
273 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
275 /* keywords only recognized in attribute lists
276 * This table MUST be alphabetically sorted on the kw field
278 static const struct keyword attr_keywords[] =
280 {"aggregatable", tAGGREGATABLE},
281 {"allocate", tALLOCATE},
282 {"appobject", tAPPOBJECT},
284 {"async_uuid", tASYNCUUID},
285 {"auto_handle", tAUTOHANDLE},
286 {"bindable", tBINDABLE},
287 {"broadcast", tBROADCAST},
288 {"byte_count", tBYTECOUNT},
289 {"call_as", tCALLAS},
290 {"callback", tCALLBACK},
292 {"comm_status", tCOMMSTATUS},
293 {"context_handle", tCONTEXTHANDLE},
294 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
295 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
296 {"control", tCONTROL},
297 {"defaultcollelem", tDEFAULTCOLLELEM},
298 {"defaultvalue", tDEFAULTVALUE},
299 {"defaultvtable", tDEFAULTVTABLE},
300 {"displaybind", tDISPLAYBIND},
301 {"dllname", tDLLNAME},
303 {"endpoint", tENDPOINT},
305 {"explicit_handle", tEXPLICITHANDLE},
307 {"helpcontext", tHELPCONTEXT},
308 {"helpfile", tHELPFILE},
309 {"helpstring", tHELPSTRING},
310 {"helpstringcontext", tHELPSTRINGCONTEXT},
311 {"helpstringdll", tHELPSTRINGDLL},
314 {"idempotent", tIDEMPOTENT},
316 {"immediatebind", tIMMEDIATEBIND},
317 {"implicit_handle", tIMPLICITHANDLE},
319 {"in_line", tIN_LINE},
320 {"input_sync", tINPUTSYNC},
322 {"length_is", tLENGTHIS},
324 {"nonbrowsable", tNONBROWSABLE},
325 {"noncreatable", tNONCREATABLE},
326 {"nonextensible", tNONEXTENSIBLE},
329 {"oleautomation", tOLEAUTOMATION},
330 {"optional", tOPTIONAL},
332 {"pointer_default", tPOINTERDEFAULT},
333 {"propget", tPROPGET},
334 {"propput", tPROPPUT},
335 {"propputref", tPROPPUTREF},
339 {"readonly", tREADONLY},
341 {"requestedit", tREQUESTEDIT},
342 {"restricted", tRESTRICTED},
344 {"size_is", tSIZEIS},
346 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
348 {"switch_is", tSWITCHIS},
349 {"switch_type", tSWITCHTYPE},
350 {"transmit_as", tTRANSMITAS},
353 {"v1_enum", tV1ENUM},
355 {"version", tVERSION},
356 {"wire_marshal", tWIREMARSHAL},
360 #define KWP(p) ((const struct keyword *)(p))
362 static int kw_cmp_func(const void *s1, const void *s2)
364 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
367 static int kw_token(const char *kw)
369 struct keyword key, *kwp;
371 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
373 parser_lval.str = xstrdup(kwp->kw);
376 parser_lval.str = xstrdup(kw);
377 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
380 static int attr_token(const char *kw)
382 struct keyword key, *kwp;
384 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
385 sizeof(attr_keywords[0]), kw_cmp_func);
387 parser_lval.str = xstrdup(kwp->kw);
393 static void addcchar(char c)
395 if(cbufidx >= cbufalloc)
398 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
399 if(cbufalloc > 65536)
400 parser_warning("Reallocating string buffer larger than 64kB\n");
402 cbuffer[cbufidx++] = c;
405 static char *get_buffered_cstring(void)
408 return xstrdup(cbuffer);
411 void pop_import(void)
413 int ptr = import_stack_ptr-1;
416 yy_delete_buffer( YY_CURRENT_BUFFER );
417 yy_switch_to_buffer( import_stack[ptr].state );
422 temp_name = import_stack[ptr].temp_name;
423 input_name = import_stack[ptr].input_name;
424 line_number = import_stack[ptr].line_number;
430 struct imports *next;
433 int do_import(char *fname)
437 struct imports *import;
438 int ptr = import_stack_ptr;
441 import = first_import;
442 while (import && strcmp(import->name, fname))
443 import = import->next;
444 if (import) return 0; /* already imported */
446 import = xmalloc(sizeof(struct imports));
447 import->name = xstrdup(fname);
448 import->next = first_import;
449 first_import = import;
451 /* don't search for a file name with a path in the include directories,
452 * for compatibility with MIDL */
453 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
454 path = strdup( fname );
455 else if (!(path = wpp_find_include( fname, input_name )))
456 error_loc("Unable to open include file %s\n", fname);
458 import_stack[ptr].temp_name = temp_name;
459 import_stack[ptr].input_name = input_name;
460 import_stack[ptr].line_number = line_number;
465 ret = wpp_parse_temp( path, NULL, &temp_name );
468 if((f = fopen(temp_name, "r")) == NULL)
469 error_loc("Unable to open %s\n", temp_name);
471 import_stack[ptr].state = YY_CURRENT_BUFFER;
472 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
476 void abort_import(void)
480 for (ptr=0; ptr<import_stack_ptr; ptr++)
481 unlink(import_stack[ptr].temp_name);