Source

qutils / qcc / pr_lex.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <stdarg.h>
#include <unistd.h>
#include "../common/cmdlib.h"
#include "pr_comp.h"
#include "qcc.h"

int pr_source_line;

char *pr_file_p;
char *pr_line_start;			// start of current source line

int pr_bracelevel;

char pr_token[2048];
token_type_t pr_token_type;
type_t *pr_immediate_type;
eval_t pr_immediate;

char pr_immediate_string[2048];

int pr_error_count;

char *pr_punctuation[] =
// longer symbols must be before a shorter partial match
{ "&&", "||", "<=", ">=", "==", "!=", ";", ",", "!", "*", "/", "(", ")", "-",
		"+", "=", "[", "]", "{", "}", "...", ".", "<", ">", "#", "&", "|",
		NULL };

// simple types.  function types are dynamically allocated
type_t type_void = { ev_void, &def_void };
type_t type_string = { ev_string, &def_string };
type_t type_float = { ev_float, &def_float };
type_t type_vector = { ev_vector, &def_vector };
type_t type_entity = { ev_entity, &def_entity };
type_t type_field = { ev_field, &def_field };
type_t type_function = { ev_function, &def_function, NULL, &type_void };

// type_function is a void() function used for state defs
type_t type_pointer = { ev_pointer, &def_pointer };

type_t type_floatfield = { ev_field, &def_field, NULL, &type_float };

int type_size[8] = { 1, 1, 1, 3, 1, 1, 1, 1 };

def_t def_void = { &type_void, "temp" };
def_t def_string = { &type_string, "temp" };
def_t def_float = { &type_float, "temp" };
def_t def_vector = { &type_vector, "temp" };
def_t def_entity = { &type_entity, "temp" };
def_t def_field = { &type_field, "temp" };
def_t def_function = { &type_function, "temp" };
def_t def_pointer = { &type_pointer, "temp" };

def_t def_ret, def_parms[MAX_PARMS];

def_t *def_for_type[8] =
	{ &def_void, &def_string, &def_float, &def_vector, &def_entity,
&def_field, &def_function, &def_pointer };

void PR_LexWhitespace(void);

/*
==============
PR_PrintNextLine
==============
*/
void
PR_PrintNextLine(void)
{
	char *t;

	printf("%3i:", pr_source_line);
	for(t = pr_line_start; *t && *t != '\n'; t++)
		printf("%c", *t);
	printf("\n");
}

/*
==============
PR_NewLine

Call at start of file and when *pr_file_p == '\n'
==============
*/
void
PR_NewLine(void)
{
	qboolean m;

	if(*pr_file_p == '\n') {
		pr_file_p++;
		m = true;
	} else
		m = false;

	pr_source_line++;
	pr_line_start = pr_file_p;

//  if (pr_dumpasm)
//      PR_PrintNextLine ();
	if(m)
		pr_file_p--;
}

/*
==============
PR_LexString

Parses a quoted string
==============
*/
void
PR_LexString(void)
{
	int c;
	int len;

	len = 0;
	pr_file_p++;
	do {
		c = *pr_file_p++;
		if(!c)
			PR_ParseError("EOF inside quote");
		if(c == '\n')
			PR_ParseError("newline inside quote");
		if(c == '\\') {			// escape char
			c = *pr_file_p++;
			if(!c)
				PR_ParseError("EOF inside quote");
			if(c == 'n')
				c = '\n';
			else if(c == '"')
				c = '"';
			else
				PR_ParseError("Unknown escape char");
		} else if(c == '\"') {
			pr_token[len] = 0;
			pr_token_type = tt_immediate;
			pr_immediate_type = &type_string;
			strcpy(pr_immediate_string, pr_token);
			return;
		}
		pr_token[len] = c;
		len++;
	} while(1);
}

/*
==============
PR_LexNumber
==============
*/
float
PR_LexNumber(void)
{
	int c;
	int len;

	len = 0;
	c = *pr_file_p;
	do {
		pr_token[len] = c;
		len++;
		pr_file_p++;
		c = *pr_file_p;
	} while((c >= '0' && c <= '9') || c == '.');
	pr_token[len] = 0;
	return atof(pr_token);
}

/*
==============
PR_LexVector

Parses a single quoted vector
==============
*/
void
PR_LexVector(void)
{
	int i;

	pr_file_p++;
	pr_token_type = tt_immediate;
	pr_immediate_type = &type_vector;
	for(i = 0; i < 3; i++) {
		pr_immediate.vector[i] = PR_LexNumber();
		PR_LexWhitespace();
	}
	if(*pr_file_p != '\'')
		PR_ParseError("Bad vector");
	pr_file_p++;
}

