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]+)*
56 #include "parser.tab.h"
58 extern char *temp_name;
60 static void addcchar(char c);
61 static char *get_buffered_cstring(void);
65 static int cbufalloc = 0;
67 static int kw_token(const char *kw);
68 static int attr_token(const char *kw);
70 #define MAX_IMPORT_DEPTH 10
72 YY_BUFFER_STATE state;
76 } import_stack[MAX_IMPORT_DEPTH];
77 int import_stack_ptr = 0;
79 static void pop_import(void);
81 UUID *parse_uuid(const char *u)
83 UUID* uuid = xmalloc(sizeof(UUID));
85 /* it would be nice to use UuidFromStringA */
86 uuid->Data1 = strtoul(u, NULL, 16);
87 uuid->Data2 = strtoul(u+9, NULL, 16);
88 uuid->Data3 = strtoul(u+14, NULL, 16);
90 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
91 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
92 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
93 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
94 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
95 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
96 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
97 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
104 **************************************************************************
105 * The flexer starts here
106 **************************************************************************
109 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
114 lineno = (int)strtol(yytext, &cptr, 10);
116 parser_error("Malformed '#...' line-directive; invalid linenumber");
117 fname = strchr(cptr, '"');
119 parser_error("Malformed '#...' line-directive; missing filename");
121 cptr = strchr(fname, '"');
123 parser_error("Malformed '#...' line-directive; missing terminating \"");
125 line_number = lineno - 1; /* We didn't read the newline */
127 input_name = xstrdup(fname);
129 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
132 parser_lval.str = get_buffered_cstring();
136 <QUOTE>\\\" addcchar(yytext[1]);
137 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
138 <QUOTE>. addcchar(yytext[0]);
139 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
140 <ATTR>\] yy_pop_state(); return ']';
141 <ATTR>{cident} return attr_token(yytext);
143 parser_lval.uuid = parse_uuid(yytext);
146 <INITIAL,ATTR>{hex} {
147 parser_lval.num = strtoul(yytext, NULL, 0);
150 <INITIAL,ATTR>{int} {
151 parser_lval.num = strtoul(yytext, NULL, 0);
155 parser_lval.dbl = strtod(yytext, NULL);
158 SAFEARRAY{ws}*/\( return tSAFEARRAY;
159 {cident} return kw_token(yytext);
160 <INITIAL,ATTR>\n line_number++;
162 <INITIAL,ATTR>\<\< return SHL;
163 <INITIAL,ATTR>\>\> return SHR;
164 <INITIAL,ATTR>. return yytext[0];
166 if (import_stack_ptr) {
175 int parser_wrap(void)
186 static const struct keyword keywords[] = {
191 {"__stdcall", tSTDCALL},
192 {"_stdcall", tSTDCALL},
193 {"boolean", tBOOLEAN},
195 {"callback", tCALLBACK},
198 {"coclass", tCOCLASS},
200 {"comm_status", tCOMMSTATUS},
202 {"cpp_quote", tCPPQUOTE},
203 {"default", tDEFAULT},
204 {"dispinterface", tDISPINTERFACE},
207 {"error_status_t", tERRORSTATUST},
210 {"handle_t", tHANDLET},
213 {"importlib", tIMPORTLIB},
214 {"in_line", tINLINE},
216 {"interface", tINTERFACE},
217 {"library", tLIBRARY},
219 {"methods", tMETHODS},
221 {"properties", tPROPERTIES},
228 {"typedef", tTYPEDEF},
230 {"unsigned", tUNSIGNED},
234 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
236 /* keywords only recognized in attribute lists */
237 static const struct keyword attr_keywords[] =
239 {"aggregatable", tAGGREGATABLE},
240 {"allocate", tALLOCATE},
241 {"appobject", tAPPOBJECT},
243 {"async_uuid", tASYNCUUID},
244 {"auto_handle", tAUTOHANDLE},
245 {"bindable", tBINDABLE},
246 {"broadcast", tBROADCAST},
247 {"byte_count", tBYTECOUNT},
248 {"call_as", tCALLAS},
249 {"context_handle", tCONTEXTHANDLE},
250 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
251 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
252 {"control", tCONTROL},
253 {"defaultcollelem", tDEFAULTCOLLELEM},
254 {"defaultvalue", tDEFAULTVALUE},
255 {"defaultvtable", tDEFAULTVTABLE},
256 {"displaybind", tDISPLAYBIND},
257 {"dllname", tDLLNAME},
259 {"endpoint", tENDPOINT},
261 {"explicit_handle", tEXPLICITHANDLE},
263 {"helpcontext", tHELPCONTEXT},
264 {"helpfile", tHELPFILE},
265 {"helpstring", tHELPSTRING},
266 {"helpstringcontext", tHELPSTRINGCONTEXT},
267 {"helpstringdll", tHELPSTRINGDLL},
270 {"idempotent", tIDEMPOTENT},
272 {"immediatebind", tIMMEDIATEBIND},
273 {"implicit_handle", tIMPLICITHANDLE},
275 {"input_sync", tINPUTSYNC},
277 {"length_is", tLENGTHIS},
279 {"nonbrowsable", tNONBROWSABLE},
280 {"noncreatable", tNONCREATABLE},
281 {"nonextensible", tNONEXTENSIBLE},
284 {"oleautomation", tOLEAUTOMATION},
285 {"optional", tOPTIONAL},
287 {"pointer_default", tPOINTERDEFAULT},
288 {"propget", tPROPGET},
289 {"propput", tPROPPUT},
290 {"propputref", tPROPPUTREF},
294 {"readonly", tREADONLY},
296 {"requestedit", tREQUESTEDIT},
297 {"restricted", tRESTRICTED},
300 {"size_is", tSIZEIS},
303 {"switch_is", tSWITCHIS},
304 {"switch_type", tSWITCHTYPE},
305 {"transmit_as", tTRANSMITAS},
308 {"v1_enum", tV1ENUM},
310 {"version", tVERSION},
311 {"wire_marshal", tWIREMARSHAL},
315 #define KWP(p) ((const struct keyword *)(p))
317 static int kw_cmp_func(const void *s1, const void *s2)
319 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
322 static int kw_token(const char *kw)
324 struct keyword key, *kwp;
326 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
328 parser_lval.str = xstrdup(kwp->kw);
331 parser_lval.str = xstrdup(kw);
332 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
335 static int attr_token(const char *kw)
337 struct keyword key, *kwp;
339 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
340 sizeof(attr_keywords[0]), kw_cmp_func);
342 parser_lval.str = xstrdup(kwp->kw);
348 static void addcchar(char c)
350 if(cbufidx >= cbufalloc)
353 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
354 if(cbufalloc > 65536)
355 parser_warning("Reallocating string buffer larger than 64kB");
357 cbuffer[cbufidx++] = c;
360 static char *get_buffered_cstring(void)
363 return xstrdup(cbuffer);
366 static void pop_import(void)
368 int ptr = import_stack_ptr-1;
371 yy_delete_buffer( YY_CURRENT_BUFFER );
372 yy_switch_to_buffer( import_stack[ptr].state );
377 temp_name = import_stack[ptr].temp_name;
379 input_name = import_stack[ptr].input_name;
380 line_number = import_stack[ptr].line_number;
386 struct imports *next;
389 int do_import(char *fname)
392 char *hname, *path, *p;
393 struct imports *import;
394 int ptr = import_stack_ptr;
397 if (!parse_only && do_header) {
398 hname = dup_basename(fname, ".idl");
399 p = hname + strlen(hname) - 2;
400 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
402 fprintf(header, "#include <%s>\n", hname);
406 import = first_import;
407 while (import && strcmp(import->name, fname))
408 import = import->next;
409 if (import) return 0; /* already imported */
411 import = xmalloc(sizeof(struct imports));
412 import->name = xstrdup(fname);
413 import->next = first_import;
414 first_import = import;
416 if (!(path = wpp_find_include( fname, input_name )))
417 parser_error("Unable to open include file %s", fname);
419 import_stack[ptr].temp_name = temp_name;
420 import_stack[ptr].input_name = input_name;
421 import_stack[ptr].line_number = line_number;
426 ret = wpp_parse_temp( path, NULL, &temp_name );
429 if((f = fopen(temp_name, "r")) == NULL)
430 parser_error("Unable to open %s", temp_name);
432 import_stack[ptr].state = YY_CURRENT_BUFFER;
433 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
437 void abort_import(void)
441 for (ptr=0; ptr<import_stack_ptr; ptr++)
442 unlink(import_stack[ptr].temp_name);