parser.c 5.86 KB
Newer Older
dcw's avatar
dcw committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *                           PARSER FOR THE
 *                           ====== === ===
 *
 *                      DATA DECLARATION BUILDER
 *                      ==== =========== =======
 *
 *      This module is the parser used to interprete the high
 *      level data declarations.
 *
 ******* Grammar for Declarations
 *
dcw's avatar
dcw committed
13 14
 *	data   = [ tEXPORT chunk ]
 *		 [ tGLOBAL chunk ]
dcw's avatar
dcw committed
15
 *		 [ tBEGIN chunk ]
dcw's avatar
dcw committed
16 17 18
 *		 tTYPE tOPENCURLY declns tCLOSECURLY
 *	chunk  = tOPENCURLY list_of_lines tCLOSECURLY
 *	declns = list*( decln ) tEOF
dcw's avatar
dcw committed
19 20
 *	decln  = tID tEQ shapes tSEMI
 *	shapes = sep-list+( shape, tOR )
dcw's avatar
dcw committed
21
 *	shape  = tID [ tOPENBR params tCLOSEBR ] printlist
dcw's avatar
dcw committed
22 23
 *	params = sep-list+( param, tCOMMA )
 *	param  = tID tID
dcw's avatar
dcw committed
24
 *	printlist = list( tSTR | tNUM )
dcw's avatar
dcw committed
25 26 27
 */


28 29 30
#include <stdio.h>
#include <string.h>
#include <malloc.h>
dcw's avatar
dcw committed
31 32 33 34 35
#include "struct.h"
#include "lexer.h"
#include "parser.h"


36 37 38
#define COPYOF(new,old) {new=malloc(1+strlen(old));if(new)strcpy(new,old);}


dcw's avatar
dcw committed
39 40 41 42 43 44
/*
/^#ifdef HASPROTOS
!/endif$
stat %
*/

dcw's avatar
dcw committed
45

dcw's avatar
dcw committed
46
#ifdef HASPROTOS
dcw's avatar
dcw committed
47 48
static BOOL parse_chunk( char * );
static BOOL parse_declns( declnlist * );
dcw's avatar
dcw committed
49
static void error( char * );
dcw's avatar
dcw committed
50 51
static BOOL parse_decln( char ** , shapelist * );
static BOOL parse_shapes( shapelist * );
dcw's avatar
dcw committed
52
static BOOL parse_shape( char ** , paramlist * , printlist * );
dcw's avatar
dcw committed
53
static BOOL parse_params( paramlist * );
54 55
static BOOL parse_printlist( printlist * );
static BOOL parse_printitem( printitem * );
dcw's avatar
dcw committed
56
static BOOL parse_param( char ** , char ** );
dcw's avatar
dcw committed
57
#else
dcw's avatar
dcw committed
58 59
static BOOL parse_chunk();
static BOOL parse_declns();
dcw's avatar
dcw committed
60
static void error();
dcw's avatar
dcw committed
61 62 63 64
static BOOL parse_decln();
static BOOL parse_shapes();
static BOOL parse_shape();
static BOOL parse_params();
65 66
static BOOL parse_printlist();
static BOOL parse_printitem();
dcw's avatar
dcw committed
67
static BOOL parse_param();
dcw's avatar
dcw committed
68 69 70
#endif


dcw's avatar
dcw committed
71 72 73
#define MUSTBE(t,mesg)	if( nexttok() != (t) ) {error(mesg); return FALSE;}


dcw's avatar
dcw committed
74 75 76
/*
 *	data   = [ tEXPORT tOPENCURLY crap_to_} ]
 *		 [ tGLOBAL tOPENCURLY crap_to_} ]
dcw's avatar
dcw committed
77
 *		 [ tBEGIN tOPENCURLY crap_to_} ]
dcw's avatar
dcw committed
78 79 80
 *		 tTYPE tOPENCURLY declns tCLOSECURLY
 */

