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 nounput noyy_top_state
23 %option 8bit never-interactive prefix="parser_"
27 cident [a-zA-Z_][0-9a-zA-Z_]*
31 uuid {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
32 double [0-9]+\.[0-9]+([eE][+-]?[0-9]+)*
51 #define YY_NO_UNISTD_H
59 #include "parser.tab.h"
61 extern char *temp_name;
63 static void addcchar(char c);
64 static char *get_buffered_cstring(void);
68 static int cbufalloc = 0;
70 static int kw_token(const char *kw);
71 static int attr_token(const char *kw);
73 #define MAX_IMPORT_DEPTH 10
75 YY_BUFFER_STATE state;
79 } import_stack[MAX_IMPORT_DEPTH];
80 int import_stack_ptr = 0;
82 UUID *parse_uuid(const char *u)
84 UUID* uuid = xmalloc(sizeof(UUID));
86 /* it would be nice to use UuidFromStringA */
87 uuid->Data1 = strtoul(u, NULL, 16);
88 uuid->Data2 = strtoul(u+9, NULL, 16);
89 uuid->Data3 = strtoul(u+14, NULL, 16);
91 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
92 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
93 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
94 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
95 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
96 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
97 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
98 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
105 **************************************************************************
106 * The flexer starts here
107 **************************************************************************
110 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
115 lineno = (int)strtol(yytext, &cptr, 10);
117 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
118 fname = strchr(cptr, '"');
120 error_loc("Malformed '#...' line-directive; missing filename\n");
122 cptr = strchr(fname, '"');
124 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
126 line_number = lineno - 1; /* We didn't read the newline */
128 input_name = xstrdup(fname);
130 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
133 parser_lval.str = get_buffered_cstring();
137 <QUOTE>\\\" addcchar(yytext[1]);
138 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
139 <QUOTE>. addcchar(yytext[0]);
140 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
141 <ATTR>\] yy_pop_state(); return ']';
142 <ATTR>{cident} return attr_token(yytext);
144 parser_lval.uuid = parse_uuid(yytext);
147 <INITIAL,ATTR>{hex} {
148 parser_lval.num = strtoul(yytext, NULL, 0);
151 <INITIAL,ATTR>{int} {
152 parser_lval.num = strtoul(yytext, NULL, 0);
156 parser_lval.dbl = strtod(yytext, NULL);
159 SAFEARRAY{ws}*/\( return tSAFEARRAY;
160 {cident} return kw_token(yytext);
161 <INITIAL,ATTR>\n line_number++;
163 <INITIAL,ATTR>\<\< return SHL;
164 <INITIAL,ATTR>\>\> return SHR;
165 <INITIAL,ATTR>. return yytext[0];
167 if (import_stack_ptr)
174 int parser_wrap(void)
185 static const struct keyword keywords[] = {
189 {"__fastcall", tFASTCALL},
191 {"__pascal", tPASCAL},
192 {"__stdcall", tSTDCALL},
194 {"_fastcall", tFASTCALL},
195 {"_pascal", tPASCAL},
196 {"_stdcall", tSTDCALL},
197 {"boolean", tBOOLEAN},
199 {"callback", tCALLBACK},
203 {"coclass", tCOCLASS},
205 {"comm_status", tCOMMSTATUS},
207 {"cpp_quote", tCPPQUOTE},
208 {"default", tDEFAULT},
209 {"dispinterface", tDISPINTERFACE},
212 {"error_status_t", tERRORSTATUST},
215 {"handle_t", tHANDLET},
218 {"importlib", tIMPORTLIB},
219 {"in_line", tINLINE},
221 {"interface", tINTERFACE},
222 {"library", tLIBRARY},
224 {"methods", tMETHODS},
227 {"properties", tPROPERTIES},
232 {"stdcall", tSTDCALL},
235 {"typedef", tTYPEDEF},
237 {"unsigned", tUNSIGNED},
241 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
243 /* keywords only recognized in attribute lists */
244 static const struct keyword attr_keywords[] =
246 {"aggregatable", tAGGREGATABLE},
247 {"allocate", tALLOCATE},
248 {"appobject", tAPPOBJECT},
250 {"async_uuid", tASYNCUUID},
251 {"auto_handle", tAUTOHANDLE},
252 {"bindable", tBINDABLE},
253 {"broadcast", tBROADCAST},
254 {"byte_count", tBYTECOUNT},
255 {"call_as", tCALLAS},
256 {"context_handle", tCONTEXTHANDLE},
257 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
258 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
259 {"control", tCONTROL},
260 {"defaultcollelem", tDEFAULTCOLLELEM},
261 {"defaultvalue", tDEFAULTVALUE},
262 {"defaultvtable", tDEFAULTVTABLE},
263 {"displaybind", tDISPLAYBIND},
264 {"dllname", tDLLNAME},
266 {"endpoint", tENDPOINT},
268 {"explicit_handle", tEXPLICITHANDLE},
270 {"helpcontext", tHELPCONTEXT},
271 {"helpfile", tHELPFILE},
272 {"helpstring", tHELPSTRING},
273 {"helpstringcontext", tHELPSTRINGCONTEXT},
274 {"helpstringdll", tHELPSTRINGDLL},
277 {"idempotent", tIDEMPOTENT},
279 {"immediatebind", tIMMEDIATEBIND},
280 {"implicit_handle", tIMPLICITHANDLE},
282 {"input_sync", tINPUTSYNC},
284 {"length_is", tLENGTHIS},
286 {"nonbrowsable", tNONBROWSABLE},
287 {"noncreatable", tNONCREATABLE},
288 {"nonextensible", tNONEXTENSIBLE},
291 {"oleautomation", tOLEAUTOMATION},
292 {"optional", tOPTIONAL},
294 {"pointer_default", tPOINTERDEFAULT},
295 {"propget", tPROPGET},
296 {"propput", tPROPPUT},
297 {"propputref", tPROPPUTREF},
301 {"readonly", tREADONLY},
303 {"requestedit", tREQUESTEDIT},
304 {"restricted", tRESTRICTED},
307 {"size_is", tSIZEIS},
309 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
311 {"switch_is", tSWITCHIS},
312 {"switch_type", tSWITCHTYPE},
313 {"transmit_as", tTRANSMITAS},
316 {"v1_enum", tV1ENUM},
318 {"version", tVERSION},
319 {"wire_marshal", tWIREMARSHAL},
323 #define KWP(p) ((const struct keyword *)(p))
325 static int kw_cmp_func(const void *s1, const void *s2)
327 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
330 static int kw_token(const char *kw)
332 struct keyword key, *kwp;
334 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
336 parser_lval.str = xstrdup(kwp->kw);
339 parser_lval.str = xstrdup(kw);
340 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
343 static int attr_token(const char *kw)
345 struct keyword key, *kwp;
347 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
348 sizeof(attr_keywords[0]), kw_cmp_func);
350 parser_lval.str = xstrdup(kwp->kw);
356 static void addcchar(char c)
358 if(cbufidx >= cbufalloc)
361 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
362 if(cbufalloc > 65536)
363 parser_warning("Reallocating string buffer larger than 64kB\n");
365 cbuffer[cbufidx++] = c;
368 static char *get_buffered_cstring(void)
371 return xstrdup(cbuffer);
374 void pop_import(void)
376 int ptr = import_stack_ptr-1;
379 yy_delete_buffer( YY_CURRENT_BUFFER );
380 yy_switch_to_buffer( import_stack[ptr].state );
385 temp_name = import_stack[ptr].temp_name;
387 input_name = import_stack[ptr].input_name;
388 line_number = import_stack[ptr].line_number;
394 struct imports *next;
397 int do_import(char *fname)
400 char *hname, *path, *p;
401 struct imports *import;
402 int ptr = import_stack_ptr;
405 if (!parse_only && do_header) {
406 hname = dup_basename(fname, ".idl");
407 p = hname + strlen(hname) - 2;
408 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
410 fprintf(header, "#include <%s>\n", hname);
414 import = first_import;
415 while (import && strcmp(import->name, fname))
416 import = import->next;
417 if (import) return 0; /* already imported */
419 import = xmalloc(sizeof(struct imports));
420 import->name = xstrdup(fname);
421 import->next = first_import;
422 first_import = import;
424 /* don't search for a file name with a path in the include directories,
425 * for compatibility with MIDL */
426 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
427 path = strdup( fname );
428 else if (!(path = wpp_find_include( fname, input_name )))
429 error_loc("Unable to open include file %s\n", fname);
431 import_stack[ptr].temp_name = temp_name;
432 import_stack[ptr].input_name = input_name;
433 import_stack[ptr].line_number = line_number;
438 ret = wpp_parse_temp( path, NULL, &temp_name );
441 if((f = fopen(temp_name, "r")) == NULL)
442 error_loc("Unable to open %s\n", temp_name);
444 import_stack[ptr].state = YY_CURRENT_BUFFER;
445 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
449 void abort_import(void)
453 for (ptr=0; ptr<import_stack_ptr; ptr++)
454 unlink(import_stack[ptr].temp_name);