-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathParser.cpp
3296 lines (3030 loc) · 73.1 KB
/
Parser.cpp
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
The Dark Mod GPL Source Code
This file is part of the The Dark Mod Source Code, originally based
on the Doom 3 GPL Source Code as published in 2011.
The Dark Mod Source Code is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version. For details, see LICENSE.TXT.
Project: The Dark Mod (http://www.thedarkmod.com/)
******************************************************************************/
#include "precompiled.h"
#pragma hdrstop
//#define DEBUG_EVAL
#define MAX_DEFINEPARMS 128
#define DEFINEHASHSIZE 2048
#define TOKEN_FL_RECURSIVE_DEFINE 1
define_t * idParser::globaldefines;
/*
================
idParser::SetBaseFolder
================
*/
void idParser::SetBaseFolder( const char *path) {
idLexer::SetBaseFolder(path);
}
/*
================
idParser::AddGlobalDefine
================
*/
int idParser::AddGlobalDefine( const char *string ) {
define_t *define;
define = idParser::DefineFromString(string);
if (!define) {
return false;
}
define->next = globaldefines;
globaldefines = define;
return true;
}
/*
================
idParser::RemoveGlobalDefine
================
*/
int idParser::RemoveGlobalDefine( const char *name ) {
define_t *d, *prev;
for ( prev = NULL, d = idParser::globaldefines; d; prev = d, d = d->next ) {
if ( !strcmp( d->name, name ) ) {
break;
}
}
if ( d ) {
if ( prev ) {
prev->next = d->next;
}
else {
idParser::globaldefines = d->next;
}
idParser::FreeDefine( d );
return true;
}
return false;
}
/*
================
idParser::RemoveAllGlobalDefines
================
*/
void idParser::RemoveAllGlobalDefines( void ) {
define_t *define;
for ( define = globaldefines; define; define = globaldefines ) {
globaldefines = globaldefines->next;
idParser::FreeDefine(define);
}
}
/*
===============================================================================
idParser
===============================================================================
*/
/*
================
idParser::PrintDefine
================
*/
void idParser::PrintDefine( define_t *define ) {
idLib::common->Printf("define->name = %s\n", define->name);
idLib::common->Printf("define->flags = %d\n", define->flags);
idLib::common->Printf("define->builtin = %d\n", define->builtin);
idLib::common->Printf("define->numparms = %d\n", define->numparms);
}
/*
================
PC_PrintDefineHashTable
================
* /
static void PC_PrintDefineHashTable(define_t **definehash) {
int i;
define_t *d;
for (i = 0; i < DEFINEHASHSIZE; i++) {
Log_Write("%4d:", i);
for (d = definehash[i]; d; d = d->hashnext) {
Log_Write(" %s", d->name);
}
Log_Write("\n");
}
}
*/
/*
================
PC_NameHash
================
*/
ID_INLINE int PC_NameHash( const char *name ) {
int hash, i;
hash = 0;
for ( i = 0; name[i] != '\0'; i++ ) {
hash += name[i] * (119 + i);
}
hash = (hash ^ (hash >> 10) ^ (hash >> 20)) & (DEFINEHASHSIZE-1);
return hash;
}
/*
================
idParser::AddDefineToHash
================
*/
void idParser::AddDefineToHash( define_t *define, define_t **definehash ) {
int hash;
hash = PC_NameHash(define->name);
define->hashnext = definehash[hash];
definehash[hash] = define;
}
/*
================
FindHashedDefine
================
*/
define_t *idParser::FindHashedDefine( define_t **definehash, const char *name ) {
define_t *d;
int hash;
hash = PC_NameHash(name);
for ( d = definehash[hash]; d; d = d->hashnext ) {
if ( !strcmp(d->name, name) ) {
return d;
}
}
return NULL;
}
/*
================
idParser::FindDefine
================
*/
define_t *idParser::FindDefine( define_t *defines, const char *name ) {
define_t *d;
for ( d = defines; d; d = d->next ) {
if ( !strcmp(d->name, name) ) {
return d;
}
}
return NULL;
}
idStrList idParser::GetAllDefineNames(bool sorted) {
idStrList ret;
for (int i = 0; i < DEFINEHASHSIZE; i++) {
for (define_t *def = definehash[i]; def; def = def->hashnext ) {
ret.Append(def->name);
}
}
if (sorted) {
auto comparator = [](const idStr *a, const idStr *b) -> int {
return (*a == *b ? 0 : (*a < *b ? -1 : 1));
};
ret.Sort(comparator);
}
return ret;
}
idStr idParser::GetDefineValueString(const char *name) {
define_t *def = FindHashedDefine(definehash, name);
idStr ret;
for (idToken *token = def->tokens; token; token = token->next) {
ret += *token;
}
return ret;
}
/*
================
idParser::FindDefineParm
================
*/
int idParser::FindDefineParm( define_t *define, const char *name ) {
idToken *p;
int i;
i = 0;
for ( p = define->parms; p; p = p->next ) {
if ( (*p) == name ) {
return i;
}
i++;
}
return -1;
}
/*
================
idParser::CopyDefine
================
*/
define_t *idParser::CopyDefine( define_t *define ) {
define_t *newdefine;
idToken *token, *newtoken, *lasttoken;
newdefine = (define_t *)Mem_Alloc(static_cast<int>(sizeof(define_t) + strlen(define->name) + 1));
//copy the define name
newdefine->name = (char *) newdefine + sizeof(define_t);
strcpy(newdefine->name, define->name);
newdefine->flags = define->flags;
newdefine->builtin = define->builtin;
newdefine->numparms = define->numparms;
//the define is not linked
newdefine->next = NULL;
newdefine->hashnext = NULL;
//copy the define tokens
newdefine->tokens = NULL;
for (lasttoken = NULL, token = define->tokens; token; token = token->next) {
newtoken = new idToken(token);
newtoken->next = NULL;
if (lasttoken) lasttoken->next = newtoken;
else newdefine->tokens = newtoken;
lasttoken = newtoken;
}
//copy the define parameters
newdefine->parms = NULL;
for (lasttoken = NULL, token = define->parms; token; token = token->next) {
newtoken = new idToken(token);
newtoken->next = NULL;
if (lasttoken) lasttoken->next = newtoken;
else newdefine->parms = newtoken;
lasttoken = newtoken;
}
return newdefine;
}
/*
================
idParser::FreeDefine
================
*/
void idParser::FreeDefine( define_t *define ) {
idToken *t, *next;
//free the define parameters
for (t = define->parms; t; t = next) {
next = t->next;
delete t;
}
//free the define tokens
for (t = define->tokens; t; t = next) {
next = t->next;
delete t;
}
//free the define
Mem_Free( define );
}
/*
================
idParser::DefineFromString
================
*/
define_t *idParser::DefineFromString( const char *string ) {
idParser src;
if (!src.LoadMemory(string, static_cast<int>(strlen(string)), "*defineString")) {
return NULL;
}
// create a define from the source
if ( !src.Directive_define() ) {
return NULL;
}
for ( int i = 0; i < DEFINEHASHSIZE; i++ ) {
for ( define_t *def = src.definehash[i]; def; def = def->hashnext ) {
if ( FindDefine( globaldefines, def->name ) )
continue;
return src.CopyDefine( def );
}
}
return NULL;
}
/*
================
idParser::Error
================
*/
void idParser::Error( const char *str, ... ) const {
char text[MAX_STRING_CHARS];
va_list ap;
va_start(ap, str);
vsprintf(text, str, ap);
va_end(ap);
if ( idParser::scriptstack ) {
idParser::scriptstack->Error( "%s", text );
}
}
/*
================
idParser::Warning
================
*/
void idParser::Warning( const char *str, ... ) const {
char text[MAX_STRING_CHARS];
va_list ap;
va_start(ap, str);
vsprintf(text, str, ap);
va_end(ap);
if ( idParser::scriptstack ) {
idParser::scriptstack->Warning( "%s", text );
}
}
/*
================
idParser::PushIndent
================
*/
void idParser::PushIndent( int type, int skip ) {
indent_t *indent;
indent = (indent_t *) Mem_Alloc(sizeof(indent_t));
indent->type = type;
indent->script = idParser::scriptstack;
indent->skip = (skip != 0);
idParser::skip += indent->skip;
indent->next = idParser::indentstack;
idParser::indentstack = indent;
}
/*
================
idParser::PopIndent
================
*/
void idParser::PopIndent( int *type, int *skip ) {
indent_t *indent;
*type = 0;
*skip = 0;
indent = idParser::indentstack;
if (!indent) return;
// must be an indent from the current script
if (idParser::indentstack->script != idParser::scriptstack) {
return;
}
*type = indent->type;
*skip = indent->skip;
idParser::indentstack = idParser::indentstack->next;
idParser::skip -= indent->skip;
Mem_Free( indent );
}
/*
================
idParser::PushScript
================
*/
void idParser::PushScript( idLexer *script ) {
idLexer *s;
for ( s = idParser::scriptstack; s; s = s->next ) {
if ( !idStr::Icmp(s->GetFileName(), script->GetFileName()) ) {
idParser::Warning( "'%s' recursively included", script->GetDisplayFileName() );
return;
}
}
//push the script on the script stack
script->next = idParser::scriptstack;
idParser::scriptstack = script;
}
/*
================
idParser::ReadSourceToken
================
*/
int idParser::ReadSourceToken( idToken *token ) {
idToken *t;
idLexer *script;
int type, skip, changedScript;
if ( !idParser::scriptstack ) {
idLib::common->FatalError( "idParser::ReadSourceToken: not loaded" );
return false;
}
changedScript = 0;
// if there's no token already available
while( !idParser::tokens ) {
// if there's a token to read from the script
if ( idParser::scriptstack->ReadToken( token ) ) {
token->linesCrossed += changedScript;
// set the marker based on the start of the token read in
if ( !marker_p ) {
marker_p = token->whiteSpaceEnd_p;
}
return true;
}
// if at the end of the script
if ( idParser::scriptstack->EndOfFile() ) {
// remove all indents of the script
while( idParser::indentstack && idParser::indentstack->script == idParser::scriptstack ) {
idParser::Warning( "missing #endif" );
idParser::PopIndent( &type, &skip );
}
changedScript = 1;
}
// if this was the initial script
if ( !idParser::scriptstack->next ) {
return false;
}
// remove the script and return to the previous one
script = idParser::scriptstack;
idParser::scriptstack = idParser::scriptstack->next;
delete script;
}
// copy the already available token
*token = idParser::tokens;
// remove the token from the source
t = idParser::tokens;
idParser::tokens = idParser::tokens->next;
delete t;
return true;
}
/*
================
idParser::UnreadSourceToken
================
*/
int idParser::UnreadSourceToken( idToken *token ) {
idToken *t;
t = new idToken(token);
t->next = idParser::tokens;
idParser::tokens = t;
return true;
}
/*
================
idParser::ReadDefineParms
================
*/
int idParser::ReadDefineParms( define_t *define, idToken **parms, int maxparms ) {
define_t *newdefine;
idToken token, *t, *last;
int i, done, lastcomma, numparms, indent;
if ( !idParser::ReadSourceToken( &token ) ) {
idParser::Error( "define '%s' missing parameters", define->name );
return false;
}
if ( define->numparms > maxparms ) {
idParser::Error( "define with more than %d parameters", maxparms );
return false;
}
for ( i = 0; i < define->numparms; i++ ) {
parms[i] = NULL;
}
// if no leading "("
if ( token != "(" ) {
idParser::UnreadSourceToken( &token );
idParser::Error( "define '%s' missing parameters", define->name );
return false;
}
// read the define parameters
for ( done = 0, numparms = 0, indent = 1; !done; ) {
if ( numparms >= maxparms ) {
idParser::Error( "define '%s' with too many parameters", define->name );
return false;
}
parms[numparms] = NULL;
lastcomma = 1;
last = NULL;
while( !done ) {
if ( !idParser::ReadSourceToken( &token ) ) {
idParser::Error( "define '%s' incomplete", define->name );
return false;
}
if ( token == "," ) {
if ( indent <= 1 ) {
if ( lastcomma ) {
idParser::Warning( "too many comma's" );
}
if ( numparms >= define->numparms ) {
idParser::Warning( "too many define parameters" );
}
lastcomma = 1;
break;
}
}
else if ( token == "(" ) {
indent++;
}
else if ( token == ")" ) {
indent--;
if ( indent <= 0 ) {
if ( !parms[define->numparms-1] ) {
idParser::Warning( "too few define parameters" );
}
done = 1;
break;
}
}
else if ( token.type == TT_NAME ) {
newdefine = FindHashedDefine( idParser::definehash, token.c_str() );
if ( newdefine ) {
if ( !idParser::ExpandDefineIntoSource( &token, newdefine ) ) {
return false;
}
continue;
}
}
lastcomma = 0;
if ( numparms < define->numparms ) {
t = new idToken( token );
t->next = NULL;
if (last) last->next = t;
else parms[numparms] = t;
last = t;
}
}
numparms++;
}
return true;
}
/*
================
idParser::StringizeTokens
================
*/
int idParser::StringizeTokens( idToken *tokens, idToken *token ) {
idToken *t;
token->type = TT_STRING;
token->whiteSpaceStart_p = NULL;
token->whiteSpaceEnd_p = NULL;
(*token) = "";
for ( t = tokens; t; t = t->next ) {
token->Append( t->c_str() );
}
return true;
}
/*
================
idParser::MergeTokens
================
*/
int idParser::MergeTokens( idToken *t1, idToken *t2 ) {
// merging of a name with a name or number
if ( t1->type == TT_NAME && (t2->type == TT_NAME || (t2->type == TT_NUMBER && !(t2->subtype & TT_FLOAT))) ) {
t1->Append( t2->c_str() );
return true;
}
// merging of two strings
if (t1->type == TT_STRING && t2->type == TT_STRING) {
t1->Append( t2->c_str() );
return true;
}
// merging of two numbers
if ( t1->type == TT_NUMBER && t2->type == TT_NUMBER &&
!(t1->subtype & (TT_HEX|TT_BINARY)) && !(t2->subtype & (TT_HEX|TT_BINARY)) &&
(!(t1->subtype & TT_FLOAT) || !(t2->subtype & TT_FLOAT)) ) {
t1->Append( t2->c_str() );
return true;
}
return false;
}
/*
================
idParser::AddBuiltinDefines
================
*/
void idParser::AddBuiltinDefines( void ) {
int i;
define_t *define;
struct builtin
{
const char *string;
int id;
} builtin[] = {
{ "__LINE__", BUILTIN_LINE },
{ "__FILE__", BUILTIN_FILE },
{ "__DATE__", BUILTIN_DATE },
{ "__TIME__", BUILTIN_TIME },
{ "__STDC__", BUILTIN_STDC },
{ NULL, 0 }
};
for (i = 0; builtin[i].string; i++) {
define = (define_t *)Mem_Alloc(static_cast<int>(sizeof(define_t) + strlen(builtin[i].string) + 1));
define->name = (char *) define + sizeof(define_t);
strcpy(define->name, builtin[i].string);
define->flags = DEFINE_FIXED;
define->builtin = builtin[i].id;
define->numparms = 0;
define->parms = NULL;
define->tokens = NULL;
// add the define to the source
AddDefineToHash(define, idParser::definehash);
}
}
/*
================
idParser::ExpandBuiltinDefine
================
*/
int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken ) {
idToken *token;
ID_TIME_T t;
char *curtime;
char buf[MAX_STRING_CHARS];
token = new idToken(deftoken);
switch( define->builtin ) {
case BUILTIN_LINE: {
sprintf( buf, "%d", deftoken->line );
(*token) = buf;
token->intvalue = deftoken->line;
token->floatvalue = deftoken->line;
token->type = TT_NUMBER;
token->subtype = TT_DECIMAL | TT_INTEGER | TT_VALUESVALID;
token->line = deftoken->line;
token->linesCrossed = deftoken->linesCrossed;
token->flags = 0;
*firsttoken = token;
*lasttoken = token;
break;
}
case BUILTIN_FILE: {
(*token) = idParser::scriptstack->GetFileName();
token->type = TT_NAME;
token->subtype = token->Length();
token->line = deftoken->line;
token->linesCrossed = deftoken->linesCrossed;
token->flags = 0;
*firsttoken = token;
*lasttoken = token;
break;
}
case BUILTIN_DATE: {
t = time(NULL);
curtime = ctime(&t);
(*token) = "\"";
token->Append( curtime+4 );
(*token)[7] = '\0';
token->Append( curtime+20 );
(*token)[10] = '\0';
token->Append( "\"" );
free(curtime);
token->type = TT_STRING;
token->subtype = token->Length();
token->line = deftoken->line;
token->linesCrossed = deftoken->linesCrossed;
token->flags = 0;
*firsttoken = token;
*lasttoken = token;
break;
}
case BUILTIN_TIME: {
t = time(NULL);
curtime = ctime(&t);
(*token) = "\"";
token->Append( curtime+11 );
(*token)[8] = '\0';
token->Append( "\"" );
free(curtime);
token->type = TT_STRING;
token->subtype = token->Length();
token->line = deftoken->line;
token->linesCrossed = deftoken->linesCrossed;
token->flags = 0;
*firsttoken = token;
*lasttoken = token;
break;
}
case BUILTIN_STDC: {
idParser::Warning( "__STDC__ not supported\n" );
*firsttoken = NULL;
*lasttoken = NULL;
break;
}
default: {
*firsttoken = NULL;
*lasttoken = NULL;
break;
}
}
return true;
}
/*
================
idParser::ExpandDefine
================
*/
int idParser::ExpandDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken ) {
idToken *parms[MAX_DEFINEPARMS], *dt, *pt, *t;
idToken *t1, *t2, *first, *last, *nextpt, token;
int parmnum, i;
//stgatilov: allow writing #if MACRO for undefined MACRO
//so we substitute undefined define with zero
if (!define) {
idToken *zero = new idToken(*deftoken);;
*zero = "0";
zero->type = TT_NUMBER;
zero->NumberValue();
*firsttoken = *lasttoken = zero;
return true;
}
// if it is a builtin define
if ( define->builtin ) {
return idParser::ExpandBuiltinDefine( deftoken, define, firsttoken, lasttoken );
}
// if the define has parameters
if ( define->numparms ) {
if ( !idParser::ReadDefineParms( define, parms, MAX_DEFINEPARMS ) ) {
return false;
}
#ifdef DEBUG_EVAL
for ( i = 0; i < define->numparms; i++ ) {
Log_Write("define parms %d:", i);
for ( pt = parms[i]; pt; pt = pt->next ) {
Log_Write( "%s", pt->c_str() );
}
}
#endif //DEBUG_EVAL
}
// empty list at first
first = NULL;
last = NULL;
// create a list with tokens of the expanded define
for ( dt = define->tokens; dt; dt = dt->next ) {
parmnum = -1;
// if the token is a name, it could be a define parameter
if ( dt->type == TT_NAME ) {
parmnum = FindDefineParm( define, dt->c_str() );
}
// if it is a define parameter
if ( parmnum >= 0 ) {
for ( pt = parms[parmnum]; pt; pt = pt->next ) {
t = new idToken(pt);
//add the token to the list
t->next = NULL;
if (last) last->next = t;
else first = t;
last = t;
}
}
else {
// if stringizing operator
if ( (*dt) == "#" ) {
// the stringizing operator must be followed by a define parameter
if ( dt->next ) {
parmnum = FindDefineParm( define, dt->next->c_str() );
}
else {
parmnum = -1;
}
if ( parmnum >= 0 ) {
// step over the stringizing operator
dt = dt->next;
// stringize the define parameter tokens
if ( !idParser::StringizeTokens( parms[parmnum], &token ) ) {
idParser::Error( "can't stringize tokens" );
return false;
}
t = new idToken(token);
t->line = deftoken->line;
}
else {
idParser::Warning( "stringizing operator without define parameter" );
continue;
}
}
else {
t = new idToken(dt);
t->line = deftoken->line;
}
// add the token to the list
t->next = NULL;
// the token being read from the define list should use the line number of
// the original file, not the header file
t->line = deftoken->line;
if ( last ) last->next = t;
else first = t;
last = t;
}
}
// check for the merging operator
for ( t = first; t; ) {
if ( t->next ) {
// if the merging operator
if ( (*t->next) == "##" ) {
t1 = t;
t2 = t->next->next;
if ( t2 ) {
if ( !idParser::MergeTokens( t1, t2 ) ) {
idParser::Error( "can't merge '%s' with '%s'", t1->c_str(), t2->c_str() );
return false;
}
delete t1->next;
t1->next = t2->next;
if ( t2 == last ) last = t1;
delete t2;
continue;
}
}
}
t = t->next;
}
// store the first and last token of the list
*firsttoken = first;
*lasttoken = last;
// free all the parameter tokens
for ( i = 0; i < define->numparms; i++ ) {
for ( pt = parms[i]; pt; pt = nextpt ) {
nextpt = pt->next;
delete pt;
}
}
return true;
}
/*
================
idParser::ExpandDefineIntoSource
================
*/
int idParser::ExpandDefineIntoSource( idToken *deftoken, define_t *define ) {
idToken *firsttoken, *lasttoken;
if ( !idParser::ExpandDefine( deftoken, define, &firsttoken, &lasttoken ) ) {
return false;
}
// if the define is not empty
if ( firsttoken && lasttoken ) {
firsttoken->linesCrossed += deftoken->linesCrossed;
lasttoken->next = idParser::tokens;
idParser::tokens = firsttoken;
}
return true;
}
/*
================
idParser::ReadLine
reads a token from the current line, continues reading on the next
line only if a backslash '\' is found
================
*/
int idParser::ReadLine( idToken *token ) {
int crossline;
crossline = 0;
do {
if (!idParser::ReadSourceToken( token )) {
return false;
}
if (token->linesCrossed > crossline) {
idParser::UnreadSourceToken( token );
return false;
}
crossline = 1;
} while( (*token) == "\\" );
return true;
}
/*
================
idParser::Directive_include
================
*/
int idParser::Directive_include( void ) {
idLexer *script;
idToken token;
idStr path;
if ( !idParser::ReadSourceToken( &token ) ) {
idParser::Error( "#include without file name" );
return false;
}
if ( token.linesCrossed > 0 ) {
idParser::Error( "#include without file name" );
return false;
}
if ( token.type == TT_STRING ) {
script = new idLexer;
if ( token.Find('*') >= 0 || token.Find('?') >= 0 || token.Find('[') >= 0 ) {
// stgatilov #6336: detect all files matching wildcard
// note: only absolute paths are handled (relative to root of D3 VFS of course)
if ( !token.IendsWith( ".script" ) ) {
idParser::Error( "Bad included file '%s', only extension 'script' allowed", token.c_str() );
return false;
}
int lastSlash = token.Last( '/' );
if ( lastSlash < 0 ) {
idParser::Error( "Wildcard include '%s' not supported: no directory", token.c_str() );
return false;
}
if ( token.Find('*', 0, lastSlash) >= 0 || token.Find('?', 0, lastSlash) >= 0 || token.Find('[', 0, lastSlash) >= 0 ) {
idParser::Error( "Bad included file '%s', control characters in directory not allowed", token.c_str() );
return false;
}
idStr searchDir = token.Left(lastSlash);
// collect list of files matching glob
idFileList *fileList = fileSystem->ListFilesTree( searchDir.c_str(), ".script", true );
idStr stubText = "// In-memory script file to implement #include by wildcard\n";
for ( int i = 0; i < fileList->GetNumFiles(); i++ ) {
idStr filepath = fileList->GetFile( i );
if ( filepath.Filter( token.c_str(), false ) ) {
stubText += idStr("#include \"") + filepath + "\"\n";
}
}
fileSystem->FreeFileList( fileList );
// now include the memory file with transitive #include-s
char *data = (char *)Mem_Alloc( stubText.Length() + 1 );
strcpy( data, stubText.c_str() );
if ( !script->LoadMemory( data, stubText.Length(), "include_wildcard_stub.script" ) ) {
Mem_Free( data );
delete script;
script = NULL;
}
script->OwnLoadedMemory();
}
else {