/*
==============
PR_LexName

Parses an identifier
==============
*/
void
PR_LexName(void)
{
	int c;
	int len;

	len = 0;
	c = *pr_file_p;
	do {
		pr_token[len] = c;
		len++;
		pr_file_p++;
		c = *pr_file_p;
	} while((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
			|| (c >= '0' && c <= '9'));
	pr_token[len] = 0;
	pr_token_type = tt_name;
}

/*
==============
PR_LexPunctuation
==============
*/
void
PR_LexPunctuation(void)
{
	int i;
	int len;
	char *p;

	pr_token_type = tt_punct;

	for(i = 0; (p = pr_punctuation[i]) != NULL; i++) {
		len = strlen(p);
		if(!strncmp(p, pr_file_p, len)) {
			strcpy(pr_token, p);
			if(p[0] == '{')
				pr_bracelevel++;
			else if(p[0] == '}')
				pr_bracelevel--;
			pr_file_p += len;
			return;
		}
	}

	PR_ParseError("Unknown punctuation");
}

/*
==============
PR_LexWhitespace
==============
*/
void
PR_LexWhitespace(void)
{
	int c;

	while(1) {
		// skip whitespace
		while((c = *pr_file_p) <= ' ') {
			if(c == '\n')
				PR_NewLine();
			if(c == 0)
				return;			// end of file
			pr_file_p++;
		}

		// skip // comments
		if(c == '/' && pr_file_p[1] == '/') {
			while(*pr_file_p && *pr_file_p != '\n')
				pr_file_p++;
			PR_NewLine();
			pr_file_p++;
			continue;
		}
		// skip /* */ comments
		if(c == '/' && pr_file_p[1] == '*') {
			do {
				pr_file_p++;
				if(pr_file_p[0] == '\n')
					PR_NewLine();
				if(pr_file_p[1] == 0)
					return;
			} while(pr_file_p[-1] != '*' || pr_file_p[0] != '/');
			pr_file_p++;
			continue;
		}

		break;					// a real character has been found
	}
}

#define	MAX_FRAMES	256

char pr_framemacros[MAX_FRAMES][16];
int pr_nummacros;

void
PR_ClearGrabMacros(void)
{
	pr_nummacros = 0;
}

void
PR_FindMacro(void)
{
	int i;

	for(i = 0; i < pr_nummacros; i++)
		if(!strcmp(pr_token, pr_framemacros[i])) {
			sprintf(pr_token, "%d", i);
			pr_token_type = tt_immediate;
			pr_immediate_type = &type_float;
			pr_immediate._float = i;
			return;
		}
	PR_ParseError("Unknown frame macro $%s", pr_token);
}

// just parses text, returning false if an eol is reached
qboolean
PR_SimpleGetToken(void)
{
	int c;
	int i;

// skip whitespace
	while((c = *pr_file_p) <= ' ') {
		if(c == '\n' || c == 0)
			return false;
		pr_file_p++;
	}

	i = 0;
	while((c = *pr_file_p) > ' ' && c != ',' && c != ';') {
		pr_token[i] = c;
		i++;
		pr_file_p++;
	}
	pr_token[i] = 0;
	return true;
}

void
PR_ParseFrame(void)
{
	while(PR_SimpleGetToken()) {
		strcpy(pr_framemacros[pr_nummacros], pr_token);
		pr_nummacros++;
	}
}

/*
==============
PR_LexGrab

Deals with counting sequence numbers and replacing frame macros
==============
*/
void
PR_LexGrab(void)
{
	pr_file_p++;				// skip the $
	if(!PR_SimpleGetToken())
		PR_ParseError("hanging $");

// check for $frame
	if(!strcmp(pr_token, "frame")) {
		PR_ParseFrame();
		PR_Lex();
	}
// ignore other known $commands
	else if(!strcmp(pr_token, "cd")
			|| !strcmp(pr_token, "origin")
			|| !strcmp(pr_token, "base")
			|| !strcmp(pr_token, "flags")
			|| !strcmp(pr_token, "scale")
			|| !strcmp(pr_token, "skin")) {	// skip to end of line
		while(PR_SimpleGetToken()) ;
		PR_Lex();
	}
// look for a frame name macro
	else
		PR_FindMacro();
}

/*
==============
PR_Lex

Sets pr_token, pr_token_type, and possibly pr_immediate and pr_immediate_type
==============
*/
void
PR_Lex(void)
{
	int c;

	pr_token[0] = 0;

	if(!pr_file_p) {
		pr_token_type = tt_eof;
		return;
	}

	PR_LexWhitespace();

	c = *pr_file_p;

	if(!c) {
		pr_token_type = tt_eof;
		return;
	}
// handle quoted strings as a unit
	if(c == '\"') {
		PR_LexString();
		return;
	}
// handle quoted vectors as a unit
	if(c == '\'') {
		PR_LexVector();
		return;
	}
// if the first character is a valid identifier, parse until a non-id
// character is reached
	if((c >= '0' && c <= '9')
	   || (c == '-' && pr_file_p[1] >= '0' && pr_file_p[1] <= '9')) {
		pr_token_type = tt_immediate;
		pr_immediate_type = &type_float;
		pr_immediate._float = PR_LexNumber();
		return;
	}

	if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') {
		PR_LexName();
		return;
	}

	if(c == '$') {
		PR_LexGrab();
		return;
	}
// parse symbol strings until a non-symbol is found
	PR_LexPunctuation();
}

