94eec0d520061c8f28d2b83b681f6f2f67bee258
[reactos.git] / reactos / tools / widl / parser.l
1 /* -*-C-*-
2 * IDL Compiler
3 *
4 * Copyright 2002 Ove Kaaven
5 *
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.
10 *
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.
15 *
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
19 */
20
21 %option stack
22 %option nounput noyy_top_state
23 %option 8bit never-interactive prefix="parser_"
24
25 nl \r?\n
26 ws [ \f\t\r]
27 cident [a-zA-Z_][0-9a-zA-Z_]*
28 int [0-9]+
29 hexd [0-9a-fA-F]
30 hex 0x{hexd}+
31 uuid {hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
32 double [0-9]+\.[0-9]+([eE][+-]?[0-9]+)*
33
34 %x QUOTE
35 %x ATTR
36 %x PP_LINE
37
38 %{
39
40 #include "config.h"
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.h>
46 #include <assert.h>
47
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #else
51 #define YY_NO_UNISTD_H
52 #endif
53
54 #include "widl.h"
55 #include "utils.h"
56 #include "parser.h"
57 #include "wine/wpp.h"
58
59 #include "parser.tab.h"
60
61 extern char *temp_name;
62
63 static void addcchar(char c);
64 static char *get_buffered_cstring(void);
65
66 static char *cbuffer;
67 static int cbufidx;
68 static int cbufalloc = 0;
69
70 static int kw_token(const char *kw);
71 static int attr_token(const char *kw);
72
73 #define MAX_IMPORT_DEPTH 10
74 struct {
75 YY_BUFFER_STATE state;
76 char *input_name;
77 int line_number;
78 char *temp_name;
79 } import_stack[MAX_IMPORT_DEPTH];
80 int import_stack_ptr = 0;
81
82 static void pop_import(void);
83
84 UUID *parse_uuid(const char *u)
85 {
86 UUID* uuid = xmalloc(sizeof(UUID));
87 char b[3];
88 /* it would be nice to use UuidFromStringA */
89 uuid->Data1 = strtoul(u, NULL, 16);
90 uuid->Data2 = strtoul(u+9, NULL, 16);
91 uuid->Data3 = strtoul(u+14, NULL, 16);
92 b[2] = 0;
93 memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
94 memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
95 memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
96 memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
97 memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
98 memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
99 memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
100 memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
101 return uuid;
102 }
103
104 %}
105
106 /*
107 **************************************************************************
108 * The flexer starts here
109 **************************************************************************
110 */
111 %%
112 <INITIAL,ATTR>^{ws}*\#{ws}* yy_push_state(PP_LINE);
113 <PP_LINE>[^\n]* {
114 int lineno;
115 char *cptr, *fname;
116 yy_pop_state();
117 lineno = (int)strtol(yytext, &cptr, 10);
118 if(!lineno)
119 error_loc("Malformed '#...' line-directive; invalid linenumber\n");
120 fname = strchr(cptr, '"');
121 if(!fname)
122 error_loc("Malformed '#...' line-directive; missing filename\n");
123 fname++;
124 cptr = strchr(fname, '"');
125 if(!cptr)
126 error_loc("Malformed '#...' line-directive; missing terminating \"\n");
127 *cptr = '\0';
128 line_number = lineno - 1; /* We didn't read the newline */
129 free( input_name );
130 input_name = xstrdup(fname);
131 }
132 <INITIAL,ATTR>\" yy_push_state(QUOTE); cbufidx = 0;
133 <QUOTE>\" {
134 yy_pop_state();
135 parser_lval.str = get_buffered_cstring();
136 return aSTRING;
137 }
138 <QUOTE>\\\\ |
139 <QUOTE>\\\" addcchar(yytext[1]);
140 <QUOTE>\\. addcchar('\\'); addcchar(yytext[1]);
141 <QUOTE>. addcchar(yytext[0]);
142 <INITIAL,ATTR>\[ yy_push_state(ATTR); return '[';
143 <ATTR>\] yy_pop_state(); return ']';
144 <ATTR>{cident} return attr_token(yytext);
145 <ATTR>{uuid} {
146 parser_lval.uuid = parse_uuid(yytext);
147 return aUUID;
148 }
149 <INITIAL,ATTR>{hex} {
150 parser_lval.num = strtoul(yytext, NULL, 0);
151 return aHEXNUM;
152 }
153 <INITIAL,ATTR>{int} {
154 parser_lval.num = strtoul(yytext, NULL, 0);
155 return aNUM;
156 }
157 <INITIAL>{double} {
158 parser_lval.dbl = strtod(yytext, NULL);
159 return aDOUBLE;
160 }
161 SAFEARRAY{ws}*/\( return tSAFEARRAY;
162 {cident} return kw_token(yytext);
163 <INITIAL,ATTR>\n line_number++;
164 <INITIAL,ATTR>{ws}
165 <INITIAL,ATTR>\<\< return SHL;
166 <INITIAL,ATTR>\>\> return SHR;
167 <INITIAL,ATTR>. return yytext[0];
168 <<EOF>> {
169 if (import_stack_ptr) {
170 pop_import();
171 return aEOF;
172 }
173 else yyterminate();
174 }
175 %%
176
177 #ifndef parser_wrap
178 int parser_wrap(void)
179 {
180 return 1;
181 }
182 #endif
183
184 struct keyword {
185 const char *kw;
186 int token;
187 };
188
189 static const struct keyword keywords[] = {
190 {"FALSE", tFALSE},
191 {"TRUE", tTRUE},
192 {"__cdecl", tCDECL},
193 {"__int64", tINT64},
194 {"__stdcall", tSTDCALL},
195 {"_stdcall", tSTDCALL},
196 {"boolean", tBOOLEAN},
197 {"byte", tBYTE},
198 {"callback", tCALLBACK},
199 {"case", tCASE},
200 {"char", tCHAR},
201 {"coclass", tCOCLASS},
202 {"code", tCODE},
203 {"comm_status", tCOMMSTATUS},
204 {"const", tCONST},
205 {"cpp_quote", tCPPQUOTE},
206 {"default", tDEFAULT},
207 {"dispinterface", tDISPINTERFACE},
208 {"double", tDOUBLE},
209 {"enum", tENUM},
210 {"error_status_t", tERRORSTATUST},
211 {"extern", tEXTERN},
212 {"float", tFLOAT},
213 {"handle_t", tHANDLET},
214 {"hyper", tHYPER},
215 {"import", tIMPORT},
216 {"importlib", tIMPORTLIB},
217 {"in_line", tINLINE},
218 {"int", tINT},
219 {"interface", tINTERFACE},
220 {"library", tLIBRARY},
221 {"long", tLONG},
222 {"methods", tMETHODS},
223 {"module", tMODULE},
224 {"properties", tPROPERTIES},
225 {"short", tSHORT},
226 {"signed", tSIGNED},
227 {"sizeof", tSIZEOF},
228 {"small", tSMALL},
229 {"struct", tSTRUCT},
230 {"switch", tSWITCH},
231 {"typedef", tTYPEDEF},
232 {"union", tUNION},
233 {"unsigned", tUNSIGNED},
234 {"void", tVOID},
235 {"wchar_t", tWCHAR},
236 };
237 #define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
238
239 /* keywords only recognized in attribute lists */
240 static const struct keyword attr_keywords[] =
241 {
242 {"aggregatable", tAGGREGATABLE},
243 {"allocate", tALLOCATE},
244 {"appobject", tAPPOBJECT},
245 {"async", tASYNC},
246 {"async_uuid", tASYNCUUID},
247 {"auto_handle", tAUTOHANDLE},
248 {"bindable", tBINDABLE},
249 {"broadcast", tBROADCAST},
250 {"byte_count", tBYTECOUNT},
251 {"call_as", tCALLAS},
252 {"context_handle", tCONTEXTHANDLE},
253 {"context_handle_noserialize", tCONTEXTHANDLENOSERIALIZE},
254 {"context_handle_serialize", tCONTEXTHANDLENOSERIALIZE},
255 {"control", tCONTROL},
256 {"defaultcollelem", tDEFAULTCOLLELEM},
257 {"defaultvalue", tDEFAULTVALUE},
258 {"defaultvtable", tDEFAULTVTABLE},
259 {"displaybind", tDISPLAYBIND},
260 {"dllname", tDLLNAME},
261 {"dual", tDUAL},
262 {"endpoint", tENDPOINT},
263 {"entry", tENTRY},
264 {"explicit_handle", tEXPLICITHANDLE},
265 {"handle", tHANDLE},
266 {"helpcontext", tHELPCONTEXT},
267 {"helpfile", tHELPFILE},
268 {"helpstring", tHELPSTRING},
269 {"helpstringcontext", tHELPSTRINGCONTEXT},
270 {"helpstringdll", tHELPSTRINGDLL},
271 {"hidden", tHIDDEN},
272 {"id", tID},
273 {"idempotent", tIDEMPOTENT},
274 {"iid_is", tIIDIS},
275 {"immediatebind", tIMMEDIATEBIND},
276 {"implicit_handle", tIMPLICITHANDLE},
277 {"in", tIN},
278 {"input_sync", tINPUTSYNC},
279 {"lcid", tLCID},
280 {"length_is", tLENGTHIS},
281 {"local", tLOCAL},
282 {"nonbrowsable", tNONBROWSABLE},
283 {"noncreatable", tNONCREATABLE},
284 {"nonextensible", tNONEXTENSIBLE},
285 {"object", tOBJECT},
286 {"odl", tODL},
287 {"oleautomation", tOLEAUTOMATION},
288 {"optional", tOPTIONAL},
289 {"out", tOUT},
290 {"pointer_default", tPOINTERDEFAULT},
291 {"propget", tPROPGET},
292 {"propput", tPROPPUT},
293 {"propputref", tPROPPUTREF},
294 {"ptr", tPTR},
295 {"public", tPUBLIC},
296 {"range", tRANGE},
297 {"readonly", tREADONLY},
298 {"ref", tREF},
299 {"requestedit", tREQUESTEDIT},
300 {"restricted", tRESTRICTED},
301 {"retval", tRETVAL},
302 {"single", tSINGLE},
303 {"size_is", tSIZEIS},
304 {"source", tSOURCE},
305 {"strict_context_handle", tSTRICTCONTEXTHANDLE},
306 {"string", tSTRING},
307 {"switch_is", tSWITCHIS},
308 {"switch_type", tSWITCHTYPE},
309 {"transmit_as", tTRANSMITAS},
310 {"unique", tUNIQUE},
311 {"uuid", tUUID},
312 {"v1_enum", tV1ENUM},
313 {"vararg", tVARARG},
314 {"version", tVERSION},
315 {"wire_marshal", tWIREMARSHAL},
316 };
317
318
319 #define KWP(p) ((const struct keyword *)(p))
320
321 static int kw_cmp_func(const void *s1, const void *s2)
322 {
323 return strcmp(KWP(s1)->kw, KWP(s2)->kw);
324 }
325
326 static int kw_token(const char *kw)
327 {
328 struct keyword key, *kwp;
329 key.kw = kw;
330 kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
331 if (kwp) {
332 parser_lval.str = xstrdup(kwp->kw);
333 return kwp->token;
334 }
335 parser_lval.str = xstrdup(kw);
336 return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
337 }
338
339 static int attr_token(const char *kw)
340 {
341 struct keyword key, *kwp;
342 key.kw = kw;
343 kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
344 sizeof(attr_keywords[0]), kw_cmp_func);
345 if (kwp) {
346 parser_lval.str = xstrdup(kwp->kw);
347 return kwp->token;
348 }
349 return kw_token(kw);
350 }
351
352 static void addcchar(char c)
353 {
354 if(cbufidx >= cbufalloc)
355 {
356 cbufalloc += 1024;
357 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
358 if(cbufalloc > 65536)
359 parser_warning("Reallocating string buffer larger than 64kB\n");
360 }
361 cbuffer[cbufidx++] = c;
362 }
363
364 static char *get_buffered_cstring(void)
365 {
366 addcchar(0);
367 return xstrdup(cbuffer);
368 }
369
370 static void pop_import(void)
371 {
372 int ptr = import_stack_ptr-1;
373
374 fclose(yyin);
375 yy_delete_buffer( YY_CURRENT_BUFFER );
376 yy_switch_to_buffer( import_stack[ptr].state );
377 if (temp_name) {
378 unlink(temp_name);
379 free(temp_name);
380 }
381 temp_name = import_stack[ptr].temp_name;
382 free( input_name );
383 input_name = import_stack[ptr].input_name;
384 line_number = import_stack[ptr].line_number;
385 import_stack_ptr--;
386 }
387
388 struct imports {
389 char *name;
390 struct imports *next;
391 } *first_import;
392
393 int do_import(char *fname)
394 {
395 FILE *f;
396 char *hname, *path, *p;
397 struct imports *import;
398 int ptr = import_stack_ptr;
399 int ret;
400
401 if (!parse_only && do_header) {
402 hname = dup_basename(fname, ".idl");
403 p = hname + strlen(hname) - 2;
404 if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
405
406 fprintf(header, "#include <%s>\n", hname);
407 free(hname);
408 }
409
410 import = first_import;
411 while (import && strcmp(import->name, fname))
412 import = import->next;
413 if (import) return 0; /* already imported */
414
415 import = xmalloc(sizeof(struct imports));
416 import->name = xstrdup(fname);
417 import->next = first_import;
418 first_import = import;
419
420 /* don't search for a file name with a path in the include directories,
421 * for compatibility with MIDL */
422 if (strchr( fname, '/' ) || strchr( fname, '\\' ))
423 path = strdup( fname );
424 else if (!(path = wpp_find_include( fname, input_name )))
425 error_loc("Unable to open include file %s\n", fname);
426
427 import_stack[ptr].temp_name = temp_name;
428 import_stack[ptr].input_name = input_name;
429 import_stack[ptr].line_number = line_number;
430 import_stack_ptr++;
431 input_name = path;
432 line_number = 1;
433
434 ret = wpp_parse_temp( path, NULL, &temp_name );
435 if (ret) exit(1);
436
437 if((f = fopen(temp_name, "r")) == NULL)
438 error_loc("Unable to open %s\n", temp_name);
439
440 import_stack[ptr].state = YY_CURRENT_BUFFER;
441 yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
442 return 1;
443 }
444
445 void abort_import(void)
446 {
447 int ptr;
448
449 for (ptr=0; ptr<import_stack_ptr; ptr++)
450 unlink(import_stack[ptr].temp_name);
451 }