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}
55 #include "parser.tab.h"
57 extern char *temp_name;
59 static void addcchar(char c);
60 static char *get_buffered_cstring(void);
64 static int cbufalloc = 0;
66 static int kw_token(const char *kw);
67 static int attr_token(const char *kw);
69 #define MAX_IMPORT_DEPTH 10
71 YY_BUFFER_STATE state;
75 } import_stack[MAX_IMPORT_DEPTH];
76 int import_stack_ptr = 0;
78 static void pop_import(void);
80 static UUID* parse_uuid(const char*u)
82 UUID* uuid = xmalloc(sizeof(UUID));
84 /* it would be nice to use UuidFromStringA */
85 uuid->Data1 = strtoul(u, NULL, 16);
86 uuid->Data2 = strtoul(u+9, NULL, 16);
87 uuid->Data3 = strtoul(u+14, NULL, 16);
89 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
90 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
91 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
92 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
93 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
94 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
95 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
96 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
103 **************************************************************************
104 * The flexer starts here
105 **************************************************************************
108 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
113 lineno = (int)strtol(yytext, &cptr, 10);
115 parser_error("Malformed '#...' line-directive; invalid linenumber");
116 fname = strchr(cptr, '"');
118 parser_error("Malformed '#...' line-directive; missing filename");
120 cptr = strchr(fname, '"');
122 parser_error("Malformed '#...' line-directive; missing terminating \"");
124 line_number = lineno - 1; /* We didn't read the newline */
126 input_name = xstrdup(fname);
128 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
131 parser_lval.str = get_buffered_cstring();
135 <QUOTE>\\\" addcchar(yytext[1]);
136 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
137 <QUOTE>. addcchar(yytext[0]);
138 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
139 <ATTR>\] yy_pop_state(); return ']';
140 <ATTR>{cident} return attr_token(yytext);
142 parser_lval.uuid = parse_uuid(yytext);
145 <INITIAL,ATTR>{hex} {
146 parser_lval.num = strtoul(yytext, NULL, 0);
149 <INITIAL,ATTR>{int} {
150 parser_lval.num = strtoul(yytext, NULL, 0);
153 SAFEARRAY{ws}*/\( return tSAFEARRAY;
154 {cident} return kw_token(yytext);
155 <INITIAL,ATTR>\n line_number++;
157 <INITIAL,ATTR>\<\< return SHL;
158 <INITIAL,ATTR>\>\> return SHR;
159 <INITIAL,ATTR>. return yytext[0];
161 if (import_stack_ptr) {
170 int parser_wrap(void)
181 static const struct keyword keywords[] = {
186 {"__stdcall", tSTDCALL},
187 {"_stdcall", tSTDCALL},
188 {"boolean", tBOOLEAN},
190 {"callback", tCALLBACK},
193 {"coclass", tCOCLASS},
195 {"comm_status", tCOMMSTATUS},
197 {"control", tCONTROL},
198 {"cpp_quote", tCPPQUOTE},
199 {"default", tDEFAULT},
200 {"dispinterface", tDISPINTERFACE},
203 {"error_status_t", tERRORSTATUST},
206 {"handle_t", tHANDLET},
209 {"importlib", tIMPORTLIB},
210 {"in_line", tINLINE},
212 {"interface", tINTERFACE},
213 {"library", tLIBRARY},
215 {"methods", tMETHODS},
217 {"properties", tPROPERTIES},
224 {"typedef", tTYPEDEF},
226 {"unsigned", tUNSIGNED},
230 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
232 /* keywords only recognized in attribute lists */
233 static const struct keyword attr_keywords[] =
235 {"aggregatable", tAGGREGATABLE},
236 {"allocate", tALLOCATE},
237 {"appobject", tAPPOBJECT},
239 {"async_uuid", tASYNCUUID},
240 {"auto_handle", tAUTOHANDLE},
241 {"bindable", tBINDABLE},
242 {"broadcast", tBROADCAST},
243 {"byte_count", tBYTECOUNT},
244 {"call_as", tCALLAS},
245 {"context_handle", tCONTEXTHANDLE},
246 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
247 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
248 {"defaultcollelem", tDEFAULTCOLLELEM},
249 {"defaultvalue", tDEFAULTVALUE},
250 {"defaultvtable", tDEFAULTVTABLE},
251 {"displaybind", tDISPLAYBIND},
252 {"dllname", tDLLNAME},
254 {"endpoint", tENDPOINT},
256 {"explicit_handle", tEXPLICITHANDLE},
258 {"helpcontext", tHELPCONTEXT},
259 {"helpfile", tHELPFILE},
260 {"helpstring", tHELPSTRING},
261 {"helpstringcontext", tHELPSTRINGCONTEXT},
262 {"helpstringdll", tHELPSTRINGDLL},
265 {"idempotent", tIDEMPOTENT},
267 {"immediatebind", tIMMEDIATEBIND},
268 {"implicit_handle", tIMPLICITHANDLE},
270 {"input_sync", tINPUTSYNC},
272 {"length_is", tLENGTHIS},
274 {"nonbrowsable", tNONBROWSABLE},
275 {"noncreatable", tNONCREATABLE},
276 {"nonextensible", tNONEXTENSIBLE},
279 {"oleautomation", tOLEAUTOMATION},
280 {"optional", tOPTIONAL},
282 {"pointer_default", tPOINTERDEFAULT},
283 {"propget", tPROPGET},
284 {"propput", tPROPPUT},
285 {"propputref", tPROPPUTREF},
289 {"readonly", tREADONLY},
291 {"requestedit", tREQUESTEDIT},
292 {"restricted", tRESTRICTED},
295 {"size_is", tSIZEIS},
298 {"switch_is", tSWITCHIS},
299 {"switch_type", tSWITCHTYPE},
300 {"transmit_as", tTRANSMITAS},
303 {"v1_enum", tV1ENUM},
305 {"version", tVERSION},
306 {"wire_marshal", tWIREMARSHAL},
310 #define KWP(p) ((const struct keyword *)(p))
312 static int kw_cmp_func(const void *s1, const void *s2)
314 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
317 static int kw_token(const char *kw)
319 struct keyword key, *kwp;
321 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
323 parser_lval.str = xstrdup(kwp->kw);
326 parser_lval.str = xstrdup(kw);
327 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
330 static int attr_token(const char *kw)
332 struct keyword key, *kwp;
334 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
335 sizeof(attr_keywords[0]), kw_cmp_func);
337 parser_lval.str = xstrdup(kwp->kw);
343 static void addcchar(char c)
345 if(cbufidx >= cbufalloc)
348 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
349 if(cbufalloc > 65536)
350 parser_warning("Reallocating string buffer larger than 64kB");
352 cbuffer[cbufidx++] = c;
355 static char *get_buffered_cstring(void)
358 return xstrdup(cbuffer);
361 static void pop_import(void)
363 int ptr = import_stack_ptr-1;
366 yy_delete_buffer( YY_CURRENT_BUFFER );
367 yy_switch_to_buffer( import_stack[ptr].state );
372 temp_name = import_stack[ptr].temp_name;
374 input_name = import_stack[ptr].input_name;
375 line_number = import_stack[ptr].line_number;
381 struct imports *next;
384 int do_import(char *fname)
387 char *hname, *path, *p;
388 struct imports *import;
389 int ptr = import_stack_ptr;
392 if (!parse_only && do_header) {
393 hname = dup_basename(fname, ".idl");
394 p = hname + strlen(hname) - 2;
395 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
397 fprintf(header, "#include <%s>\n", hname);
401 import = first_import;
402 while (import && strcmp(import->name, fname))
403 import = import->next;
404 if (import) return 0; /* already imported */
406 import = xmalloc(sizeof(struct imports));
407 import->name = xstrdup(fname);
408 import->next = first_import;
409 first_import = import;
411 if (!(path = wpp_find_include( fname, input_name )))
412 parser_error("Unable to open include file %s", fname);
414 import_stack[ptr].temp_name = temp_name;
415 import_stack[ptr].input_name = input_name;
416 import_stack[ptr].line_number = line_number;
421 ret = wpp_parse_temp( path, NULL, &temp_name );
424 if((f = fopen(temp_name, "r")) == NULL)
425 parser_error("Unable to open %s", temp_name);
427 import_stack[ptr].state = YY_CURRENT_BUFFER;
428 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
432 void abort_import(void)
436 for (ptr=0; ptr<import_stack_ptr; ptr++)
437 unlink(import_stack[ptr].temp_name);