/*
============
PR_ParseError

Aborts the current file load
============
*/
void
PR_ParseError(char *error, ...)
{
	va_list argptr;
	char string[1024];

	va_start(argptr, error);
	vsprintf(string, error, argptr);
	va_end(argptr);

	printf("%s:%i:%s\n", strings + s_file, pr_source_line, string);

	longjmp(pr_parse_abort, 1);
}

/*
=============
PR_Expect

Issues an error if the current token isn't equal to string
Gets the next token
=============
*/
void
PR_Expect(char *string)
{
	if(strcmp(string, pr_token))
		PR_ParseError("expected %s, found %s", string, pr_token);
	PR_Lex();
}

/*
=============
PR_Check

Returns true and gets the next token if the current token equals string
Returns false and does nothing otherwise
=============
*/
qboolean
PR_Check(char *string)
{
	if(strcmp(string, pr_token))
		return false;

	PR_Lex();
	return true;
}

/*
============
PR_ParseName

Checks to see if the current token is a valid name
============
*/
char *
PR_ParseName(void)
{
	static char ident[MAX_NAME];

	if(pr_token_type != tt_name)
		PR_ParseError("not a name");
	if(strlen(pr_token) >= MAX_NAME - 1)
		PR_ParseError("name too long");
	strcpy(ident, pr_token);
	PR_Lex();

	return ident;
}

/*
============
PR_FindType

Returns a preexisting complex type that matches the parm, or allocates
a new one and copies it out.
============
*/
type_t *
PR_FindType(type_t * type)
{
	def_t *def;
	type_t *check;
	int i;

	for(check = pr.types; check; check = check->next) {
		if(check->type != type->type
		   || check->aux_type != type->aux_type
		   || check->num_parms != type->num_parms)
			continue;

		for(i = 0; i < type->num_parms; i++)
			if(check->parm_types[i] != type->parm_types[i])
				break;

		if(i == type->num_parms)
			return check;
	}

// allocate a new one
	check = malloc(sizeof(*check));
	*check = *type;
	check->next = pr.types;
	pr.types = check;

// allocate a generic def for the type, so fields can reference it
	def = malloc(sizeof(def_t));
	def->name = "COMPLEX TYPE";
	def->type = check;
	check->def = def;
	return check;
}

/*
============
PR_SkipToSemicolon

For error recovery, also pops out of nested braces
============
*/
void
PR_SkipToSemicolon(void)
{
	do {
		if(!pr_bracelevel && PR_Check(";"))
			return;
		PR_Lex();
	} while(pr_token[0]);		// eof will return a null token
}

/*
============
PR_ParseType

Parses a variable type, including field and functions types
============
*/
char pr_parm_names[MAX_PARMS][MAX_NAME];

type_t *
PR_ParseType(void)
{
	type_t new;
	type_t *type;
	char *name;

	if(PR_Check(".")) {
		memset(&new, 0, sizeof(new));
		new.type = ev_field;
		new.aux_type = PR_ParseType();
		return PR_FindType(&new);
	}

	if(!strcmp(pr_token, "float"))
		type = &type_float;
	else if(!strcmp(pr_token, "vector"))
		type = &type_vector;
	else if(!strcmp(pr_token, "float"))
		type = &type_float;
	else if(!strcmp(pr_token, "entity"))
		type = &type_entity;
	else if(!strcmp(pr_token, "string"))
		type = &type_string;
	else if(!strcmp(pr_token, "void"))
		type = &type_void;
	else {
		PR_ParseError("\"%s\" is not a type", pr_token);
		type = &type_float;		// shut up compiler warning
	}
	PR_Lex();

	if(!PR_Check("("))
		return type;

// function type
	memset(&new, 0, sizeof(new));
	new.type = ev_function;
	new.aux_type = type;		// return type
	new.num_parms = 0;
	if(!PR_Check(")")) {
		if(PR_Check("..."))
			new.num_parms = -1;	// variable args
		else
			do {
				type = PR_ParseType();
				name = PR_ParseName();
				strcpy(pr_parm_names[new.num_parms], name);
				new.parm_types[new.num_parms] = type;
				new.num_parms++;
			} while(PR_Check(","));

		PR_Expect(")");
	}

	return PR_FindType(&new);
}