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 nounput noyy_top_state
23 %option 8bit never-interactive
27 cident [a-zA-Z_][0-9a-zA-Z_]*
31 uuid {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
54 #include "parser.tab.h"
58 #define YY_NO_TOP_STATE
60 extern char *temp_name;
62 static void addcchar(char c);
63 static char *get_buffered_cstring(void);
67 static int cbufalloc = 0;
69 static int kw_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 static 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>^{ws}*\#{ws}* yy_push_state(pp_line);
115 lineno = (int)strtol(yytext, &cptr, 10);
117 yyerror("Malformed '#...' line-directive; invalid linenumber");
118 fname = strchr(cptr, '"');
120 yyerror("Malformed '#...' line-directive; missing filename");
122 cptr = strchr(fname, '"');
124 yyerror("Malformed '#...' line-directive; missing terminating \"");
126 line_number = lineno - 1; /* We didn't read the newline */
128 input_name = xstrdup(fname);
130 \" yy_push_state(QUOTE); cbufidx = 0;
133 yylval.str = get_buffered_cstring();
137 <QUOTE>\\\" addcchar(yytext[1]);
138 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
139 <QUOTE>. addcchar(yytext[0]);
141 yylval.uuid = parse_uuid(yytext);
145 yylval.num = strtoul(yytext, NULL, 0);
149 yylval.num = strtoul(yytext, NULL, 0);
152 {cident} return kw_token(yytext);
159 if (import_stack_ptr) {
174 static struct keyword {
181 {"__stdcall", tSTDCALL},
182 {"_stdcall", tSTDCALL},
183 {"aggregatable", tAGGREGATABLE},
184 {"allocate", tALLOCATE},
185 {"appobject", tAPPOBJECT},
188 {"async_uuid", tASYNCUUID},
189 {"auto_handle", tAUTOHANDLE},
190 {"bindable", tBINDABLE},
191 {"boolean", tBOOLEAN},
192 {"broadcast", tBROADCAST},
194 {"byte_count", tBYTECOUNT},
195 {"call_as", tCALLAS},
196 {"callback", tCALLBACK},
199 {"coclass", tCOCLASS},
201 {"comm_status", tCOMMSTATUS},
203 {"context_handle", tCONTEXTHANDLE},
204 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
205 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
206 {"control", tCONTROL},
207 {"cpp_quote", tCPPQUOTE},
209 {"default", tDEFAULT},
210 {"defaultvalue", tDEFAULTVALUE},
212 {"dispinterface", tDISPINTERFACE},
214 {"displaybind", tDISPLAYBIND},
215 {"dllname", tDLLNAME},
219 {"endpoint", tENDPOINT},
222 {"error_status_t", tERRORSTATUST},
223 {"explicit_handle", tEXPLICITHANDLE},
229 {"handle_t", tHANDLET},
231 {"helpcontext", tHELPCONTEXT},
232 {"helpfile", tHELPFILE},
233 {"helpstring", tHELPSTRING},
234 {"helpstringcontext", tHELPSTRINGCONTEXT},
235 {"helpstringdll", tHELPSTRINGDLL},
240 {"idempotent", tIDEMPOTENT},
244 {"implicit_handle", tIMPLICITHANDLE},
246 {"importlib", tIMPORTLIB},
248 {"include", tINCLUDE},
249 {"in_line", tINLINE},
250 {"input_sync", tINPUTSYNC},
253 {"interface", tINTERFACE},
255 {"length_is", tLENGTHIS},
256 {"library", tLIBRARY},
261 {"methods", tMETHODS},
265 {"noncreatable", tNONCREATABLE},
268 {"oleautomation", tOLEAUTOMATION},
270 {"optional", tOPTIONAL},
273 {"pointer_default", tPOINTERDEFAULT},
275 {"properties", tPROPERTIES},
276 {"propget", tPROPGET},
277 {"propput", tPROPPUT},
278 {"propputref", tPROPPUTREF},
284 {"readonly", tREADONLY},
287 {"restricted", tRESTRICTED},
293 {"size_is", tSIZEIS},
302 {"switch_is", tSWITCHIS},
303 {"switch_type", tSWITCHTYPE},
305 {"transmit_as", tTRANSMITAS},
306 {"typedef", tTYPEDEF},
310 {"unsigned", tUNSIGNED},
313 {"v1_enum", tV1ENUM},
316 {"version", tVERSION},
319 {"wire_marshal", tWIREMARSHAL}
321 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
322 #define KWP(p) ((const struct keyword *)(p))
324 static int kw_cmp_func(const void *s1, const void *s2)
326 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
330 static int kw_token(const char *kw)
332 struct keyword key, *kwp;
335 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
339 for (kwp=NULL, i=0; i < NKEYWORDS; i++)
340 if (!kw_cmp_func(&key, &keywords[i])) {
347 yylval.str = (char*)kwp->kw;
350 yylval.str = xstrdup(kw);
351 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
354 static void addcchar(char c)
356 if(cbufidx >= cbufalloc)
359 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
360 if(cbufalloc > 65536)
361 yywarning("Reallocating string buffer larger than 64kB");
363 cbuffer[cbufidx++] = c;
366 static char *get_buffered_cstring(void)
369 return xstrdup(cbuffer);
372 static void pop_import(void)
374 int ptr = import_stack_ptr-1;
377 yy_delete_buffer( YY_CURRENT_BUFFER );
378 yy_switch_to_buffer( import_stack[ptr].state );
383 temp_name = import_stack[ptr].temp_name;
385 input_name = import_stack[ptr].input_name;
386 line_number = import_stack[ptr].line_number;
392 struct imports *next;
395 int do_import(char *fname)
398 char *hname, *path, *p;
399 struct imports *import;
400 int ptr = import_stack_ptr;
403 if (!parse_only && do_header) {
404 hname = dup_basename(fname, ".idl");
405 p = hname + strlen(hname) - 2;
406 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
408 fprintf(header, "#include <%s>\n", hname);
412 import = first_import;
413 while (import && strcmp(import->name, fname))
414 import = import->next;
415 if (import) return 0; /* already imported */
417 import = xmalloc(sizeof(struct imports));
418 import->name = xstrdup(fname);
419 import->next = first_import;
420 first_import = import;
422 if (!(path = wpp_find_include( fname, input_name )))
423 yyerror("Unable to open include file %s", 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 yyerror("Unable to open %s", 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);