parser.l 10.7 KB
Newer Older
1
/* -*-C-*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * IDL Compiler
 *
 * Copyright 2002 Ove Kaaven
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

%option stack
22 23
%option nounput noyy_top_state
%option 8bit never-interactive
24 25 26 27 28 29 30 31 32 33

nl	\r?\n
ws	[ \f\t\r]
cident	[a-zA-Z_][0-9a-zA-Z_]*
int	[0-9]+
hexd	[0-9a-fA-F]
hex	0x{hexd}+
uuid	{hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}

%x QUOTE
34
%x pp_line
35 36 37

%{

38 39
#include "config.h"

40 41 42 43 44
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
45 46 47
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
48 49 50 51

#include "widl.h"
#include "utils.h"
#include "parser.h"
52
#include "wine/wpp.h"
53

54
#include "parser.tab.h"
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

extern char *temp_name;

static void addcchar(char c);
static char *get_buffered_cstring(void);

static char *cbuffer;
static int cbufidx;
static int cbufalloc = 0;

static int kw_token(const char *kw);

#define MAX_IMPORT_DEPTH 10
struct {
  YY_BUFFER_STATE state;
70 71
  char *input_name;
  int   line_number;
72 73 74 75 76 77
  char *temp_name;
} import_stack[MAX_IMPORT_DEPTH];
int import_stack_ptr = 0;

static void pop_import(void);

78
static UUID* parse_uuid(const char*u)
79
{
80
  UUID* uuid = xmalloc(sizeof(UUID));
81 82
  char b[3];
  /* it would be nice to use UuidFromStringA */
83 84 85
  uuid->Data1 = strtoul(u, NULL, 16);
  uuid->Data2 = strtoul(u+9, NULL, 16);
  uuid->Data3 = strtoul(u+14, NULL, 16);
86
  b[2] = 0;
87 88 89 90 91 92 93 94
  memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
  memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
  memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
  memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
  memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
  memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
  memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
  memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
95 96 97
  return uuid;
}

98 99 100 101 102 103 104 105
%}

/*
 **************************************************************************
 * The flexer starts here
 **************************************************************************
 */
%%
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
<INITIAL>^{ws}*\#{ws}*	yy_push_state(pp_line);
<pp_line>[^\n]*         {
                            int lineno;
                            char *cptr, *fname;
                            yy_pop_state();
                            lineno = (int)strtol(yytext, &cptr, 10);
                            if(!lineno)
                                yyerror("Malformed '#...' line-directive; invalid linenumber");
                            fname = strchr(cptr, '"');
                            if(!fname)
                                yyerror("Malformed '#...' line-directive; missing filename");
                            fname++;
                            cptr = strchr(fname, '"');
                            if(!cptr)
                                yyerror("Malformed '#...' line-directive; missing terminating \"");
                            *cptr = '\0';
                            line_number = lineno - 1;  /* We didn't read the newline */
                            free( input_name );
                            input_name = xstrdup(fname);
                        }
126 127 128 129 130 131 132 133 134 135
\"			yy_push_state(QUOTE); cbufidx = 0;
<QUOTE>\"		{
				yy_pop_state();
				yylval.str = get_buffered_cstring();
				return aSTRING;
			}
<QUOTE>\\\\		|
<QUOTE>\\\"		addcchar(yytext[1]);
<QUOTE>\\.		addcchar('\\'); addcchar(yytext[1]);
<QUOTE>.		addcchar(yytext[0]);
136 137 138 139
{uuid}			{
				yylval.uuid = parse_uuid(yytext);
				return aUUID;
			}
140
{hex}			{
141
				yylval.num = strtoul(yytext, NULL, 0);
142 143
				return aHEXNUM;
			}
144
{int}			{
145
				yylval.num = strtoul(yytext, NULL, 0);
146 147
				return aNUM;
			}
148
{cident}		return kw_token(yytext);
149
\n			line_number++;
150 151 152 153 154
{ws}
\<\<			return SHL;
\>\>			return SHR;
.			return yytext[0];
<<EOF>>			{
155 156 157 158
				if (import_stack_ptr) {
					pop_import();
					return aEOF;
				}
159 160 161 162 163 164 165 166 167 168 169
				else yyterminate();
			}
%%

#ifndef yywrap
int yywrap(void)
{
	return 1;
}
#endif

