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"
56 extern char *temp_name;
58 static void addcchar(char c);
59 static char *get_buffered_cstring(void);
63 static int cbufalloc = 0;
65 static int kw_token(const char *kw);
67 #define MAX_IMPORT_DEPTH 10
69 YY_BUFFER_STATE state;
73 } import_stack[MAX_IMPORT_DEPTH];
74 int import_stack_ptr = 0;
76 static void pop_import(void);
78 static UUID* parse_uuid(const char*u)
80 UUID* uuid = xmalloc(sizeof(UUID));
82 /* it would be nice to use UuidFromStringA */
83 uuid->Data1 = strtoul(u, NULL, 16);
84 uuid->Data2 = strtoul(u+9, NULL, 16);
85 uuid->Data3 = strtoul(u+14, NULL, 16);
87 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
88 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
89 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
90 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
91 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
92 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
93 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
94 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
101 **************************************************************************
102 * The flexer starts here
103 **************************************************************************
106 <INITIAL>^{ws}*\#{ws}* yy_push_state(pp_line);
111 lineno = (int)strtol(yytext, &cptr, 10);
113 yyerror("Malformed '#...' line-directive; invalid linenumber");
114 fname = strchr(cptr, '"');
116 yyerror("Malformed '#...' line-directive; missing filename");
118 cptr = strchr(fname, '"');
120 yyerror("Malformed '#...' line-directive; missing terminating \"");
122 line_number = lineno - 1; /* We didn't read the newline */
124 input_name = xstrdup(fname);
126 \" yy_push_state(QUOTE); cbufidx = 0;
129 yylval.str = get_buffered_cstring();
133 <QUOTE>\\\" addcchar(yytext[1]);
134 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
135 <QUOTE>. addcchar(yytext[0]);
137 yylval.uuid = parse_uuid(yytext);
141 yylval.num = strtoul(yytext, NULL, 0);
145 yylval.num = strtoul(yytext, NULL, 0);
148 {cident} return kw_token(yytext);
155 if (import_stack_ptr) {
170 static struct keyword {
177 {"__stdcall", tSTDCALL},
178 {"_stdcall", tSTDCALL},
179 {"aggregatable", tAGGREGATABLE},
180 {"allocate", tALLOCATE},
181 {"appobject", tAPPOBJECT},
184 {"async_uuid", tASYNCUUID},
185 {"auto_handle", tAUTOHANDLE},
186 {"bindable", tBINDABLE},
187 {"boolean", tBOOLEAN},
188 {"broadcast", tBROADCAST},
190 {"byte_count", tBYTECOUNT},
191 {"call_as", tCALLAS},
192 {"callback", tCALLBACK},
195 {"coclass", tCOCLASS},
197 {"comm_status", tCOMMSTATUS},
199 {"context_handle", tCONTEXTHANDLE},
200 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
201 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
202 {"control", tCONTROL},
203 {"cpp_quote", tCPPQUOTE},
205 {"default", tDEFAULT},
206 {"defaultvalue", tDEFAULTVALUE},
208 {"dispinterface", tDISPINTERFACE},
210 {"displaybind", tDISPLAYBIND},
211 {"dllname", tDLLNAME},
215 {"endpoint", tENDPOINT},
218 {"error_status_t", tERRORSTATUST},
219 {"explicit_handle", tEXPLICITHANDLE},
225 {"handle_t", tHANDLET},
227 {"helpcontext", tHELPCONTEXT},
228 {"helpfile", tHELPFILE},
229 {"helpstring", tHELPSTRING},
230 {"helpstringcontext", tHELPSTRINGCONTEXT},
231 {"helpstringdll", tHELPSTRINGDLL},
236 {"idempotent", tIDEMPOTENT},
240 {"implicit_handle", tIMPLICITHANDLE},
242 {"importlib", tIMPORTLIB},
244 {"include", tINCLUDE},
245 {"in_line", tINLINE},
246 {"input_sync", tINPUTSYNC},
249 {"interface", tINTERFACE},
251 {"length_is", tLENGTHIS},
252 {"library", tLIBRARY},
257 {"methods", tMETHODS},
261 {"noncreatable", tNONCREATABLE},
264 {"oleautomation", tOLEAUTOMATION},
266 {"optional", tOPTIONAL},
269 {"pointer_default", tPOINTERDEFAULT},
271 {"properties", tPROPERTIES},
272 {"propget", tPROPGET},
273 {"propput", tPROPPUT},
274 {"propputref", tPROPPUTREF},
280 {"readonly", tREADONLY},
283 {"restricted", tRESTRICTED},
289 {"size_is", tSIZEIS},
298 {"switch_is", tSWITCHIS},
299 {"switch_type", tSWITCHTYPE},
301 {"transmit_as", tTRANSMITAS},
302 {"typedef", tTYPEDEF},
306 {"unsigned", tUNSIGNED},
309 {"v1_enum", tV1ENUM},
312 {"version", tVERSION},
315 {"wire_marshal", tWIREMARSHAL}
317 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
318 #define KWP(p) ((const struct keyword *)(p))
320 static int kw_cmp_func(const void *s1, const void *s2)
322 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
326 static int kw_token(const char *kw)
328 struct keyword key, *kwp;
331 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
335 for (kwp=NULL, i=0; i < NKEYWORDS; i++)
336 if (!kw_cmp_func(&key, &keywords[i])) {
343 yylval.str = (char*)kwp->kw;
346 yylval.str = xstrdup(kw);
347 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
350 static void addcchar(char c)
352 if(cbufidx >= cbufalloc)
355 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
356 if(cbufalloc > 65536)
357 yywarning("Reallocating string buffer larger than 64kB");
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 if (!(path = wpp_find_include( fname, input_name )))
419 yyerror("Unable to open include file %s", fname);
421 import_stack[ptr].temp_name = temp_name;
422 import_stack[ptr].input_name = input_name;
423 import_stack[ptr].line_number = line_number;
428 ret = wpp_parse_temp( path, NULL, &temp_name );
431 if((f = fopen(temp_name, "r")) == NULL)
432 yyerror("Unable to open %s", temp_name);
434 import_stack[ptr].state = YY_CURRENT_BUFFER;
435 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
439 void abort_import(void)
443 for (ptr=0; ptr<import_stack_ptr; ptr++)
444 unlink(import_stack[ptr].temp_name);