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}
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 parser_error("Malformed '#...' line-directive; invalid linenumber");
114 fname = strchr(cptr, '"');
116 parser_error("Malformed '#...' line-directive; missing filename");
118 cptr = strchr(fname, '"');
120 parser_error("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 parser_lval.str = get_buffered_cstring();
133 <QUOTE>\\\" addcchar(yytext[1]);
134 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
135 <QUOTE>. addcchar(yytext[0]);
137 parser_lval.uuid = parse_uuid(yytext);
141 parser_lval.num = strtoul(yytext, NULL, 0);
145 parser_lval.num = strtoul(yytext, NULL, 0);
148 SAFEARRAY{ws}*/\( return tSAFEARRAY;
149 {cident} return kw_token(yytext);
156 if (import_stack_ptr) {
165 int parser_wrap(void)
171 static struct keyword {
179 {"__stdcall", tSTDCALL},
180 {"_stdcall", tSTDCALL},
181 {"aggregatable", tAGGREGATABLE},
182 {"allocate", tALLOCATE},
183 {"appobject", tAPPOBJECT},
185 {"async_uuid", tASYNCUUID},
186 {"auto_handle", tAUTOHANDLE},
187 {"bindable", tBINDABLE},
188 {"boolean", tBOOLEAN},
189 {"broadcast", tBROADCAST},
191 {"byte_count", tBYTECOUNT},
192 {"call_as", tCALLAS},
193 {"callback", tCALLBACK},
196 {"coclass", tCOCLASS},
198 {"comm_status", tCOMMSTATUS},
200 {"context_handle", tCONTEXTHANDLE},
201 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
202 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
203 {"control", tCONTROL},
204 {"cpp_quote", tCPPQUOTE},
206 {"default", tDEFAULT},
207 {"defaultcollelem", tDEFAULTCOLLELEM},
208 {"defaultvalue", tDEFAULTVALUE},
209 {"defaultvtable", tDEFAULTVTABLE},
210 {"dispinterface", tDISPINTERFACE},
211 {"displaybind", tDISPLAYBIND},
212 {"dllname", tDLLNAME},
215 {"endpoint", tENDPOINT},
218 {"error_status_t", tERRORSTATUST},
219 {"explicit_handle", tEXPLICITHANDLE},
223 {"handle_t", tHANDLET},
224 {"helpcontext", tHELPCONTEXT},
225 {"helpfile", tHELPFILE},
226 {"helpstring", tHELPSTRING},
227 {"helpstringcontext", tHELPSTRINGCONTEXT},
228 {"helpstringdll", tHELPSTRINGDLL},
232 {"idempotent", tIDEMPOTENT},
235 {"immediatebind", tIMMEDIATEBIND},
236 {"implicit_handle", tIMPLICITHANDLE},
238 {"importlib", tIMPORTLIB},
240 {"in_line", tINLINE},
241 {"input_sync", tINPUTSYNC},
244 {"interface", tINTERFACE},
246 {"length_is", tLENGTHIS},
247 {"library", tLIBRARY},
252 {"methods", tMETHODS},
256 {"nonbrowsable", tNONBROWSABLE},
257 {"noncreatable", tNONCREATABLE},
258 {"nonextensible", tNONEXTENSIBLE},
261 {"oleautomation", tOLEAUTOMATION},
263 {"optional", tOPTIONAL},
266 {"pointer_default", tPOINTERDEFAULT},
268 {"properties", tPROPERTIES},
269 {"propget", tPROPGET},
270 {"propput", tPROPPUT},
271 {"propputref", tPROPPUTREF},
277 {"readonly", tREADONLY},
279 {"requestedit", tREQUESTEDIT},
280 {"restricted", tRESTRICTED},
286 {"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 parser_lval.str = xstrdup(kwp->kw);
343 parser_lval.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 parser_warning("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, input_name )))
416 parser_error("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 parser_error("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);