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 static void pop_import(void);
84 UUID *parse_uuid(const char *u)
86 UUID* uuid = xmalloc(sizeof(UUID));
88 /* it would be nice to use UuidFromStringA */
89 uuid->Data1 = strtoul(u, NULL, 16);
90 uuid->Data2 = strtoul(u+9, NULL, 16);
91 uuid->Data3 = strtoul(u+14, NULL, 16);
93 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
94 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
95 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
96 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
97 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
98 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
99 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
100 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
107 **************************************************************************
108 * The flexer starts here
109 **************************************************************************
112 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
117 lineno = (int)strtol(yytext, &cptr, 10);
119 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
120 fname = strchr(cptr, '"');
122 error_loc("Malformed '#...' line-directive; missing filename\n");
124 cptr = strchr(fname, '"');
126 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
128 line_number = lineno - 1; /* We didn't read the newline */
130 input_name = xstrdup(fname);
132 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
135 parser_lval.str = get_buffered_cstring();
139 <QUOTE>\\\" addcchar(yytext[1]);
140 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
141 <QUOTE>. addcchar(yytext[0]);
142 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
143 <ATTR>\] yy_pop_state(); return ']';
144 <ATTR>{cident} return attr_token(yytext);
146 parser_lval.uuid = parse_uuid(yytext);
149 <INITIAL,ATTR>{hex} {
150 parser_lval.num = strtoul(yytext, NULL, 0);
153 <INITIAL,ATTR>{int} {
154 parser_lval.num = strtoul(yytext, NULL, 0);
158 parser_lval.dbl = strtod(yytext, NULL);
161 SAFEARRAY{ws}*/\( return tSAFEARRAY;
162 {cident} return kw_token(yytext);
163 <INITIAL,ATTR>\n line_number++;
165 <INITIAL,ATTR>\<\< return SHL;
166 <INITIAL,ATTR>\>\> return SHR;
167 <INITIAL,ATTR>. return yytext[0];
169 if (import_stack_ptr) {
178 int parser_wrap(void)
189 static const struct keyword keywords[] = {
194 {"__stdcall", tSTDCALL},
195 {"_stdcall", tSTDCALL},
196 {"boolean", tBOOLEAN},
198 {"callback", tCALLBACK},
201 {"coclass", tCOCLASS},
203 {"comm_status", tCOMMSTATUS},
205 {"cpp_quote", tCPPQUOTE},
206 {"default", tDEFAULT},
207 {"dispinterface", tDISPINTERFACE},
210 {"error_status_t", tERRORSTATUST},
213 {"handle_t", tHANDLET},
216 {"importlib", tIMPORTLIB},
217 {"in_line", tINLINE},
219 {"interface", tINTERFACE},
220 {"library", tLIBRARY},
222 {"methods", tMETHODS},
224 {"properties", tPROPERTIES},
231 {"typedef", tTYPEDEF},
233 {"unsigned", tUNSIGNED},
237 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
239 /* keywords only recognized in attribute lists */
240 static const struct keyword attr_keywords[] =
242 {"aggregatable", tAGGREGATABLE},
243 {"allocate", tALLOCATE},
244 {"appobject", tAPPOBJECT},
246 {"async_uuid", tASYNCUUID},
247 {"auto_handle", tAUTOHANDLE},
248 {"bindable", tBINDABLE},
249 {"broadcast", tBROADCAST},
250 {"byte_count", tBYTECOUNT},
251 {"call_as", tCALLAS},
252 {"context_handle", tCONTEXTHANDLE},
253 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
254 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
255 {"control", tCONTROL},
256 {"defaultcollelem", tDEFAULTCOLLELEM},
257 {"defaultvalue", tDEFAULTVALUE},
258 {"defaultvtable", tDEFAULTVTABLE},
259 {"displaybind", tDISPLAYBIND},
260 {"dllname", tDLLNAME},
262 {"endpoint", tENDPOINT},
264 {"explicit_handle", tEXPLICITHANDLE},
266 {"helpcontext", tHELPCONTEXT},
267 {"helpfile", tHELPFILE},
268 {"helpstring", tHELPSTRING},
269 {"helpstringcontext", tHELPSTRINGCONTEXT},
270 {"helpstringdll", tHELPSTRINGDLL},
273 {"idempotent", tIDEMPOTENT},
275 {"immediatebind", tIMMEDIATEBIND},
276 {"implicit_handle", tIMPLICITHANDLE},
278 {"input_sync", tINPUTSYNC},
280 {"length_is", tLENGTHIS},
282 {"nonbrowsable", tNONBROWSABLE},
283 {"noncreatable", tNONCREATABLE},
284 {"nonextensible", tNONEXTENSIBLE},
287 {"oleautomation", tOLEAUTOMATION},
288 {"optional", tOPTIONAL},
290 {"pointer_default", tPOINTERDEFAULT},
291 {"propget", tPROPGET},
292 {"propput", tPROPPUT},
293 {"propputref", tPROPPUTREF},
297 {"readonly", tREADONLY},
299 {"requestedit", tREQUESTEDIT},
300 {"restricted", tRESTRICTED},
303 {"size_is", tSIZEIS},
305 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
307 {"switch_is", tSWITCHIS},
308 {"switch_type", tSWITCHTYPE},
309 {"transmit_as", tTRANSMITAS},
312 {"v1_enum", tV1ENUM},
314 {"version", tVERSION},
315 {"wire_marshal", tWIREMARSHAL},
319 #define KWP(p) ((const struct keyword *)(p))
321 static int kw_cmp_func(const void *s1, const void *s2)
323 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
326 static int kw_token(const char *kw)
328 struct keyword key, *kwp;
330 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
332 parser_lval.str = xstrdup(kwp->kw);
335 parser_lval.str = xstrdup(kw);
336 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
339 static int attr_token(const char *kw)
341 struct keyword key, *kwp;
343 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
344 sizeof(attr_keywords[0]), kw_cmp_func);
346 parser_lval.str = xstrdup(kwp->kw);
352 static void addcchar(char c)
354 if(cbufidx >= cbufalloc)
357 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
358 if(cbufalloc > 65536)
359 parser_warning("Reallocating string buffer larger than 64kB\n");
361 cbuffer[cbufidx++] = c;
364 static char *get_buffered_cstring(void)
367 return xstrdup(cbuffer);
370 static void pop_import(void)
372 int ptr = import_stack_ptr-1;
375 yy_delete_buffer( YY_CURRENT_BUFFER );
376 yy_switch_to_buffer( import_stack[ptr].state );
381 temp_name = import_stack[ptr].temp_name;
383 input_name = import_stack[ptr].input_name;
384 line_number = import_stack[ptr].line_number;
390 struct imports *next;
393 int do_import(char *fname)
396 char *hname, *path, *p;
397 struct imports *import;
398 int ptr = import_stack_ptr;
401 if (!parse_only && do_header) {
402 hname = dup_basename(fname, ".idl");
403 p = hname + strlen(hname) - 2;
404 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
406 fprintf(header, "#include <%s>\n", hname);
410 import = first_import;
411 while (import && strcmp(import->name, fname))
412 import = import->next;
413 if (import) return 0; /* already imported */
415 import = xmalloc(sizeof(struct imports));
416 import->name = xstrdup(fname);
417 import->next = first_import;
418 first_import = import;
420 /* don't search for a file name with a path in the include directories,
421 * for compatibility with MIDL */
422 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
423 path = strdup( fname );
424 else if (!(path = wpp_find_include( fname, input_name )))
425 error_loc("Unable to open include file %s\n", fname);
427 import_stack[ptr].temp_name = temp_name;
428 import_stack[ptr].input_name = input_name;
429 import_stack[ptr].line_number = line_number;
434 ret = wpp_parse_temp( path, NULL, &temp_name );
437 if((f = fopen(temp_name, "r")) == NULL)
438 error_loc("Unable to open %s\n", temp_name);
440 import_stack[ptr].state = YY_CURRENT_BUFFER;
441 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
445 void abort_import(void)
449 for (ptr=0; ptr<import_stack_ptr; ptr++)
450 unlink(import_stack[ptr].temp_name);