/
token.h
474 lines (433 loc) · 12.2 KB
/
token.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
// Copyright (C) 1984-1998 by Symantec
// Copyright (C) 2000-2009 by Digital Mars
// All Rights Reserved
// http://www.digitalmars.com
// Written by Walter Bright
/*
* This source file is made available for personal use
* only. The license is in /dmd/src/dmd/backendlicense.txt
* or /dm/src/dmd/backendlicense.txt
* For any other uses, please contact Digital Mars.
*/
/**********************************
* Symbol tokens:
*
* TKstar * TKdot . TKeq =
* TKand & TKlbra [ TKaddass +=
* TKmin - TKrbra ] TKminass -=
* TKnot ! TKarrow -> TKmulass *=
* TKcom ~ TKdiv / TKdivass /=
* TKplpl ++ TKmod % TKmodass %=
* TKlpar ( TKxor ^ TKshrass >>=
* TKrpar ) TKor | TKshlass <<=
* TKques ? TKoror || TKandass &=
* TKcolon : TKandand && TKxorass ^=
* TKcomma , TKshl << TKorass |=
* TKmimi -- TKshr >> TKsemi ;
* TKlcur { TKrcur } TKlt <
* TKle <= TKgt > TKge >=
* TKeqeq == TKne != TKadd +
* TKellipsis ... TKcolcol :: TKdollar $
*
* Other tokens:
*
* TKstring string
* TKfilespec <filespec>
*/
//#pragma once
#ifndef TOKEN_H
#define TOKEN_H 1
#if !TX86
extern int skipping; /* TRUE ==> skipping false part of #conditional */
#endif
#if !defined(TOKENS_ONLY) || TOKENS_ONLY
// Keyword tokens. Needn't be ascii sorted
typedef unsigned char enum_TK;
enum TK {
TKauto,
TKbreak,
TKcase,
TKchar,
TKconst,
TKcontinue,
TKdefault,
TKdo,
TKdouble,
TKelse,
TKenum,
TKextern,
TKfloat,
TKfor,
TKgoto,
TKif,
TKint,
TKlong,
TKregister,
TKreturn,
TKshort,
TKsigned,
TKsizeof,
TKstatic,
TKstruct,
TKswitch,
TKtypedef,
TKunion,
TKunsigned,
TKvoid,
TKvolatile,
TKwhile,
// ANSI C99
TK_Complex,
TK_Imaginary,
TKrestrict,
//#if CPP
TKbool,
TKcatch,
TKclass,
TKconst_cast,
TKdelete,
TKdynamic_cast,
TKexplicit,
TKfalse,
TKfriend,
TKinline,
TKmutable,
TKnamespace,
TKnew,
TKoperator,
TKoverload,
TKprivate,
TKprotected,
TKpublic,
TKreinterpret_cast,
TKstatic_cast,
TKtemplate,
TKthis,
TKthrow,
TKtrue,
TKtry,
TKtypeid,
TKtypename,
TKusing,
TKvirtual,
TKwchar_t,
TK_typeinfo,
TK_typemask,
//#endif
#if CPP0X
TKalignof,
TKchar16_t,
TKchar32_t,
TKconstexpr,
TKdecltype,
TKnoexcept,
TKnullptr,
TKstatic_assert,
TKthread_local,
#endif
TKasm,
TK_inf,
TK_nan,
TK_nans,
TK_i, // imaginary constant i
TK_with,
TK_istype,
TK_cdecl,
TK_fortran,
TK_pascal,
TK_debug,
TK_in,
TK_out,
TK_body,
TK_invariant,
#if TX86
TK_Seg16,
TK_System,
TK__emit__,
TK_far,
TK_huge,
TK_near,
TK_asm,
TK_based,
TK_cs,
TK_declspec,
TK_except,
TK_export,
TK_far16,
TK_fastcall,
TK_finally,
TK_handle,
TK_java,
TK_int64,
TK_interrupt,
TK_leave,
TK_loadds,
TK_real80,
TK_saveregs,
TK_segname,
TK_ss,
TK_stdcall,
TK_syscall,
TK_try,
#if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS
TK_attribute,
TK_extension,
TK_format,
TK_restrict,
TK_bltin_const,
#endif
#else
TKcomp,
TKextended,
TK_handle,
TK_machdl,
TK_pasobj,
//#if CPP
TK__class,
TKinherited,
//#endif
#endif
TK_unaligned,
TKsymbol, // special internal token
#define KWMAX (TK_unaligned + 1) // number of keywords
TKcolcol, // ::
TKarrowstar, // ->*
TKdotstar, // .*
TKstar,TKand,TKmin,TKnot,TKcom,TKplpl,TKlpar,TKrpar,TKques,TKcolon,TKcomma,
TKmimi,TKlcur,TKdot,TKlbra,TKrbra,TKarrow,TKdiv,TKmod,TKxor,TKor,TKoror,
TKandand,TKshl,TKshr,TKrcur,TKeq,TKaddass,TKminass,TKmulass,TKdivass,
TKmodass,TKshrass,TKshlass,TKandass,TKxorass,TKorass,TKsemi,
TKadd,TKellipsis,
#if !TX86 || TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS
TKdollar,
#endif
/* The following relational tokens must be in the same order as the
corresponding operators.
*/
/* == != */
TKle,TKgt,TKlt,TKge,TKeqeq,TKne,
/* !<>= <> <>= !> !>= !< !<= !<> */
TKunord,TKlg,TKleg,TKule,TKul,TKuge,TKug,TKue,
TKstring,
TKfilespec, /* <filespec> */
TKpragma,
TKnum, /* integral number */
TKreal_f,
TKreal_d,
TKreal_da,
TKreal_ld,
TKident, /* identifier */
TKeol, /* end of line */
TKeof, /* end of file */
TKnone, /* no token */
TKMAX /* number of tokens */
};
#endif
#if !defined(TOKENS_ONLY) || !TOKENS_ONLY
struct token_t
{
enum_TK TKval; // what the token is
unsigned char TKflags; // Token flags
#define TKFfree 1 // free the token after it's scanned
#define TKFinherited 2 // keyword INHERITED prior to token
#define TKFpasstr 4 // pascal string
unsigned char TKty; // TYxxxx for TKstring and TKnum
union _TKutok
{
// Scheme for short IDs avoids malloc/frees
struct _ident // TKident
{ char *ident; // pointer to identifier
char idtext[4]; // if short identifier
} _idx;
struct _uts /* TKstring and TKfilespec */
{
char *string;/* for strings (not null terminated) */
int lenstr; /* length of string */
} uts;
symbol *sym; // TKsymbol
int pragma; // TKpragma: PRxxxx, pragma number
// -1 if unrecognized pragma
targ_long Vlong; /* integer when TKnum */
#if LONGLONG
targ_llong Vllong;
#else
#define Vllong Vlong
#endif
targ_float Vfloat;
targ_double Vdouble;
targ_ldouble Vldouble;
} TKutok;
Srcpos TKsrcpos; // line number from where it was taken
token_t *TKnext; // to create a list of tokens
#ifdef DEBUG
unsigned short id;
#define IDtoken 0xA745
#define token_debug(e) assert((e)->id == IDtoken)
#else
#define token_debug(e)
#endif
void setSymbol(symbol *s);
void print();
};
#define TKstr TKutok.uts.string
#define TKlenstr TKutok.uts.lenstr
#define TKid TKutok._idx.ident
#define TKsym TKutok.sym
// Use this for fast scans
#define _IDS 1 // start of identifier
#define _ID 2 // identifier
#define _TOK 4 // single character token
#define _EOL 8 // end of line
#define _MUL 0x10 // start of multibyte character sequence
#define _BCS 0x20 // in basic-source-character-set
#define _MTK 0x40 // could be multi-character token
#define _ZFF 0x80 // 0 or 0xFF (must be sign bit)
#define istok(x) (_chartype[(x) + 1] & _TOK)
#define iseol(x) (_chartype[(x) + 1] & _EOL)
#define isidstart(x) (_chartype[(x) + 1] & _IDS)
#define isidchar(x) (_chartype[(x) + 1] & (_IDS | _ID))
#define ismulti(x) (_chartype[(x) + 1] & _MUL)
#define isbcs(x) (_chartype[(x) + 1] & _BCS)
/* from token.c */
extern int igncomment;
extern char *tok_arg;
extern unsigned argmax;
extern token_t tok;
extern int ininclude;
CEXTERN char tok_ident[]; // identifier
extern unsigned char _chartype[];
#if TX86 || TARGET_MAC
extern token_t *toklist;
#endif
void token_setdbcs(int);
void token_setlocale(const char *);
token_t *token_copy(void);
void token_free(token_t *tl);
void token_hydrate(token_t **ptl);
void token_dehydrate(token_t **ptl);
#if TX86
token_t *token_funcbody(int);
#else
token_t *token_funcbody(int bFlag);
#endif
token_t *token_defarg(void);
void token_funcbody_print(token_t *t);
void token_setlist(token_t *t);
void token_poplist(void);
void token_unget(void);
void token_markfree(token_t *t);
void token_setident(char *);
void token_semi(void);
Srcpos token_linnum(void);
enum_TK token_peek();
enum_TK rtoken(int);
#if TARGET_MAC
#define stoken ptoken
extern void tok_retrieve(void);
#else
#if SPP
#define stoken() rtoken(1)
#else
enum_TK stokenx(void);
inline enum_TK stoken() { return toklist ? stokenx() : rtoken(1); }
#endif
#endif
void token_init(void);
void removext(void);
void __near comment(void);
void __near cppcomment(void);
char *combinestrings(targ_size_t *plen);
char *combinestrings(targ_size_t *plen, tym_t *pty);
void __near inident(void);
void inidentX(char *p);
unsigned comphash(const char *p);
int insertSpace(unsigned char xclast, unsigned char xcnext);
void panic(enum_TK ptok);
void chktok(enum_TK toknum , unsigned errnum);
void chktok(enum_TK toknum , unsigned errnum, const char *str);
void opttok(enum_TK toknum);
bool iswhite(int c);
void token_term(void);
#if TX86
int binary(const char *p , const char __near * __near *tab, int high);
#endif
#define ptoken() rtoken(1)
#define token() rtoken(0)
/* from pragma.c */
//enum_TK ptoken(void);
void pragma_process();
int __near pragma_search(char *id);
macro_t * __near macfind(void);
void __near listident(void);
void pragma_term(void);
macro_t *defmac(const char *name , const char *text);
int pragma_defined(void);
#if SPP && TX86
#define token_linnum() getlinnum()
#endif
// listing control
// Listings can be produce via -l and SCpre
// -l expand all characters not if'd out including
// comments
// SCpre list only characters to be compiled
// i.e. exclude comments and # preprocess lines
#if SPP
#define SCPRE_LISTING_ON() expflag--; assert(expflag >= 0)
#define SCPRE_LISTING_OFF() assert(expflag >= 0); expflag++
#define EXPANDED_LISTING_ON() expflag--; assert(expflag >= 0)
#define EXPANDED_LISTING_OFF() assert(expflag >= 0); expflag++
#else
#define SCPRE_LISTING_OFF()
#define SCPRE_LISTING_ON()
#define EXPANDED_LISTING_ON() expflag--; assert(expflag >= 0)
#define EXPANDED_LISTING_OFF() assert(expflag >= 0); expflag++
#endif
#define EXPANDING_LISTING() (expflag == 0)
#define NOT_EXPANDING_LISTING() (expflag)
#endif
/***********************************************
* This is the token lookahead API, which enables us to
* look an arbitrary number of tokens ahead and then
* be able to 'unget' all of them.
*/
struct Token_lookahead
{
int inited; // 1 if initialized
token_t *toks; // list of tokens
token_t **pend; // pointer to end of that list
void init()
{
toks = NULL;
pend = &toks;
inited = 1;
}
enum_TK lookahead()
{
#ifdef DEBUG
//assert(inited == 1);
#endif
*pend = token_copy();
(*pend)->TKflags |= TKFfree;
pend = &(*pend)->TKnext;
return stoken();
}
void term()
{
#ifdef DEBUG
//assert(inited == 1);
#endif
inited--;
if (toks)
{
token_unget();
token_setlist(toks);
stoken();
}
}
void discard()
{
inited--;
token_free(toks);
}
};
#endif /* TOKEN_H */