dcw's avatar
dcw committed
81
BOOL parse_data( exports, globals, begin, dp ) char *exports, *globals, *begin; declnlist *dp;
dcw's avatar
dcw committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
	if( nexttok() == tEXPORT )
	{
		if( ! parse_chunk( exports ) ) return FALSE;
	} else
	{
		*exports = '\0';
		ungettok();
	}

	if( nexttok() == tGLOBAL )
	{
		if( ! parse_chunk( globals ) ) return FALSE;
	} else
	{
		*globals = '\0';
		ungettok();
	}
dcw's avatar
dcw committed
100 101 102 103 104 105 106 107 108 109 110

	if( nexttok() == tBEGIN )
	{
		if( ! parse_chunk( begin ) ) return FALSE;
	} else
	{
		*begin = '\0';
		ungettok();
	}

	MUSTBE( tTYPE, "TYPE/EXPORT/GLOBAL/BEGIN expected" );
dcw's avatar
dcw committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	MUSTBE( tOPENCURLY, "{ expected" );
	if( ! parse_declns( dp ) ) return FALSE;
	MUSTBE( tCLOSECURLY, "{ expected" );
	MUSTBE( tEOF, "Spurious characters found beyond EOF" );

	return TRUE;
}


/* chunk  = tOPENCURLY list_of_lines tCLOSECURLY */

static BOOL parse_chunk( s ) char *s;
{
	char line[256];
	int  startlineno;

	*s = '\0';

	MUSTBE( tOPENCURLY, "'{' expected" );

	startlineno = lineno;
	for(;;)
	{
		if( ! readnextline( line ) )
		{
			fprintf( stderr,
				 "Premature EOF after { - started at line %d\n",
				 startlineno );
			return FALSE;
		}
	if( streq( line, "}" ) ) break;
		strcat( s, line );
		strcat( s, "\n" );
	}
	return TRUE;
}

dcw's avatar
dcw committed
148

dcw's avatar
dcw committed
149 150 151 152
/* declns = list*( decln )       */
/* NB: a decln starts with a tID */

static BOOL parse_declns( dp ) declnlist *dp;
dcw's avatar
dcw committed
153 154 155 156
{
	char		*name;
	shapelist	shapes;

dcw's avatar
dcw committed
157 158 159
	while( nexttok() == tID )
	{
		ungettok();
dcw's avatar
dcw committed
160 161
		if( ! parse_decln( &name, &shapes ) ) return FALSE;
		*dp = build_declnlist( name, shapes, (declnlist) NULL );
dcw's avatar
dcw committed
162 163
		dp = &( (*dp)->next );
	}
dcw's avatar
dcw committed
164
	ungettok();
dcw's avatar
dcw committed
165 166 167 168
	return TRUE;
}


dcw's avatar
dcw committed
169
static void error( s ) char *s;
dcw's avatar
dcw committed
170
{
dcw's avatar
dcw committed
171
	fprintf( stderr, "%s at line %d\n", s, lineno );
dcw's avatar
dcw committed
172 173 174 175 176 177 178
}


/* decln  = tID tEQ shapes tSEMI */

static BOOL parse_decln( name, shapes ) char **name; shapelist *shapes;
{
dcw's avatar
dcw committed
179 180
	MUSTBE( tID, "declaration expected" );
	COPYOF( *name, lexidval );
dcw's avatar
dcw committed
181

dcw's avatar
dcw committed
182
	MUSTBE( tEQ, "'=' expected" );
dcw's avatar
dcw committed
183 184 185

	if( ! parse_shapes( shapes ) ) return FALSE;

dcw's avatar
dcw committed
186
	MUSTBE( tSEMI, "';' expected" );
dcw's avatar
dcw committed
187

dcw's avatar
dcw committed
188
	return TRUE;
dcw's avatar
dcw committed
189 190 191
}


dcw's avatar
dcw committed
192
/* shapes = sep-list+( shape, tOR ) */
dcw's avatar
dcw committed
193 194 195 196 197

