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