170
static struct keyword {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	const char *kw;
	int token;
	int val;
} keywords[] = {
	{"__cdecl",			tCDECL},
	{"__int64",			tINT64},
	{"__stdcall",			tSTDCALL},
	{"_stdcall",			tSTDCALL},
	{"aggregatable",		tAGGREGATABLE},
	{"allocate",			tALLOCATE},
	{"appobject",			tAPPOBJECT},
	{"arrays",			tARRAYS},
	{"async",			tASYNC},
	{"async_uuid",			tASYNCUUID},
	{"auto_handle",			tAUTOHANDLE},
	{"bindable",			tBINDABLE},
	{"boolean",			tBOOLEAN},
	{"broadcast",			tBROADCAST},
	{"byte",			tBYTE},
	{"byte_count",			tBYTECOUNT},
	{"call_as",			tCALLAS},
	{"callback",			tCALLBACK},
	{"case",			tCASE},
	{"char",			tCHAR},
	{"coclass",			tCOCLASS},
	{"code",			tCODE},
	{"comm_status",			tCOMMSTATUS},
	{"const",			tCONST},
	{"context_handle",		tCONTEXTHANDLE},
	{"context_handle_noserialize",	tCONTEXTHANDLENOSERIALIZE},
	{"context_handle_serialize",	tCONTEXTHANDLENOSERIALIZE},
	{"control",			tCONTROL},
	{"cpp_quote",			tCPPQUOTE},
/* ... */
	{"default",			tDEFAULT},
206
	{"defaultvalue",		tDEFAULTVALUE},
207
/* ... */
208 209
	{"dispinterface",		tDISPINTERFACE},
/* ... */
210
	{"displaybind",			tDISPLAYBIND},
211
	{"dllname",			tDLLNAME},
212
	{"double",			tDOUBLE},
213
	{"dual",			tDUAL},
214
/* ... */
215
	{"endpoint",			tENDPOINT},
216
	{"entry",			tENTRY},
217
	{"enum",			tENUM},
218
	{"error_status_t",		tERRORSTATUST},
219
	{"explicit_handle",		tEXPLICITHANDLE},
220 221 222
	{"extern",			tEXTERN},
/* ... */
	{"float",			tFLOAT},
223
/* ... */
224
	{"handle",			tHANDLE},
225
	{"handle_t",			tHANDLET},
226
/* ... */
227 228
	{"helpcontext",			tHELPCONTEXT},
	{"helpfile",			tHELPFILE},
229
	{"helpstring",			tHELPSTRING},
230 231
	{"helpstringcontext",		tHELPSTRINGCONTEXT},
	{"helpstringdll",		tHELPSTRINGDLL},
232
/* ... */
233
	{"hidden",                      tHIDDEN},
234 235
	{"hyper",			tHYPER},
	{"id",				tID},
236
	{"idempotent",			tIDEMPOTENT},
237 238 239
/* ... */
	{"iid_is",			tIIDIS},
/* ... */
240
	{"implicit_handle",		tIMPLICITHANDLE},
241 242 243 244 245
	{"import",			tIMPORT},
	{"importlib",			tIMPORTLIB},
	{"in",				tIN},
	{"include",			tINCLUDE},
	{"in_line",			tINLINE},
246
	{"input_sync",			tINPUTSYNC},
247 248 249 250 251
	{"int",				tINT},
/* ... */
	{"interface",			tINTERFACE},
/* ... */
	{"length_is",			tLENGTHIS},
252
	{"library",			tLIBRARY},
253 254 255
/* ... */
	{"local",			tLOCAL},
	{"long",			tLONG},
256 257 258 259
/* ... */
	{"methods",			tMETHODS},
/* ... */
	{"module",			tMODULE},
260
/* ... */
Huw Davies's avatar
Huw Davies committed
261
	{"noncreatable",		tNONCREATABLE},
262 263 264 265
	{"object",			tOBJECT},
	{"odl",				tODL},
	{"oleautomation",		tOLEAUTOMATION},
/* ... */
266
	{"optional",			tOPTIONAL},
267 268 269 270
	{"out",				tOUT},
/* ... */
	{"pointer_default",		tPOINTERDEFAULT},
/* ... */
271
	{"properties",			tPROPERTIES},
272 273
	{"propget",			tPROPGET},
	{"propput",			tPROPPUT},
274
	{"propputref",			tPROPPUTREF},
275
        {"ptr",				tPTR},
276 277
/* ... */
	{"public",			tPUBLIC},
278
        {"range",			tRANGE},
279 280
/* ... */
	{"readonly",			tREADONLY},
281
	{"ref",				tREF},
282
/* ... */
283
	{"restricted",                  tRESTRICTED},
284
	{"retval",			tRETVAL},
285 286 287
/* ... */
	{"short",			tSHORT},
	{"signed",			tSIGNED},
288
	{"single",			tSINGLE},
289 290
	{"size_is",			tSIZEIS},
	{"sizeof",			tSIZEOF},
291
        {"small",			tSMALL},
292
/* ... */
293 294
	{"source",			tSOURCE},
/* ... */	
295 296 297 298 299 300
	{"string",			tSTRING},
	{"struct",			tSTRUCT},
	{"switch",			tSWITCH},
	{"switch_is",			tSWITCHIS},
	{"switch_type",			tSWITCHTYPE},
/* ... */
301
	{"transmit_as",			tTRANSMITAS},
302 303 304 305 306 307 308 309 310
	{"typedef",			tTYPEDEF},
	{"union",			tUNION},
/* ... */
	{"unique",			tUNIQUE},
	{"unsigned",			tUNSIGNED},
/* ... */
	{"uuid",			tUUID},
	{"v1_enum",			tV1ENUM},
/* ... */
311
	{"vararg",			tVARARG},
312 313 314
	{"version",			tVERSION},
	{"void",			tVOID},
	{"wchar_t",			tWCHAR},
315
	{"wire_marshal",		tWIREMARSHAL}
316
};
317
#define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
318
#define KWP(p) ((const struct keyword *)(p))
319 320 321 322 323