static BOOL parse_shapes( sp ) shapelist *sp;
{
	char		*tagname;
	paramlist	paras;
198
	printlist	print;
dcw's avatar
dcw committed
199

dcw's avatar
dcw committed
200 201 202 203
	for(;;) {
		if( ! parse_shape( &tagname, &paras, &print ) ) return FALSE;
		*sp = build_shapelist( tagname, paras,
				       print, (shapelist) NULL );
dcw's avatar
dcw committed
204 205 206 207 208 209 210 211
	if( nexttok() != tOR ) break;
		sp = &( (*sp)->next );
	}
	ungettok();
	return TRUE;
}


dcw's avatar
dcw committed
212
/* shape = tID [ tOPENBR params tCLOSEBR ] printlist */
dcw's avatar
dcw committed
213

214
static BOOL parse_shape( tagname, pl, print ) char **tagname; paramlist *pl; printlist *print;
dcw's avatar
dcw committed
215
{
dcw's avatar
dcw committed
216
	*pl    = (paramlist) NULL;
dcw's avatar
dcw committed
217
	*print = (printlist) NULL;
218

dcw's avatar
dcw committed
219 220
	MUSTBE( tID, "shape name expected" );
	COPYOF( *tagname, lexidval );
dcw's avatar
dcw committed
221

dcw's avatar
dcw committed
222
	if( nexttok() == tOPENBR )
dcw's avatar
dcw committed
223 224 225
	{
		if( ! parse_params( pl ) ) return FALSE;

dcw's avatar
dcw committed
226
		MUSTBE( tCLOSEBR, "')' expected" );
dcw's avatar
dcw committed
227
	} else
dcw's avatar
dcw committed
228 229 230 231
	{
		ungettok();
	}

dcw's avatar
dcw committed
232
	return parse_printlist( print );
dcw's avatar
dcw committed
233 234 235
}


dcw's avatar
dcw committed
236
/* params = sep-list+( param, tCOMMA ) */
dcw's avatar
dcw committed
237 238 239 240 241 242

static BOOL parse_params( pp ) paramlist *pp;
{
	char *type;
	char *name;

dcw's avatar
dcw committed
243
	for(;;) {
244
		if( !parse_param( &type, &name ) ) return FALSE;
dcw's avatar
dcw committed
245
		*pp = build_paramlist( type, name, (paramlist) NULL );
dcw's avatar
dcw committed
246
	if( nexttok() != tCOMMA ) break;
dcw's avatar
dcw committed
247 248 249
		pp = &( (*pp)->next );
	}
	ungettok();
dcw's avatar
dcw committed
250 251 252 253
	return TRUE;
}


254 255 256 257 258 259 260
/* printlist = list( printitem ) */
/* NB: Never fails, cos items are one token long */

static BOOL parse_printlist( pp ) printlist *pp;
{
	printitem item;

dcw's avatar
dcw committed
261
	*pp = (printlist) NULL;
262 263 264

	while( parse_printitem( &item ) )
	{
dcw's avatar
dcw committed
265 266
		*pp = build_printlist( item, (printlist) NULL );
		pp = &( (*pp)->next );
267 268 269 270 271 272
	}
	return TRUE;
}


/* printitem = tSTR | tNUM */
dcw's avatar
dcw committed
273

274 275
static BOOL parse_printitem( item ) printitem *item;
{
dcw's avatar
dcw committed
276 277
	char *temp;

278 279
	switch( nexttok() ) {
	case tSTR:
dcw's avatar
dcw committed
280 281
		COPYOF( temp, lexidval );
		*item = build_printitem_str( temp );
282 283 284 285 286 287 288 289 290 291 292 293 294 295
		return TRUE;
	case tNUM:
		*item = build_printitem_num( lexintval );
		return TRUE;
	default:
		ungettok();
		return FALSE;
	}
}


/* param     = tID tID */

static BOOL parse_param( type, name ) char **type, **name;
dcw's avatar
dcw committed
296
{
dcw's avatar
dcw committed
297
	MUSTBE( tID, "Field type expected" );
dcw's avatar
dcw committed
298 299
	COPYOF( *type, lexidval );

dcw's avatar
dcw committed
300
	MUSTBE( tID, "Field name expected" );
dcw's avatar
dcw committed
301 302 303 304
	COPYOF( *name, lexidval );

	return TRUE;
}