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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 %option never-interactive
26 cident [a-zA-Z_][0-9a-zA-Z_]*
30 uuid {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
57 #define YY_NO_TOP_STATE
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);
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 static 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>^{ws}*\#{ws}* yy_push_state(pp_line);
114 lineno = (int)strtol(yytext, &cptr, 10);
116 yyerror("Malformed '#...' line-directive; invalid linenumber");
117 fname = strchr(cptr, '"');
119 yyerror("Malformed '#...' line-directive; missing filename");
121 cptr = strchr(fname, '"');
123 yyerror("Malformed '#...' line-directive; missing terminating \"");
125 line_number = lineno - 1; /* We didn't read the newline */
127 input_name = xstrdup(fname);
129 \" yy_push_state(QUOTE); cbufidx = 0;
132 yylval.str = get_buffered_cstring();
136 <QUOTE>\\\" addcchar(yytext[1]);
137 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
138 <QUOTE>. addcchar(yytext[0]);
140 yylval.uuid = parse_uuid(yytext);
144 yylval.num = strtoul(yytext, NULL, 0);
148 yylval.num = strtoul(yytext, NULL, 0);
151 {cident} return kw_token(yytext);
158 if (import_stack_ptr) {
173 static struct keyword {
180 {"__stdcall", tSTDCALL},
181 {"_stdcall", tSTDCALL},
182 {"aggregatable", tAGGREGATABLE},
183 {"allocate", tALLOCATE},
184 {"appobject", tAPPOBJECT},
187 {"async_uuid", tASYNCUUID},
188 {"auto_handle", tAUTOHANDLE},
189 {"bindable", tBINDABLE},
190 {"boolean", tBOOLEAN},
191 {"broadcast", tBROADCAST},
193 {"byte_count", tBYTECOUNT},
194 {"call_as", tCALLAS},
195 {"callback", tCALLBACK},
198 {"coclass", tCOCLASS},
200 {"comm_status", tCOMMSTATUS},
202 {"context_handle", tCONTEXTHANDLE},
203 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
204 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
205 {"control", tCONTROL},
206 {"cpp_quote", tCPPQUOTE},
208 {"default", tDEFAULT},
209 {"defaultvalue", tDEFAULTVALUE},
211 {"dispinterface", tDISPINTERFACE},
213 {"dllname", tDLLNAME},
217 {"endpoint", tENDPOINT},
220 {"error_status_t", tERRORSTATUST},
227 {"handle_t", tHANDLET},
229 {"helpcontext", tHELPCONTEXT},
230 {"helpfile", tHELPFILE},
231 {"helpstring", tHELPSTRING},
232 {"helpstringcontext", tHELPSTRINGCONTEXT},
233 {"helpstringdll", tHELPSTRINGDLL},
238 {"idempotent", tIDEMPOTENT},
243 {"importlib", tIMPORTLIB},
245 {"include", tINCLUDE},
246 {"in_line", tINLINE},
247 {"input_sync", tINPUTSYNC},
250 {"interface", tINTERFACE},
252 {"length_is", tLENGTHIS},
253 {"library", tLIBRARY},
258 {"methods", tMETHODS},
262 {"noncreatable", tNONCREATABLE},
265 {"oleautomation", tOLEAUTOMATION},
267 {"optional", tOPTIONAL},
270 {"pointer_default", tPOINTERDEFAULT},
272 {"properties", tPROPERTIES},
273 {"propget", tPROPGET},
274 {"propput", tPROPPUT},
275 {"propputref", tPROPPUTREF},
279 {"readonly", tREADONLY},
282 {"restricted", tRESTRICTED},
287 {"size_is", tSIZEIS},
295 {"switch_is", tSWITCHIS},
296 {"switch_type", tSWITCHTYPE},
298 {"transmit_as", tTRANSMITAS},
299 {"typedef", tTYPEDEF},
303 {"unsigned", tUNSIGNED},
306 {"v1_enum", tV1ENUM},
309 {"version", tVERSION},
312 {"wire_marshal", tWIREMARSHAL}
314 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
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);
323 static int kw_token(const char *kw)
325 struct keyword key, *kwp;
328 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
332 for (kwp=NULL, i=0; i < NKEYWORDS; i++)
333 if (!kw_cmp_func(&key, &keywords[i])) {
340 yylval.str = (char*)kwp->kw;
343 yylval.str = xstrdup(kw);
344 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
347 static void addcchar(char c)
349 if(cbufidx >= cbufalloc)
352 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
353 if(cbufalloc > 65536)
354 yywarning("Reallocating string buffer larger than 64kB");
356 cbuffer[cbufidx++] = c;
359 static char *get_buffered_cstring(void)
362 return xstrdup(cbuffer);
365 static void pop_import(void)
367 int ptr = import_stack_ptr-1;
370 yy_delete_buffer( YY_CURRENT_BUFFER );
371 yy_switch_to_buffer( import_stack[ptr].state );
376 temp_name = import_stack[ptr].temp_name;
378 input_name = import_stack[ptr].input_name;
379 line_number = import_stack[ptr].line_number;
385 struct imports *next;
388 int do_import(char *fname)
391 char *hname, *path, *p;
392 struct imports *import;
393 int ptr = import_stack_ptr;
396 if (!parse_only && do_header) {
397 hname = dup_basename(fname, ".idl");
398 p = hname + strlen(hname) - 2;
399 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
401 fprintf(header, "#include <%s>\n", hname);
405 import = first_import;
406 while (import && strcmp(import->name, fname))
407 import = import->next;
408 if (import) return 0; /* already imported */
410 import = xmalloc(sizeof(struct imports));
411 import->name = xstrdup(fname);
412 import->next = first_import;
413 first_import = import;
415 if (!(path = wpp_find_include( fname, 1 )))
416 yyerror("Unable to open include file %s", fname);
418 import_stack[ptr].temp_name = temp_name;
419 import_stack[ptr].input_name = input_name;
420 import_stack[ptr].line_number = line_number;
425 ret = wpp_parse_temp( path, NULL, &temp_name );
428 if((f = fopen(temp_name, "r")) == NULL)
429 yyerror("Unable to open %s", temp_name);
431 import_stack[ptr].state = YY_CURRENT_BUFFER;
432 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
436 void abort_import(void)
440 for (ptr=0; ptr<import_stack_ptr; ptr++)
441 unlink(import_stack[ptr].temp_name);