-
Notifications
You must be signed in to change notification settings - Fork 2
/
l_script.c
1423 lines (1369 loc) · 40.7 KB
/
l_script.c
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
/*
===========================================================================
Return to Castle Wolfenstein single player GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Return to Castle Wolfenstein single player GPL Source Code (RTCW SP Source Code).
RTCW SP 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.
RTCW SP Source Code 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTCW SP Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the RTCW SP Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the RTCW SP
Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms,
you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
/*****************************************************************************
* name: l_script.c
*
* desc: lexicographical parser
*
*
*****************************************************************************/
//#define SCREWUP
//#define BOTLIB
//#define MEQCC
//#define BSPC
#ifdef SCREWUP
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdarg.h>
#include "l_memory.h"
#include "l_script.h"
typedef enum {qfalse, qtrue} qboolean;
#endif //SCREWUP
#ifdef BOTLIB
//include files for usage in the bot library
#include "../game/q_shared.h"
#include "botlib.h"
#include "be_interface.h"
#include "l_script.h"
#include "l_memory.h"
#include "l_log.h"
#include "l_libvar.h"
#endif //BOTLIB
#ifdef MEQCC
//include files for usage in MrElusive's QuakeC Compiler
#include "qcc.h"
#include "l_script.h"
#include "l_memory.h"
#include "l_log.h"
#define qtrue true
#define qfalse false
#endif //MEQCC
#ifdef BSPC
//include files for usage in the BSP Converter
#include "../bspc/qbsp.h"
#include "../bspc/l_log.h"
#include "../bspc/l_mem.h"
#define qtrue true
#define qfalse false
#endif //BSPC
#define PUNCTABLE
//longer punctuations first
punctuation_t default_punctuations[] =
{
//binary operators
{">>=",P_RSHIFT_ASSIGN, NULL},
{"<<=",P_LSHIFT_ASSIGN, NULL},
//
{"...",P_PARMS, NULL},
//define merge operator
{"##",P_PRECOMPMERGE, NULL},
//logic operators
{"&&",P_LOGIC_AND, NULL},
{"||",P_LOGIC_OR, NULL},
{">=",P_LOGIC_GEQ, NULL},
{"<=",P_LOGIC_LEQ, NULL},
{"==",P_LOGIC_EQ, NULL},
{"!=",P_LOGIC_UNEQ, NULL},
//arithmatic operators
{"*=",P_MUL_ASSIGN, NULL},
{"/=",P_DIV_ASSIGN, NULL},
{"%=",P_MOD_ASSIGN, NULL},
{"+=",P_ADD_ASSIGN, NULL},
{"-=",P_SUB_ASSIGN, NULL},
{"++",P_INC, NULL},
{"--",P_DEC, NULL},
//binary operators
{"&=",P_BIN_AND_ASSIGN, NULL},
{"|=",P_BIN_OR_ASSIGN, NULL},
{"^=",P_BIN_XOR_ASSIGN, NULL},
{">>",P_RSHIFT, NULL},
{"<<",P_LSHIFT, NULL},
//reference operators
{"->",P_POINTERREF, NULL},
//C++
{"::",P_CPP1, NULL},
{".*",P_CPP2, NULL},
//arithmatic operators
{"*",P_MUL, NULL},
{"/",P_DIV, NULL},
{"%",P_MOD, NULL},
{"+",P_ADD, NULL},
{"-",P_SUB, NULL},
{"=",P_ASSIGN, NULL},
//binary operators
{"&",P_BIN_AND, NULL},
{"|",P_BIN_OR, NULL},
{"^",P_BIN_XOR, NULL},
{"~",P_BIN_NOT, NULL},
//logic operators
{"!",P_LOGIC_NOT, NULL},
{">",P_LOGIC_GREATER, NULL},
{"<",P_LOGIC_LESS, NULL},
//reference operator
{".",P_REF, NULL},
//seperators
{",",P_COMMA, NULL},
{";",P_SEMICOLON, NULL},
//label indication
{":",P_COLON, NULL},
//if statement
{"?",P_QUESTIONMARK, NULL},
//embracements
{"(",P_PARENTHESESOPEN, NULL},
{")",P_PARENTHESESCLOSE, NULL},
{"{",P_BRACEOPEN, NULL},
{"}",P_BRACECLOSE, NULL},
{"[",P_SQBRACKETOPEN, NULL},
{"]",P_SQBRACKETCLOSE, NULL},
//
{"\\",P_BACKSLASH, NULL},
//precompiler operator
{"#",P_PRECOMP, NULL},
#ifdef DOLLAR
{"$",P_DOLLAR, NULL},
#endif //DOLLAR
{NULL, 0}
};
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void PS_CreatePunctuationTable( script_t *script, punctuation_t *punctuations ) {
int i;
punctuation_t *p, *lastp, *newp;
//get memory for the table
if ( !script->punctuationtable ) {
script->punctuationtable = (punctuation_t **)
GetMemory( 256 * sizeof( punctuation_t * ) );
}
memset( script->punctuationtable, 0, 256 * sizeof( punctuation_t * ) );
//add the punctuations in the list to the punctuation table
for ( i = 0; punctuations[i].p; i++ )
{
newp = &punctuations[i];
lastp = NULL;
//sort the punctuations in this table entry on length (longer punctuations first)
for ( p = script->punctuationtable[(unsigned int) newp->p[0]]; p; p = p->next )
{
if ( strlen( p->p ) < strlen( newp->p ) ) {
newp->next = p;
if ( lastp ) {
lastp->next = newp;
} else { script->punctuationtable[(unsigned int) newp->p[0]] = newp;}
break;
} //end if
lastp = p;
} //end for
if ( !p ) {
newp->next = NULL;
if ( lastp ) {
lastp->next = newp;
} else { script->punctuationtable[(unsigned int) newp->p[0]] = newp;}
} //end if
} //end for
} //end of the function PS_CreatePunctuationTable
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
char *PunctuationFromNum( script_t *script, int num ) {
int i;
for ( i = 0; script->punctuations[i].p; i++ )
{
if ( script->punctuations[i].n == num ) {
return script->punctuations[i].p;
}
} //end for
return "unkown punctuation";
} //end of the function PunctuationFromNum
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void QDECL ScriptError( script_t *script, char *str, ... ) {
char text[1024];
va_list ap;
if ( script->flags & SCFL_NOERRORS ) {
return;
}
va_start( ap, str );
vsprintf( text, str, ap );
va_end( ap );
#ifdef BOTLIB
botimport.Print( PRT_ERROR, "file %s, line %d: %s\n", script->filename, script->line, text );
#endif //BOTLIB
#ifdef MEQCC
printf( "error: file %s, line %d: %s\n", script->filename, script->line, text );
#endif //MEQCC
#ifdef BSPC
Log_Print( "error: file %s, line %d: %s\n", script->filename, script->line, text );
#endif //BSPC
} //end of the function ScriptError
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void QDECL ScriptWarning( script_t *script, char *str, ... ) {
char text[1024];
va_list ap;
if ( script->flags & SCFL_NOWARNINGS ) {
return;
}
va_start( ap, str );
vsprintf( text, str, ap );
va_end( ap );
#ifdef BOTLIB
botimport.Print( PRT_WARNING, "file %s, line %d: %s\n", script->filename, script->line, text );
#endif //BOTLIB
#ifdef MEQCC
printf( "warning: file %s, line %d: %s\n", script->filename, script->line, text );
#endif //MEQCC
#ifdef BSPC
Log_Print( "warning: file %s, line %d: %s\n", script->filename, script->line, text );
#endif //BSPC
} //end of the function ScriptWarning
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void SetScriptPunctuations( script_t *script, punctuation_t *p ) {
#ifdef PUNCTABLE
if ( p ) {
PS_CreatePunctuationTable( script, p );
} else { PS_CreatePunctuationTable( script, default_punctuations );}
#endif //PUNCTABLE
if ( p ) {
script->punctuations = p;
} else { script->punctuations = default_punctuations;}
} //end of the function SetScriptPunctuations
//============================================================================
// Reads spaces, tabs, C-like comments etc.
// When a newline character is found the scripts line counter is increased.
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadWhiteSpace( script_t *script ) {
while ( 1 )
{
//skip white space
while ( *script->script_p <= ' ' )
{
if ( !*script->script_p ) {
return 0;
}
if ( *script->script_p == '\n' ) {
script->line++;
}
script->script_p++;
} //end while
//skip comments
if ( *script->script_p == '/' ) {
//comments //
if ( *( script->script_p + 1 ) == '/' ) {
script->script_p++;
do
{
script->script_p++;
if ( !*script->script_p ) {
return 0;
}
} //end do
while ( *script->script_p != '\n' );
script->line++;
script->script_p++;
if ( !*script->script_p ) {
return 0;
}
continue;
} //end if
//comments /* */
else if ( *( script->script_p + 1 ) == '*' ) {
script->script_p++;
do
{
script->script_p++;
if ( !*script->script_p ) {
return 0;
}
if ( *script->script_p == '\n' ) {
script->line++;
}
} //end do
while ( !( *script->script_p == '*' && *( script->script_p + 1 ) == '/' ) );
script->script_p++;
if ( !*script->script_p ) {
return 0;
}
script->script_p++;
if ( !*script->script_p ) {
return 0;
}
continue;
} //end if
} //end if
break;
} //end while
return 1;
} //end of the function PS_ReadWhiteSpace
//============================================================================
// Reads an escape character.
//
// Parameter: script : script to read from
// ch : place to store the read escape character
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadEscapeCharacter( script_t *script, char *ch ) {
int c, val, i;
//step over the leading '\\'
script->script_p++;
//determine the escape character
switch ( *script->script_p )
{
case '\\': c = '\\'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'v': c = '\v'; break;
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'a': c = '\a'; break;
case '\'': c = '\''; break;
case '\"': c = '\"'; break;
case '\?': c = '\?'; break;
case 'x':
{
script->script_p++;
for ( i = 0, val = 0; ; i++, script->script_p++ )
{
c = *script->script_p;
if ( c >= '0' && c <= '9' ) {
c = c - '0';
} else if ( c >= 'A' && c <= 'Z' ) {
c = c - 'A' + 10;
} else if ( c >= 'a' && c <= 'z' ) {
c = c - 'a' + 10;
} else { break;}
val = ( val << 4 ) + c;
} //end for
script->script_p--;
if ( val > 0xFF ) {
ScriptWarning( script, "too large value in escape character" );
val = 0xFF;
} //end if
c = val;
break;
} //end case
default: //NOTE: decimal ASCII code, NOT octal
{
if ( *script->script_p < '0' || *script->script_p > '9' ) {
ScriptError( script, "unknown escape char" );
}
for ( i = 0, val = 0; ; i++, script->script_p++ )
{
c = *script->script_p;
if ( c >= '0' && c <= '9' ) {
c = c - '0';
} else { break;}
val = val * 10 + c;
} //end for
script->script_p--;
if ( val > 0xFF ) {
ScriptWarning( script, "too large value in escape character" );
val = 0xFF;
} //end if
c = val;
break;
} //end default
} //end switch
//step over the escape character or the last digit of the number
script->script_p++;
//store the escape character
*ch = c;
//succesfully read escape character
return 1;
} //end of the function PS_ReadEscapeCharacter
//============================================================================
// Reads C-like string. Escape characters are interpretted.
// Quotes are included with the string.
// Reads two strings with a white space between them as one string.
//
// Parameter: script : script to read from
// token : buffer to store the string
// Returns: qtrue when a string was read succesfully
// Changes Globals: -
//============================================================================
int PS_ReadString( script_t *script, token_t *token, int quote ) {
int len, tmpline;
char *tmpscript_p;
if ( quote == '\"' ) {
token->type = TT_STRING;
} else { token->type = TT_LITERAL;}
len = 0;
//leading quote
token->string[len++] = *script->script_p++;
//
while ( 1 )
{
//minus 2 because trailing double quote and zero have to be appended
if ( len >= MAX_TOKEN - 2 ) {
ScriptError( script, "string longer than MAX_TOKEN = %d", MAX_TOKEN );
return 0;
} //end if
//if there is an escape character and
//if escape characters inside a string are allowed
if ( *script->script_p == '\\' && !( script->flags & SCFL_NOSTRINGESCAPECHARS ) ) {
if ( !PS_ReadEscapeCharacter( script, &token->string[len] ) ) {
token->string[len] = 0;
return 0;
} //end if
len++;
} //end if
//if a trailing quote
else if ( *script->script_p == quote ) {
//step over the double quote
script->script_p++;
//if white spaces in a string are not allowed
if ( script->flags & SCFL_NOSTRINGWHITESPACES ) {
break;
}
//
tmpscript_p = script->script_p;
tmpline = script->line;
//read unusefull stuff between possible two following strings
if ( !PS_ReadWhiteSpace( script ) ) {
script->script_p = tmpscript_p;
script->line = tmpline;
break;
} //end if
//if there's no leading double qoute
if ( *script->script_p != quote ) {
script->script_p = tmpscript_p;
script->line = tmpline;
break;
} //end if
//step over the new leading double quote
script->script_p++;
} //end if
else
{
if ( *script->script_p == '\0' ) {
token->string[len] = 0;
ScriptError( script, "missing trailing quote" );
return 0;
} //end if
if ( *script->script_p == '\n' ) {
token->string[len] = 0;
ScriptError( script, "newline inside string %s", token->string );
return 0;
} //end if
token->string[len++] = *script->script_p++;
} //end else
} //end while
//trailing quote
token->string[len++] = quote;
//end string with a zero
token->string[len] = '\0';
//the sub type is the length of the string
token->subtype = len;
return 1;
} //end of the function PS_ReadString
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadName( script_t *script, token_t *token ) {
int len = 0;
char c;
token->type = TT_NAME;
do
{
token->string[len++] = *script->script_p++;
if ( len >= MAX_TOKEN ) {
ScriptError( script, "name longer than MAX_TOKEN = %d", MAX_TOKEN );
return 0;
} //end if
c = *script->script_p;
} while ( ( c >= 'a' && c <= 'z' ) ||
( c >= 'A' && c <= 'Z' ) ||
( c >= '0' && c <= '9' ) ||
c == '_' );
token->string[len] = '\0';
//the sub type is the length of the name
token->subtype = len;
return 1;
} //end of the function PS_ReadName
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void NumberValue( char *string, int subtype, unsigned long int *intvalue,
long double *floatvalue ) {
unsigned long int dotfound = 0;
*intvalue = 0;
*floatvalue = 0;
//floating point number
if ( subtype & TT_FLOAT ) {
while ( *string )
{
if ( *string == '.' ) {
if ( dotfound ) {
return;
}
dotfound = 10;
string++;
} //end if
if ( dotfound ) {
*floatvalue = *floatvalue + ( long double )( *string - '0' ) /
(long double) dotfound;
dotfound *= 10;
} //end if
else
{
*floatvalue = *floatvalue * 10.0 + ( long double )( *string - '0' );
} //end else
string++;
} //end while
*intvalue = (unsigned long) *floatvalue;
} //end if
else if ( subtype & TT_DECIMAL ) {
while ( *string ) *intvalue = *intvalue * 10 + ( *string++ - '0' );
*floatvalue = *intvalue;
} //end else if
else if ( subtype & TT_HEX ) {
//step over the leading 0x or 0X
string += 2;
while ( *string )
{
*intvalue <<= 4;
if ( *string >= 'a' && *string <= 'f' ) {
*intvalue += *string - 'a' + 10;
} else if ( *string >= 'A' && *string <= 'F' ) {
*intvalue += *string - 'A' + 10;
} else { *intvalue += *string - '0';}
string++;
} //end while
*floatvalue = *intvalue;
} //end else if
else if ( subtype & TT_OCTAL ) {
//step over the first zero
string += 1;
while ( *string ) *intvalue = ( *intvalue << 3 ) + ( *string++ - '0' );
*floatvalue = *intvalue;
} //end else if
else if ( subtype & TT_BINARY ) {
//step over the leading 0b or 0B
string += 2;
while ( *string ) *intvalue = ( *intvalue << 1 ) + ( *string++ - '0' );
*floatvalue = *intvalue;
} //end else if
} //end of the function NumberValue
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadNumber( script_t *script, token_t *token ) {
int len = 0, i;
int octal, dot;
char c;
// unsigned long int intvalue = 0;
// long double floatvalue = 0;
token->type = TT_NUMBER;
//check for a hexadecimal number
if ( *script->script_p == '0' &&
( *( script->script_p + 1 ) == 'x' ||
*( script->script_p + 1 ) == 'X' ) ) {
token->string[len++] = *script->script_p++;
token->string[len++] = *script->script_p++;
c = *script->script_p;
//hexadecimal
while ( ( c >= '0' && c <= '9' ) ||
( c >= 'a' && c <= 'f' ) ||
( c >= 'A' && c <= 'A' ) )
{
token->string[len++] = *script->script_p++;
if ( len >= MAX_TOKEN ) {
ScriptError( script, "hexadecimal number longer than MAX_TOKEN = %d", MAX_TOKEN );
return 0;
} //end if
c = *script->script_p;
} //end while
token->subtype |= TT_HEX;
} //end if
#ifdef BINARYNUMBERS
//check for a binary number
else if ( *script->script_p == '0' &&
( *( script->script_p + 1 ) == 'b' ||
*( script->script_p + 1 ) == 'B' ) ) {
token->string[len++] = *script->script_p++;
token->string[len++] = *script->script_p++;
c = *script->script_p;
//hexadecimal
while ( c == '0' || c == '1' )
{
token->string[len++] = *script->script_p++;
if ( len >= MAX_TOKEN ) {
ScriptError( script, "binary number longer than MAX_TOKEN = %d", MAX_TOKEN );
return 0;
} //end if
c = *script->script_p;
} //end while
token->subtype |= TT_BINARY;
} //end if
#endif //BINARYNUMBERS
else //decimal or octal integer or floating point number
{
octal = qfalse;
dot = qfalse;
if ( *script->script_p == '0' ) {
octal = qtrue;
}
while ( 1 )
{
token->string[len++] = *script->script_p++;
if ( len >= MAX_TOKEN ) {
ScriptError( script, "number longer than MAX_TOKEN = %d", MAX_TOKEN );
return 0;
} //end if
c = *script->script_p;
if ( c == '.' ) {
dot = qtrue;
} else if ( c == '8' || c == '9' ) {
octal = qfalse;
} else if ( c < '0' || c > '9' ) {
break;
}
} //end while
if ( octal ) {
token->subtype |= TT_OCTAL;
} else { token->subtype |= TT_DECIMAL;}
if ( dot ) {
token->subtype |= TT_FLOAT;
}
} //end else
for ( i = 0; i < 2; i++ )
{
c = *script->script_p;
//check for a LONG number
if ( (c == 'l' || c == 'L') &&
!( token->subtype & TT_LONG ) ) {
script->script_p++;
token->subtype |= TT_LONG;
} //end if
//check for an UNSIGNED number
else if ( (c == 'u' || c == 'U') &&
!( token->subtype & ( TT_UNSIGNED | TT_FLOAT ) ) ) {
script->script_p++;
token->subtype |= TT_UNSIGNED;
} //end if
} //end for
token->string[len] = '\0';
#ifdef NUMBERVALUE
NumberValue( token->string, token->subtype, &token->intvalue, &token->floatvalue );
#endif /* NUMBERVALUE */
if ( !( token->subtype & TT_FLOAT ) ) {
token->subtype |= TT_INTEGER;
}
return 1;
} //end of the function PS_ReadNumber
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadLiteral( script_t *script, token_t *token ) {
token->type = TT_LITERAL;
//first quote
token->string[0] = *script->script_p++;
//check for end of file
if ( !*script->script_p ) {
ScriptError( script, "end of file before trailing \'" );
return 0;
} //end if
//if it is an escape character
if ( *script->script_p == '\\' ) {
if ( !PS_ReadEscapeCharacter( script, &token->string[1] ) ) {
return 0;
}
} //end if
else
{
token->string[1] = *script->script_p++;
} //end else
//check for trailing quote
if ( *script->script_p != '\'' ) {
ScriptWarning( script, "too many characters in literal, ignored" );
while ( *script->script_p &&
*script->script_p != '\'' &&
*script->script_p != '\n' )
{
script->script_p++;
} //end while
if ( *script->script_p == '\'' ) {
script->script_p++;
}
} //end if
//store the trailing quote
token->string[2] = *script->script_p++;
//store trailing zero to end the string
token->string[3] = '\0';
//the sub type is the integer literal value
token->subtype = token->string[1];
//
return 1;
} //end of the function PS_ReadLiteral
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadPunctuation( script_t *script, token_t *token ) {
int len;
char *p;
punctuation_t *punc;
#ifdef PUNCTABLE
for ( punc = script->punctuationtable[(unsigned int)*script->script_p]; punc; punc = punc->next )
{
#else
int i;
for ( i = 0; script->punctuations[i].p; i++ )
{
punc = &script->punctuations[i];
#endif //PUNCTABLE
p = punc->p;
len = strlen( p );
//if the script contains at least as much characters as the punctuation
if ( script->script_p + len <= script->end_p ) {
//if the script contains the punctuation
if ( !strncmp( script->script_p, p, len ) ) {
strncpy( token->string, p, MAX_TOKEN );
script->script_p += len;
token->type = TT_PUNCTUATION;
//sub type is the number of the punctuation
token->subtype = punc->n;
return 1;
} //end if
} //end if
} //end for
return 0;
} //end of the function PS_ReadPunctuation
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadPrimitive( script_t *script, token_t *token ) {
int len;
len = 0;
while ( *script->script_p > ' ' && *script->script_p != ';' )
{
if ( len >= MAX_TOKEN ) {
ScriptError( script, "primitive token longer than MAX_TOKEN = %d", MAX_TOKEN );
return 0;
} //end if
token->string[len++] = *script->script_p++;
} //end while
token->string[len] = 0;
//copy the token into the script structure
memcpy( &script->token, token, sizeof( token_t ) );
//primitive reading successfull
return 1;
} //end of the function PS_ReadPrimitive
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ReadToken( script_t *script, token_t *token ) {
//if there is a token available (from UnreadToken)
if ( script->tokenavailable ) {
script->tokenavailable = 0;
memcpy( token, &script->token, sizeof( token_t ) );
return 1;
} //end if
//save script pointer
script->lastscript_p = script->script_p;
//save line counter
script->lastline = script->line;
//clear the token stuff
memset( token, 0, sizeof( token_t ) );
//start of the white space
script->whitespace_p = script->script_p;
token->whitespace_p = script->script_p;
//read unusefull stuff
if ( !PS_ReadWhiteSpace( script ) ) {
return 0;
}
//end of the white space
script->endwhitespace_p = script->script_p;
token->endwhitespace_p = script->script_p;
//line the token is on
token->line = script->line;
//number of lines crossed before token
token->linescrossed = script->line - script->lastline;
//if there is a leading double quote
if ( *script->script_p == '\"' ) {
if ( !PS_ReadString( script, token, '\"' ) ) {
return 0;
}
} //end if
//if an literal
else if ( *script->script_p == '\'' ) {
//if (!PS_ReadLiteral(script, token)) return 0;
if ( !PS_ReadString( script, token, '\'' ) ) {
return 0;
}
} //end if
//if there is a number
else if ( ( *script->script_p >= '0' && *script->script_p <= '9' ) ||
( *script->script_p == '.' &&
( *( script->script_p + 1 ) >= '0' && *( script->script_p + 1 ) <= '9' ) ) ) {
if ( !PS_ReadNumber( script, token ) ) {
return 0;
}
} //end if
//if this is a primitive script
else if ( script->flags & SCFL_PRIMITIVE ) {
return PS_ReadPrimitive( script, token );
} //end else if
//if there is a name
else if ( ( *script->script_p >= 'a' && *script->script_p <= 'z' ) ||
( *script->script_p >= 'A' && *script->script_p <= 'Z' ) ||
*script->script_p == '_' ) {
if ( !PS_ReadName( script, token ) ) {
return 0;
}
} //end if
//check for punctuations
else if ( !PS_ReadPunctuation( script, token ) ) {
ScriptError( script, "can't read token" );
return 0;
} //end if
//copy the token into the script structure
memcpy( &script->token, token, sizeof( token_t ) );
//succesfully read a token
return 1;
} //end of the function PS_ReadToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ExpectTokenString( script_t *script, char *string ) {
token_t token;
if ( !PS_ReadToken( script, &token ) ) {
ScriptError( script, "couldn't find expected %s", string );
return 0;
} //end if
if ( strcmp( token.string, string ) ) {
ScriptError( script, "expected %s, found %s", string, token.string );
return 0;
} //end if
return 1;
} //end of the function PS_ExpectToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PS_ExpectTokenType( script_t *script, int type, int subtype, token_t *token ) {
char str[MAX_TOKEN];
if ( !PS_ReadToken( script, token ) ) {
ScriptError( script, "couldn't read expected token" );
return 0;
} //end if
if ( token->type != type ) {
if ( type == TT_STRING ) {
strcpy( str, "string" );
}
if ( type == TT_LITERAL ) {
strcpy( str, "literal" );
}
if ( type == TT_NUMBER ) {
strcpy( str, "number" );
}
if ( type == TT_NAME ) {
strcpy( str, "name" );
}
if ( type == TT_PUNCTUATION ) {
strcpy( str, "punctuation" );
}
ScriptError( script, "expected a %s, found %s", str, token->string );
return 0;
} //end if
if ( token->type == TT_NUMBER ) {
if ( ( token->subtype & subtype ) != subtype ) {
if ( subtype & TT_DECIMAL ) {
strcpy( str, "decimal" );
}
if ( subtype & TT_HEX ) {
strcpy( str, "hex" );
}
if ( subtype & TT_OCTAL ) {
strcpy( str, "octal" );
}
if ( subtype & TT_BINARY ) {
strcpy( str, "binary" );
}
if ( subtype & TT_LONG ) {
strcat( str, " long" );
}
if ( subtype & TT_UNSIGNED ) {