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]+)*
49 #define YY_NO_UNISTD_H
57 #include "parser.tab.h"
59 extern char *temp_name;
61 static void addcchar(char c);
62 static char *get_buffered_cstring(void);
66 static int cbufalloc = 0;
68 static int kw_token(const char *kw);
69 static int attr_token(const char *kw);
71 #define MAX_IMPORT_DEPTH 10
73 YY_BUFFER_STATE state;
77 } import_stack[MAX_IMPORT_DEPTH];
78 int import_stack_ptr = 0;
80 static void pop_import(void);
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) {
176 int parser_wrap(void)
187 static const struct keyword keywords[] = {
192 {"__stdcall", tSTDCALL},
193 {"_stdcall", tSTDCALL},
194 {"boolean", tBOOLEAN},
196 {"callback", tCALLBACK},
199 {"coclass", tCOCLASS},
201 {"comm_status", tCOMMSTATUS},
203 {"cpp_quote", tCPPQUOTE},
204 {"default", tDEFAULT},
205 {"dispinterface", tDISPINTERFACE},
208 {"error_status_t", tERRORSTATUST},
211 {"handle_t", tHANDLET},
214 {"importlib", tIMPORTLIB},
215 {"in_line", tINLINE},
217 {"interface", tINTERFACE},
218 {"library", tLIBRARY},
220 {"methods", tMETHODS},
222 {"properties", tPROPERTIES},
229 {"typedef", tTYPEDEF},
231 {"unsigned", tUNSIGNED},
235 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
237 /* keywords only recognized in attribute lists */
238 static const struct keyword attr_keywords[] =
240 {"aggregatable", tAGGREGATABLE},
241 {"allocate", tALLOCATE},
242 {"appobject", tAPPOBJECT},
244 {"async_uuid", tASYNCUUID},
245 {"auto_handle", tAUTOHANDLE},
246 {"bindable", tBINDABLE},
247 {"broadcast", tBROADCAST},
248 {"byte_count", tBYTECOUNT},
249 {"call_as", tCALLAS},
250 {"context_handle", tCONTEXTHANDLE},
251 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
252 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
253 {"control", tCONTROL},
254 {"defaultcollelem", tDEFAULTCOLLELEM},
255 {"defaultvalue", tDEFAULTVALUE},
256 {"defaultvtable", tDEFAULTVTABLE},
257 {"displaybind", tDISPLAYBIND},
258 {"dllname", tDLLNAME},
260 {"endpoint", tENDPOINT},
262 {"explicit_handle", tEXPLICITHANDLE},
264 {"helpcontext", tHELPCONTEXT},
265 {"helpfile", tHELPFILE},
266 {"helpstring", tHELPSTRING},
267 {"helpstringcontext", tHELPSTRINGCONTEXT},
268 {"helpstringdll", tHELPSTRINGDLL},
271 {"idempotent", tIDEMPOTENT},
273 {"immediatebind", tIMMEDIATEBIND},
274 {"implicit_handle", tIMPLICITHANDLE},
276 {"input_sync", tINPUTSYNC},
278 {"length_is", tLENGTHIS},
280 {"nonbrowsable", tNONBROWSABLE},
281 {"noncreatable", tNONCREATABLE},
282 {"nonextensible", tNONEXTENSIBLE},
285 {"oleautomation", tOLEAUTOMATION},
286 {"optional", tOPTIONAL},
288 {"pointer_default", tPOINTERDEFAULT},
289 {"propget", tPROPGET},
290 {"propput", tPROPPUT},
291 {"propputref", tPROPPUTREF},
295 {"readonly", tREADONLY},
297 {"requestedit", tREQUESTEDIT},
298 {"restricted", tRESTRICTED},
301 {"size_is", tSIZEIS},
303 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
305 {"switch_is", tSWITCHIS},
306 {"switch_type", tSWITCHTYPE},
307 {"transmit_as", tTRANSMITAS},
310 {"v1_enum", tV1ENUM},
312 {"version", tVERSION},
313 {"wire_marshal", tWIREMARSHAL},
317 #define KWP(p) ((const struct keyword *)(p))
319 static int kw_cmp_func(const void *s1, const void *s2)
321 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
324 static int kw_token(const char *kw)
326 struct keyword key, *kwp;
328 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
330 parser_lval.str = xstrdup(kwp->kw);
333 parser_lval.str = xstrdup(kw);
334 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
337 static int attr_token(const char *kw)
339 struct keyword key, *kwp;
341 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
342 sizeof(attr_keywords[0]), kw_cmp_func);
344 parser_lval.str = xstrdup(kwp->kw);
350 static void addcchar(char c)
352 if(cbufidx >= cbufalloc)
355 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
356 if(cbufalloc > 65536)
357 parser_warning("Reallocating string buffer larger than 64kB\n");
359 cbuffer[cbufidx++] = c;
362 static char *get_buffered_cstring(void)
365 return xstrdup(cbuffer);
368 static void pop_import(void)
370 int ptr = import_stack_ptr-1;
373 yy_delete_buffer( YY_CURRENT_BUFFER );
374 yy_switch_to_buffer( import_stack[ptr].state );
379 temp_name = import_stack[ptr].temp_name;
381 input_name = import_stack[ptr].input_name;
382 line_number = import_stack[ptr].line_number;
388 struct imports *next;
391 int do_import(char *fname)
394 char *hname, *path, *p;
395 struct imports *import;
396 int ptr = import_stack_ptr;
399 if (!parse_only && do_header) {
400 hname = dup_basename(fname, ".idl");
401 p = hname + strlen(hname) - 2;
402 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
404 fprintf(header, "#include <%s>\n", hname);
408 import = first_import;
409 while (import && strcmp(import->name, fname))
410 import = import->next;
411 if (import) return 0; /* already imported */
413 import = xmalloc(sizeof(struct imports));
414 import->name = xstrdup(fname);
415 import->next = first_import;
416 first_import = import;
418 /* don't search for a file name with a path in the include directories,
419 * for compatibility with MIDL */
420 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
421 path = strdup( fname );
422 else if (!(path = wpp_find_include( fname, input_name )))
423 error_loc("Unable to open include file %s\n", fname);
425 import_stack[ptr].temp_name = temp_name;
426 import_stack[ptr].input_name = input_name;
427 import_stack[ptr].line_number = line_number;
432 ret = wpp_parse_temp( path, NULL, &temp_name );
435 if((f = fopen(temp_name, "r")) == NULL)
436 error_loc("Unable to open %s\n", temp_name);
438 import_stack[ptr].state = YY_CURRENT_BUFFER;
439 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
443 void abort_import(void)
447 for (ptr=0; ptr<import_stack_ptr; ptr++)
448 unlink(import_stack[ptr].temp_name);