forked from BSzili/OpenJK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenizer.h
674 lines (557 loc) · 14.4 KB
/
Tokenizer.h
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
// Tokenizer.h
//
#ifndef __TOKENIZER_H
#define __TOKENIZER_H
#pragma warning( disable : 4786 ) // identifier was truncated
#include <string>
#include <vector>
#include <map>
using namespace std;
#include <windows.h>
typedef unsigned char byte;
typedef unsigned short word;
//#define MAX_STRING_LENGTH 256
#define MAX_STRING_LENGTH 512
#define MAX_IDENTIFIER_LENGTH 128
#define TKF_IGNOREDIRECTIVES 0x00000001 // skip over lines starting with #
#define TKF_USES_EOL 0x00000002 // generate end of line tokens
#define TKF_NODIRECTIVES 0x00000004 // don't treat # in any special way
#define TKF_WANTUNDEFINED 0x00000008 // if token not found in symbols create undefined token
#define TKF_WIDEUNDEFINEDSYMBOLS 0x00000010 // when undefined token encountered, accumulate until space
#define TKF_RAWSYMBOLSONLY 0x00000020
#define TKF_NUMERICIDENTIFIERSTART 0x00000040
#define TKF_IGNOREKEYWORDS 0x00000080
#define TKF_NOCASEKEYWORDS 0x00000100
#define TKF_NOUNDERSCOREINIDENTIFIER 0x00000200
#define TKF_NODASHINIDENTIFIER 0x00000400
#define TKF_COMMENTTOKENS 0x00000800
#define TKF_SPACETOKENS 0x00001000
#define TKF_RETURNINTSASIDENTIFIERS 0x00002000
#define TKF_COMMENTTOKENSSLASHSLASHONLY 0x00004000 // like TKF_COMMENTTOKENS, but only does "//", not "/**/" types
#define TKF_RETURN_EOF 0x00008000
#define TKF_NO_ESC_XLAT 0x00010000 // turns off escape-sequence translation during string read (eg leaves "\n" as 2 literal chars rather than as a single CR)
enum
{
TKERR_NONE,
TKERR_UNKNOWN,
TKERR_BUFFERCREATE,
TKERR_UNRECOGNIZEDSYMBOL,
TKERR_DUPLICATESYMBOL,
TKERR_STRINGLENGTHEXCEEDED,
TKERR_IDENTIFIERLENGTHEXCEEDED,
TKERR_EXPECTED_INTEGER,
TKERR_EXPECTED_IDENTIFIER,
TKERR_EXPECTED_STRING,
TKERR_EXPECTED_CHAR,
TKERR_EXPECTED_FLOAT,
TKERR_UNEXPECTED_TOKEN,
TKERR_INVALID_DIRECTIVE,
TKERR_INCLUDE_FILE_NOTFOUND,
TKERR_UNMATCHED_DIRECTIVE,
TKERR_UNEXPECTED_EOF,
TKERR_USERERROR,
};
enum
{
TK_EOF = -1,
TK_UNDEFINED,
TK_COMMENT,
TK_EOL,
TK_CHAR,
TK_STRING,
TK_INT,
TK_INTEGER = TK_INT,
TK_FLOAT,
TK_IDENTIFIER,
TK_SPACE,
TK_USERDEF,
};
typedef struct
{
char* m_keyword;
int m_tokenvalue;
} keywordArray_t;
class lessstr
{
public:
bool operator()(LPCTSTR str1, LPCTSTR str2) const {return (strcmp(str1, str2) < 0);};
};
class CParseStream
{
public:
CParseStream();
virtual ~CParseStream();
static CParseStream* Create();
virtual void Delete();
virtual bool NextChar(byte& theByte);
virtual int GetCurLine();
virtual void GetCurFilename(char** theBuff);
virtual long GetRemainingSize();
CParseStream* GetNext();
void SetNext(CParseStream* next);
virtual bool CanEndOfFile();
virtual bool IsThisDefinition(void* theDefinition);
protected:
virtual bool Init();
CParseStream* m_next;
bool m_original;
};
class CToken
{
public:
CToken();
virtual ~CToken();
static CToken* Create();
virtual void Delete();
static void DeleteAll();
virtual int GetType();
CToken* GetNext();
void SetNext(CToken* theToken);
virtual int GetIntValue();
virtual LPCTSTR GetStringValue();
virtual float GetFloatValue();
protected:
virtual void Init();
virtual void MakeAvail();
char* m_string;
CToken* m_next;
static CToken* m_avail;
};
class CCharToken : public CToken
{
public:
CCharToken();
virtual ~CCharToken();
static CCharToken* Create(byte theByte);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(byte theByte);
virtual void MakeAvail();
static CCharToken* m_avail;
};
class CStringToken : public CToken
{
public:
CStringToken();
virtual ~CStringToken();
static CStringToken* Create(LPCTSTR theString);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(LPCTSTR theString);
virtual void MakeAvail();
static CStringToken* m_avail;
};
class CIntToken : public CToken
{
public:
CIntToken();
virtual ~CIntToken();
static CIntToken* Create(long value);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
virtual float GetFloatValue();
virtual int GetIntValue();
virtual LPCTSTR GetStringValue();
protected:
virtual void Init(long value);
virtual void MakeAvail();
long m_value;
static CIntToken* m_avail;
};
class CFloatToken : public CToken
{
public:
CFloatToken();
virtual ~CFloatToken();
static CFloatToken* Create(float value);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
virtual float GetFloatValue();
virtual LPCTSTR GetStringValue();
protected:
virtual void Init(float value);
virtual void MakeAvail();
float m_value;
static CFloatToken* m_avail;
};
class CIdentifierToken : public CToken
{
public:
CIdentifierToken();
virtual ~CIdentifierToken();
static CIdentifierToken* Create(LPCTSTR name);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(LPCTSTR name);
virtual void MakeAvail();
static CIdentifierToken* m_avail;
};
class CCommentToken : public CToken
{
public:
CCommentToken();
virtual ~CCommentToken();
static CCommentToken* Create(LPCTSTR name);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(LPCTSTR name);
virtual void MakeAvail();
static CCommentToken* m_avail;
};
class CSpaceToken : public CToken
{
public:
CSpaceToken();
virtual ~CSpaceToken();
static CSpaceToken* Create(LPCTSTR name);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(LPCTSTR name);
virtual void MakeAvail();
static CSpaceToken* m_avail;
};
class CUserToken : public CToken
{
public:
CUserToken();
virtual ~CUserToken();
static CUserToken* Create(int value, LPCTSTR string);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(int value, LPCTSTR string);
virtual void MakeAvail();
int m_value;
static CUserToken* m_avail;
};
class CUndefinedToken : public CToken
{
public:
CUndefinedToken();
virtual ~CUndefinedToken();
static CUndefinedToken* Create(LPCTSTR string);
virtual void Delete();
static void DeleteAll();
virtual int GetType();
protected:
virtual void Init(LPCTSTR string);
virtual void MakeAvail();
static CUndefinedToken* m_avail;
};
class CSymbol
{
public:
CSymbol();
virtual ~CSymbol();
static CSymbol* Create(LPCTSTR symbolName);
virtual void Delete();
LPCTSTR GetName();
protected:
virtual void Init(LPCTSTR symbolName);
char* m_symbolName;
};
typedef map<LPCTSTR, CSymbol*, lessstr> symbolmap_t;
class CDirectiveSymbol : public CSymbol
{
public:
CDirectiveSymbol();
virtual ~CDirectiveSymbol();
static CDirectiveSymbol* Create(LPCTSTR symbolName);
virtual void Delete();
void SetValue(LPCTSTR value);
LPCTSTR GetValue();
protected:
virtual void Init(LPCTSTR symbolName);
char* m_value;
};
class CIntSymbol : public CSymbol
{
public:
CIntSymbol();
static CIntSymbol* Create(LPCTSTR symbolName, int value);
virtual void Delete();
int GetValue();
protected:
virtual void Init(LPCTSTR symbolName, int value);
int m_value;
};
class CSymbolTable
{
public:
CSymbolTable();
virtual ~CSymbolTable();
static CSymbolTable* Create();
void Delete();
bool AddSymbol(CSymbol* theSymbol);
CSymbol* FindSymbol(LPCTSTR symbolName);
CSymbol* ExtractSymbol(LPCTSTR symbolName);
void RemoveSymbol(LPCTSTR symbolName);
void DiscardSymbols();
void Iterate(void function(CSymbol*, DWORD, DWORD), DWORD parm1, DWORD parm2);
int GetNumEntries();
LPCSTR GetEntry(int iIndex);
protected:
void Init();
symbolmap_t m_symbols;
};
class CSymbolLookup
{
public:
CSymbolLookup();
virtual ~CSymbolLookup();
static CSymbolLookup* Create(byte theByte);
virtual void Delete();
CSymbolLookup* GetNext();
void SetNext(CSymbolLookup* next);
void SetParent(CSymbolLookup* parent);
CSymbolLookup* GetParent();
CSymbolLookup** GetChildAddress();
CSymbolLookup* GetChild();
void SetValue(int value);
int GetValue();
byte GetByte();
protected:
void Init(byte theByte);
CSymbolLookup* m_child;
CSymbolLookup* m_sibling;
CSymbolLookup* m_parent;
int m_value;
byte m_byte;
};
class CTokenizerState
{
public:
CTokenizerState();
virtual ~CTokenizerState();
static CTokenizerState* Create(bool skip);
virtual void Delete();
CTokenizerState* GetNext();
void SetNext(CTokenizerState* next);
virtual bool ProcessElse();
bool Skipping();
protected:
void Init(bool skip);
bool m_skip;
bool m_elseHit;
CTokenizerState* m_next;
};
class CTokenizerHolderState : public CTokenizerState
{
public:
CTokenizerHolderState();
virtual ~CTokenizerHolderState();
static CTokenizerHolderState* Create();
virtual void Delete();
virtual bool ProcessElse();
protected:
void Init();
};
typedef void (*LPTokenizerErrorProc)(LPCTSTR errString);
#ifdef USES_MODULES
class CTokenizer : public CModule
#else
class CTokenizer
#endif
{
public:
CTokenizer();
virtual ~CTokenizer();
static CTokenizer* Create(UINT dwFlags = 0);
virtual void Delete();
virtual void Error(int theError);
virtual void Error(int theError, LPCTSTR errString);
virtual void Error(LPCTSTR errString, int theError = TKERR_UNKNOWN);
CToken* GetToken(UINT onFlags = 0, UINT offFlags = 0);
CToken* GetToken(keywordArray_t* keywords, UINT onFlags, UINT offFlags);
void PutBackToken(CToken* theToken, bool commented = false, LPCTSTR addedChars = NULL);
bool RequireToken(int tokenType);
void ScanUntilToken(int tokenType);
void SkipToLineEnd();
CToken* GetToEndOfLine(int tokenType = TK_IDENTIFIER);
void GetToEndOfFile();
keywordArray_t* SetKeywords(keywordArray_t* theKeywords);
void SetSymbols(keywordArray_t* theSymbols);
void SetAdditionalErrors(keywordArray_t* theErrors);
void SetErrorProc(LPTokenizerErrorProc errorProc);
void AddParseStream(byte* data, long datasize);
bool AddParseFile(LPCTSTR filename, DWORD buffsize = 0);
COLORREF ParseRGB();
long GetRemainingSize();
UINT GetFlags();
void SetFlags(UINT flags);
void GetCurFilename(char** filename);
int GetCurLine();
LPCTSTR LookupToken(int tokenID, keywordArray_t* theTable = NULL);
protected:
void SetError(int theError, LPCTSTR errString);
virtual void Init(UINT dwFlags = 0);
CToken* FetchToken();
bool AddDefineSymbol(CDirectiveSymbol* definesymbol);
bool NextChar(byte& theByte);
byte Escapement();
void InsertSymbol(LPCTSTR theSymbol, int theValue);
void PutBackChar(byte theByte);
CToken* TokenFromName(LPCTSTR name);
CToken* HandleDirective();
CToken* HandleSlash();
CToken* HandleString();
CToken* HandleQuote();
CToken* HandleIdentifier(byte theByte);
CToken* HandleNumeric(byte theByte, long value = 0);
CToken* HandleFloat(bool thesign = false, long value = 0);
CToken* HandleDecimal(bool thesign = false);
CToken* HandleSymbol(byte theByte);
CToken* HandleHex(bool thesize);
CToken* HandleOctal(bool thesize);
int DirectiveFromName(LPCTSTR name);
CParseStream* m_curParseStream;
keywordArray_t* m_keywords;
keywordArray_t* m_symbols;
keywordArray_t* m_errors;
CSymbolLookup* m_symbolLookup;
CToken* m_nextToken;
CSymbolTable m_defines;
CTokenizerState* m_state;
UINT m_flags;
LPTokenizerErrorProc m_errorProc;
bool m_parsingToEnd;
static keywordArray_t errorMessages[];
static keywordArray_t directiveKeywords[];
};
class CKeywordTable
{
public:
CKeywordTable(CTokenizer* tokenizer, keywordArray_t* keywords);
virtual ~CKeywordTable();
protected:
CTokenizer* m_tokenizer;
keywordArray_t* m_holdKeywords;
};
class CParsePutBack : public CParseStream
{
public:
CParsePutBack();
virtual ~CParsePutBack();
static CParsePutBack* Create(byte theByte, CParseStream* parent);
virtual void Delete();
virtual bool NextChar(byte& theByte);
virtual int GetCurLine();
virtual void GetCurFilename(char** theBuff);
virtual long GetRemainingSize();
protected:
virtual void Init(byte theByte, CParseStream* parent);
byte m_byte;
bool m_consumed;
int m_curLine;
CParseStream* m_parent;
};
class CParseMemory : public CParseStream
{
public:
CParseMemory();
virtual ~CParseMemory();
static CParseMemory* Create(byte* data, long datasize, bool original = false);
virtual void Delete();
virtual bool NextChar(byte& theByte);
virtual int GetCurLine();
virtual void GetCurFilename(char** theBuff);
virtual long GetRemainingSize();
virtual bool CanEndOfFile();
protected:
virtual void Init(byte* data, long datasize, bool original = false);
byte* m_data;
int m_curLine;
long m_curPos;
long m_datasize;
long m_offset;
};
class CParseBlock : public CParseMemory
{
public:
CParseBlock();
virtual ~CParseBlock();
static CParseBlock* Create(byte* data, long datasize);
virtual void Delete();
virtual bool CanEndOfFile();
protected:
virtual void Init(byte* data, long datasize);
};
class CParseToken : public CParseStream
{
public:
CParseToken();
virtual ~CParseToken();
static CParseToken* Create(CToken* token);
virtual void Delete();
virtual bool NextChar(byte& theByte);
virtual int GetCurLine();
virtual void GetCurFilename(char** theBuff);
virtual long GetRemainingSize();
protected:
virtual void Init(CToken* token);
byte* m_data;
int m_curLine;
long m_curPos;
long m_datasize;
long m_offset;
};
class CParseDefine : public CParseMemory
{
public:
CParseDefine();
virtual ~CParseDefine();
static CParseDefine* Create(CDirectiveSymbol* definesymbol);
virtual void Delete();
virtual bool IsThisDefinition(void* theDefinition);
virtual bool CanEndOfFile();
protected:
virtual void Init(CDirectiveSymbol* definesymbol);
CDirectiveSymbol* m_defineSymbol;
};
class CParseFile : public CParseStream
{
public:
CParseFile();
virtual ~CParseFile();
static CParseFile* Create();
static CParseFile* Create(LPCTSTR filename, CTokenizer* tokenizer, DWORD buffsize = 0);
// static CParseFile* Create(CFile* file, CTokenizer* tokenizer);
virtual void Delete();
virtual int GetCurLine();
virtual void GetCurFilename(char** theBuff);
virtual long GetRemainingSize();
virtual bool CanEndOfFile();
virtual bool NextChar(byte& theByte);
protected:
virtual bool Init();
virtual bool Init(LPCTSTR filename, CTokenizer* tokenizer, DWORD buffsize);
// virtual void Init(CFile* file, CTokenizer* tokenizer);
DWORD GetFileSize();
void Read(void* buff, UINT buffsize);
// CFile* m_file;
HANDLE m_fileHandle;
char* m_fileName;
int m_curLine;
int m_curPos;
byte* m_buff;
DWORD m_curByte;
DWORD m_buffByte;
DWORD m_filesize;
DWORD m_buffsize;
bool m_ownsFile;
bool m_usesBuffer;
};
#endif//__TOKENIZER_H