static int kw_cmp_func(const void *s1, const void *s2)
{
	return strcmp(KWP(s1)->kw, KWP(s2)->kw);
}
324

325
#define KW_BSEARCH
326 327
static int kw_token(const char *kw)
{
328 329 330 331 332 333 334 335 336 337 338 339 340 341
	struct keyword key, *kwp;
	key.kw = kw;
#ifdef KW_BSEARCH
	kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
#else
	{
		int i;
		for (kwp=NULL, i=0; i < NKEYWORDS; i++)
			if (!kw_cmp_func(&key, &keywords[i])) {
				kwp = &keywords[i];
				break;
			}
	}
#endif
342 343 344 345
	if (kwp) {
		yylval.str = (char*)kwp->kw;
		return kwp->token;
	}
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	yylval.str = xstrdup(kw);
	return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
}

static void addcchar(char c)
{
	if(cbufidx >= cbufalloc)
	{
		cbufalloc += 1024;
		cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
		if(cbufalloc > 65536)
			yywarning("Reallocating string buffer larger than 64kB");
	}
	cbuffer[cbufidx++] = c;
}

static char *get_buffered_cstring(void)
{
	addcchar(0);
	return xstrdup(cbuffer);
}

static void pop_import(void)
{
	int ptr = import_stack_ptr-1;

	fclose(yyin);
	yy_delete_buffer( YY_CURRENT_BUFFER );
	yy_switch_to_buffer( import_stack[ptr].state );
	if (temp_name) {
		unlink(temp_name);
		free(temp_name);
	}
	temp_name = import_stack[ptr].temp_name;
380 381 382
	free( input_name );
	input_name = import_stack[ptr].input_name;
	line_number = import_stack[ptr].line_number;
383 384 385 386 387 388 389 390
	import_stack_ptr--;
}

struct imports {
	char *name;
	struct imports *next;
} *first_import;

391
int do_import(char *fname)
392 393
{
	FILE *f;
394
	char *hname, *path, *p;
395 396 397 398
	struct imports *import;
	int ptr = import_stack_ptr;
	int ret;

Huw Davies's avatar
Huw Davies committed
399
	if (!parse_only && do_header) {
400
		hname = dup_basename(fname, ".idl");
401 402
                p = hname + strlen(hname) - 2;
                if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
403

404
		fprintf(header, "#include <%s>\n", hname);
405 406 407 408 409 410
		free(hname);
	}

	import = first_import;
	while (import && strcmp(import->name, fname))
		import = import->next;
411
	if (import) return 0; /* already imported */
412 413 414 415 416 417

	import = xmalloc(sizeof(struct imports));
	import->name = xstrdup(fname);
	import->next = first_import;
	first_import = import;

418
        if (!(path = wpp_find_include( fname, input_name )))
419 420 421
            yyerror("Unable to open include file %s", fname);

	import_stack[ptr].temp_name = temp_name;
422 423
	import_stack[ptr].input_name = input_name;
	import_stack[ptr].line_number = line_number;
424
	import_stack_ptr++;
425 426
        input_name = path;
        line_number = 1;
427

428
        ret = wpp_parse_temp( path, NULL, &temp_name );
429 430 431 432 433 434 435
        if (ret) exit(1);

	if((f = fopen(temp_name, "r")) == NULL)
		yyerror("Unable to open %s", temp_name);

	import_stack[ptr].state = YY_CURRENT_BUFFER;
	yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
436
	return 1;
437 438 439 440 441 442 443 444 445
}

void abort_import(void)
{
	int ptr;

	for (ptr=0; ptr<import_stack_ptr; ptr++)
		unlink(import_stack[ptr].temp_name);
}