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]+)*
54 #define YY_NO_UNISTD_H
62 #include "parser.tab.h"
64 extern char *temp_name;
66 static void addcchar(char c);
67 static char *get_buffered_cstring(void);
71 static int cbufalloc = 0;
73 static int kw_token(const char *kw);
74 static int attr_token(const char *kw);
76 #define MAX_IMPORT_DEPTH 10
78 YY_BUFFER_STATE state;
82 } import_stack[MAX_IMPORT_DEPTH];
83 int import_stack_ptr = 0;
85 UUID *parse_uuid(const char *u)
87 UUID* uuid = xmalloc(sizeof(UUID));
89 /* it would be nice to use UuidFromStringA */
90 uuid->Data1 = strtoul(u, NULL, 16);
91 uuid->Data2 = strtoul(u+9, NULL, 16);
92 uuid->Data3 = strtoul(u+14, NULL, 16);
94 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
95 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
96 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
97 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
98 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
99 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
100 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
101 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
108 **************************************************************************
109 * The flexer starts here
110 **************************************************************************
113 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
118 lineno = (int)strtol(yytext, &cptr, 10);
120 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
121 fname = strchr(cptr, '"');
123 error_loc("Malformed '#...' line-directive; missing filename\n");
125 cptr = strchr(fname, '"');
127 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
129 line_number = lineno - 1; /* We didn't read the newline */
131 input_name = xstrdup(fname);
133 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
136 parser_lval.str = get_buffered_cstring();
139 <INITIAL,ATTR>L\" yy_push_state(WSTRQUOTE);
142 parser_lval.str = get_buffered_cstring();
145 <QUOTE,WSTRQUOTE>\\\\ |
146 <QUOTE,WSTRQUOTE>\\\" addcchar(yytext[1]);
147 <QUOTE,WSTRQUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
148 <QUOTE,WSTRQUOTE>. addcchar(yytext[0]);
149 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
150 <ATTR>\] yy_pop_state(); return ']';
151 <ATTR>{cident} return attr_token(yytext);
153 parser_lval.uuid = parse_uuid(yytext);
156 <INITIAL,ATTR>{hex} {
157 parser_lval.num = strtoul(yytext, NULL, 0);
160 <INITIAL,ATTR>{int} {
161 parser_lval.num = strtoul(yytext, NULL, 0);
165 parser_lval.dbl = strtod(yytext, NULL);
168 SAFEARRAY{ws}*/\( return tSAFEARRAY;
169 {cident} return kw_token(yytext);
170 <INITIAL,ATTR>\n line_number++;
172 <INITIAL,ATTR>\<\< return SHL;
173 <INITIAL,ATTR>\>\> return SHR;
174 <INITIAL,ATTR>\-\> return MEMBERPTR;
175 <INITIAL,ATTR>== return EQUALITY;
176 <INITIAL,ATTR>!= return INEQUALITY;
177 <INITIAL,ATTR>\>= return GREATEREQUAL;
178 <INITIAL,ATTR>\<= return LESSEQUAL;
179 <INITIAL,ATTR>\|\| return LOGICALOR;
180 <INITIAL,ATTR>&& return LOGICALAND;
181 <INITIAL,ATTR>. return yytext[0];
183 if (import_stack_ptr)
190 int parser_wrap(void)
201 /* This table MUST be alphabetically sorted on the kw field */
202 static const struct keyword keywords[] = {
207 {"__fastcall", tFASTCALL},
209 {"__pascal", tPASCAL},
210 {"__stdcall", tSTDCALL},
212 {"_fastcall", tFASTCALL},
213 {"_pascal", tPASCAL},
214 {"_stdcall", tSTDCALL},
215 {"boolean", tBOOLEAN},
220 {"coclass", tCOCLASS},
222 {"cpp_quote", tCPPQUOTE},
223 {"default", tDEFAULT},
224 {"dispinterface", tDISPINTERFACE},
227 {"error_status_t", tERRORSTATUST},
230 {"handle_t", tHANDLET},
233 {"importlib", tIMPORTLIB},
236 {"interface", tINTERFACE},
237 {"library", tLIBRARY},
239 {"methods", tMETHODS},
242 {"properties", tPROPERTIES},
243 {"register", tREGISTER},
249 {"stdcall", tSTDCALL},
252 {"typedef", tTYPEDEF},
254 {"unsigned", tUNSIGNED},
258 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
260 /* keywords only recognized in attribute lists
261 * This table MUST be alphabetically sorted on the kw field
263 static const struct keyword attr_keywords[] =
265 {"aggregatable", tAGGREGATABLE},
266 {"allocate", tALLOCATE},
267 {"appobject", tAPPOBJECT},
269 {"async_uuid", tASYNCUUID},
270 {"auto_handle", tAUTOHANDLE},
271 {"bindable", tBINDABLE},
272 {"broadcast", tBROADCAST},
273 {"byte_count", tBYTECOUNT},
274 {"call_as", tCALLAS},
275 {"callback", tCALLBACK},
277 {"comm_status", tCOMMSTATUS},
278 {"context_handle", tCONTEXTHANDLE},
279 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
280 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
281 {"control", tCONTROL},
282 {"defaultcollelem", tDEFAULTCOLLELEM},
283 {"defaultvalue", tDEFAULTVALUE},
284 {"defaultvtable", tDEFAULTVTABLE},
285 {"displaybind", tDISPLAYBIND},
286 {"dllname", tDLLNAME},
288 {"endpoint", tENDPOINT},
290 {"explicit_handle", tEXPLICITHANDLE},
292 {"helpcontext", tHELPCONTEXT},
293 {"helpfile", tHELPFILE},
294 {"helpstring", tHELPSTRING},
295 {"helpstringcontext", tHELPSTRINGCONTEXT},
296 {"helpstringdll", tHELPSTRINGDLL},
299 {"idempotent", tIDEMPOTENT},
301 {"immediatebind", tIMMEDIATEBIND},
302 {"implicit_handle", tIMPLICITHANDLE},
304 {"in_line", tIN_LINE},
305 {"input_sync", tINPUTSYNC},
307 {"length_is", tLENGTHIS},
309 {"nonbrowsable", tNONBROWSABLE},
310 {"noncreatable", tNONCREATABLE},
311 {"nonextensible", tNONEXTENSIBLE},
314 {"oleautomation", tOLEAUTOMATION},
315 {"optional", tOPTIONAL},
317 {"pointer_default", tPOINTERDEFAULT},
318 {"propget", tPROPGET},
319 {"propput", tPROPPUT},
320 {"propputref", tPROPPUTREF},
324 {"readonly", tREADONLY},
326 {"requestedit", tREQUESTEDIT},
327 {"restricted", tRESTRICTED},
330 {"size_is", tSIZEIS},
332 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
334 {"switch_is", tSWITCHIS},
335 {"switch_type", tSWITCHTYPE},
336 {"transmit_as", tTRANSMITAS},
339 {"v1_enum", tV1ENUM},
341 {"version", tVERSION},
342 {"wire_marshal", tWIREMARSHAL},
346 #define KWP(p) ((const struct keyword *)(p))
348 static int kw_cmp_func(const void *s1, const void *s2)
350 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
353 static int kw_token(const char *kw)
355 struct keyword key, *kwp;
357 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
359 parser_lval.str = xstrdup(kwp->kw);
362 parser_lval.str = xstrdup(kw);
363 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
366 static int attr_token(const char *kw)
368 struct keyword key, *kwp;
370 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
371 sizeof(attr_keywords[0]), kw_cmp_func);
373 parser_lval.str = xstrdup(kwp->kw);
379 static void addcchar(char c)
381 if(cbufidx >= cbufalloc)
384 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
385 if(cbufalloc > 65536)
386 parser_warning("Reallocating string buffer larger than 64kB\n");
388 cbuffer[cbufidx++] = c;
391 static char *get_buffered_cstring(void)
394 return xstrdup(cbuffer);
397 void pop_import(void)
399 int ptr = import_stack_ptr-1;
402 yy_delete_buffer( YY_CURRENT_BUFFER );
403 yy_switch_to_buffer( import_stack[ptr].state );
408 temp_name = import_stack[ptr].temp_name;
409 input_name = import_stack[ptr].input_name;
410 line_number = import_stack[ptr].line_number;
416 struct imports *next;
419 int do_import(char *fname)
423 struct imports *import;
424 int ptr = import_stack_ptr;
427 import = first_import;
428 while (import && strcmp(import->name, fname))
429 import = import->next;
430 if (import) return 0; /* already imported */
432 import = xmalloc(sizeof(struct imports));
433 import->name = xstrdup(fname);
434 import->next = first_import;
435 first_import = import;
437 /* don't search for a file name with a path in the include directories,
438 * for compatibility with MIDL */
439 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
440 path = strdup( fname );
441 else if (!(path = wpp_find_include( fname, input_name )))
442 error_loc("Unable to open include file %s\n", fname);
444 import_stack[ptr].temp_name = temp_name;
445 import_stack[ptr].input_name = input_name;
446 import_stack[ptr].line_number = line_number;
451 ret = wpp_parse_temp( path, NULL, &temp_name );
454 if((f = fopen(temp_name, "r")) == NULL)
455 error_loc("Unable to open %s\n", temp_name);
457 import_stack[ptr].state = YY_CURRENT_BUFFER;
458 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
462 void abort_import(void)
466 for (ptr=0; ptr<import_stack_ptr; ptr++)
467 unlink(import_stack[ptr].temp_name);