Debian Codegraph @

terps/tads/tads2/tok.h

raw symbols SCIP
1 /*
2 $Header: d:/cvsroot/tads/TADS2/TOK.H,v 1.3 1999/07/11 00:46:30 MJRoberts Exp $
3 */
4
5 /*
6 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
7 *
8 * Please see the accompanying license file, LICENSE.TXT, for information
9 * on using and copying this software.
10 */
11 /*
12 Name
13 tok.h - tokenizer definitions
14 Function
15 Definitions for tokenizer
16 Notes
17 None
18 Modified
19 08/14/91 MJRoberts - creation
20 */
21
22 #ifndef TOK_INCLUDED
23 #define TOK_INCLUDED
24
25 #ifndef STD_INCLUDED
26 #include "std.h"
27 #endif
28 #ifndef LIN_INCLUDED
29 #include "lin.h"
30 #endif
31 #ifndef MCH_INCLUDED
32 #include "mch.h"
33 #endif
34 #ifndef ERR_INCLUDED
35 #include "err.h"
36 #endif
37 #ifndef MCM_INCLUDED
38 #include "mcm.h"
39 #endif
40
41 /* number of entries in hash table - must be power of 2 */
42 #define TOKHASHSIZE 256
43
44 /* symbol types */
45 #define TOKSTUNK 0 /* unknown symbol, not yet defined */
46 #define TOKSTFUNC 1 /* function; value is object number */
47 #define TOKSTOBJ 2 /* object; value is object number */
48 #define TOKSTPROP 3 /* property; value is property number */
49 #define TOKSTLOCAL 4 /* a local variable or formal parameter */
50 #define TOKSTSELF 5 /* the pseudo-object "self" */
51 #define TOKSTBIFN 6 /* a built-in function */
52 #define TOKSTFWDOBJ 7 /* forward-referenced object */
53 #define TOKSTFWDFN 8 /* forward-referenced object */
54 #define TOKSTINHERIT 9 /* the pseudo-object "inherited" */
55 #define TOKSTEXTERN 10 /* an external function */
56 #define TOKSTKW 11 /* keyword; value is token number */
57 #define TOKSTLABEL 12 /* statement label */
58 #define TOKSTARGC 13 /* 'argcount' pseudo-variable */
59 #define TOKSTPROPSPEC 14 /* speculative evaluation property */
60
61 /* token types */
62 #define TOKTEOF 1
63
64 /* binary operators - keep these together (see prsbopl[] in prs.c) */
65 #define TOKTPLUS 2
66 #define TOKTMINUS 3
67 #define TOKTDIV 4
68 #define TOKTTIMES 5
69 #define TOKTNOT 6 /* ! or "not" */
70 #define TOKTEQ 7
71 #define TOKTNE 8
72 #define TOKTGT 9
73 #define TOKTGE 10
74 #define TOKTLT 11
75 #define TOKTLE 12
76 #define TOKTMOD 13
77 #define TOKTBAND 14
78 #define TOKTBOR 15
79 #define TOKTXOR 16
80 #define TOKTSHL 17
81 #define TOKTSHR 18
82 #define TOKTTILDE 30
83
84 /*
85 * special 'dot' replacement for speculative evaluation mode -- this is
86 * strictly for marking parse tree nodes, and has the same meaning in a
87 * parse tree node as a regular TOKTDOT, but generates code that can't
88 * call methods
89 */
90 #define TOKTDOTSPEC 31
91
92 /* special node marker for explicit superclass inheritance nodes */
93 #define TOKTEXPINH 32
94
95 #define TOKTLPAR 50 /* ( */
96 #define TOKTRPAR 51 /* ) */
97 #define TOKTCOLON 52
98 #define TOKTDSTRING 53 /* string in double quotes */
99 #define TOKTSSTRING 54 /* string in single quotes */
100 #define TOKTNUMBER 55
101 #define TOKTSYMBOL 56
102 #define TOKTINVALID 57 /* invalid lexical token */
103 #define TOKTLBRACK 58 /* [ */
104 #define TOKTRBRACK 59 /* ] */
105 #define TOKTLBRACE 60 /* { */
106 #define TOKTRBRACE 61 /* } */
107 #define TOKTSEM 62 /* ; */
108 #define TOKTCOMMA 63
109 #define TOKTDOT 64 /* . */
110 #define TOKTOR 65 /* | or "if" */
111 #define TOKTAND 66 /* & or "and" */
112 #define TOKTIF 67 /* keywords */
113 #define TOKTELSE 68
114 #define TOKTWHILE 69
115 #define TOKTFUNCTION 70
116 #define TOKTRETURN 71
117 #define TOKTLOCAL 72
118 #define TOKTOBJECT 73
119 #define TOKTBREAK 74
120 #define TOKTCONTINUE 75
121 #define TOKTLIST 76 /* a list */
122 #define TOKTNIL 77
123 #define TOKTTRUE 78
124 #define TOKTPASS 79
125 #define TOKTCLASS 80
126 #define TOKTEXIT 81
127 #define TOKTABORT 82
128 #define TOKTASKDO 83
129 #define TOKTASKIO 84
130 #define TOKTPOUND 85 /* # */
131 #define TOKTQUESTION 86 /* ? */
132 #define TOKTCOMPOUND 87
133 #define TOKTIOSYN 88
134 #define TOKTDOSYN 89
135 #define TOKTEXTERN 90
136 #define TOKTFORMAT 91
137 #define TOKTDO 92
138 #define TOKTFOR 93
139 #define TOKTNEW 94
140 #define TOKTDELETE 95
141
142 /* assignment operators - keep these together */
143 #define TOKTINC 150 /* ++ */
144 #define TOKTPOSTINC 151 /* MUST BE TOKTINC + 1 */
145 #define TOKTDEC 152 /* -- */
146 #define TOKTPOSTDEC 153 /* MUST BE TOKTDEC + 1 */
147 #define TOKTPLEQ 154 /* += */
148 #define TOKTMINEQ 155 /* -= */
149 #define TOKTDIVEQ 156 /* /= */
150 #define TOKTTIMEQ 157 /* *= */
151 #define TOKTASSIGN 158 /* simple assignment */
152 #define TOKTMODEQ 159 /* %= (mod and assign) operator */
153 #define TOKTBANDEQ 160 /* &= */
154 #define TOKTBOREQ 161 /* |= */
155 #define TOKTXOREQ 162 /* ^= (xor and assign) */
156 #define TOKTSHLEQ 163 /* <<= (shift left and assign) */
157 #define TOKTSHREQ 164 /* >>= (shift right and assign */
158
159 #define TOKTSWITCH 200
160 #define TOKTCASE 201
161 #define TOKTDEFAULT 202
162 #define TOKTGOTO 203
163 #define TOKTELLIPSIS 204 /* ... */
164 #define TOKTSPECIAL 205 /* "specialWords" */
165 #define TOKTREPLACE 206 /* replace */
166 #define TOKTMODIFY 207 /* modify */
167
168 #define TOKTEQEQ 208 /* the '==' operator */
169 #define TOKTPOINTER 209 /* the -> operator */
170
171 /* the longest a symbol name can be */
172 #define TOKNAMMAX 39
173
174 /* symbol table entry */
175 typedef struct toksdef toksdef;
176 struct toksdef
177 {
178 uchar tokstyp; /* type of the symbol */
179 uchar tokshsh; /* hash value of symbol */
180 ushort toksval; /* value of the symbol (depends on type) */
181 ushort toksfr; /* frame offset of symbol (for debugger) */
182 uchar tokslen; /* length of the symbol's name */
183 char toksnam[TOKNAMMAX]; /* name of symbol */
184 };
185
186 /* symbol table entry without 'name' portion - for allocation purposes */
187 struct toks1def
188 {
189 uchar tokstyp;
190 uchar tokshsh;
191 ushort toksval;
192 ushort toksfr;
193 uchar tokslen;
194 char toksnam[1];
195 };
196 typedef struct toks1def toks1def;
197
198 /* generic symbol table object - other symbol tables are subclasses */
199 typedef struct toktdef toktdef;
200 struct toktdef
201 {
202 void (*toktfadd)(toktdef *tab, char *name, int namel, int typ,
203 int val, int hash); /* add symbol */
204 int (*toktfsea)(toktdef *tab, char *name, int namel, int hash,
205 toksdef *ret); /* search symbol table */
206 void (*toktfset)(toktdef *tab, toksdef *sym);
207 /* update val & typ of symbol to those in *sym */
208 void (*toktfeach)(toktdef *tab,
209 void (*fn)(void *ctx, toksdef *sym),
210 void *fnctx); /* call fn for each sym */
211 toktdef *toktnxt; /* next symbol table to be searched */
212 errcxdef *tokterr; /* error handling context */
213 };
214
215 /* maximum number of pools (TOKTSIZE bytes each) for symbols */
216 #define TOKPOOLMAX 128
217
218 /* pointer to a symbol in a hashed symbol table */
219 struct tokthpdef
220 {
221 mcmon tokthpobj; /* cache manager object number of page */
222 uint tokthpofs; /* offset within page of this symbol */
223 };
224 typedef struct tokthpdef tokthpdef;
225
226 /* extended symbol entry in a hashed symbol table */
227 struct tokshdef
228 {
229 tokthpdef tokshnxt; /* pointer to next symbol in the table */
230 toksdef tokshsc; /* superclass - normal symbol entry */
231 };
232 typedef struct tokshdef tokshdef;
233
234 /* hashing symbol table (subclass of generic symbol table) */
235 struct tokthdef
236 {
237 toktdef tokthsc; /* generic symbol table superclass data */
238 mcmcxdef *tokthmem; /* memory manager context */
239 tokthpdef tokthhsh[TOKHASHSIZE]; /* hash table */
240 uint tokthpcnt; /* number of memory pools for toksdef's */
241 mcmon tokthpool[TOKPOOLMAX]; /* memory pools for toksdef's */
242 uint tokthfinal[TOKPOOLMAX]; /* actual sizes of these pools */
243 uchar *tokthcpool; /* current pool pointer */
244 ushort tokthsize; /* remaining size of top memory pool */
245 ushort tokthofs; /* allocation offset in top memory pool */
246 };
247 typedef struct tokthdef tokthdef;
248
249 /* size of toksdef pools to allocate for hashed symbol tables */
250 #define TOKTHSIZE 4096
251
252 /*
253 * Linear cache-object-embedded symbol table. This type of symbol
254 * table is used for frame parameter/local variable lists. It is best
255 * for small tables, because it isn't broken up into hash buckets, so it
256 * is searched linearly. As a result, it's small enough to be embedded
257 * in code.
258 */
259 struct toktldef
260 {
261 toktdef toktlsc; /* generic symbol table superclass data */
262 uchar *toktlptr; /* base of linear symbol table */
263 uchar *toktlnxt; /* next free byte in table */
264 uint toktlcnt; /* number of objects in the table */
265 uint toktlsiz; /* bytes remaining in the table */
266 };
267 typedef struct toktldef toktldef;
268
269 struct tokdef
270 {
271 int toktyp; /* type of the token */
272 int toklen; /* length of token text, if a symbolic token */
273 long tokval; /* numeric value, if applicable */
274 ushort tokofs;
275 uint tokhash; /* token hash value, if a symbolic token */
276 char toknam[TOKNAMMAX+1]; /* text of token, if a symbolic token */
277 toksdef toksym; /* symbol from table matching token */
278 };
279 typedef struct tokdef tokdef;
280
281 /* special character sequence */
282 #define TOKSCMAX 3 /* maximum length of a special char sequence */
283 typedef struct tokscdef tokscdef;
284 struct tokscdef
285 {
286 tokscdef *tokscnxt; /* next sequence with same first character */
287 int toksctyp; /* token type corresponding to sequence */
288 int toksclen; /* length of the sequence */
289 char tokscstr[TOKSCMAX+1]; /* the sequence itself */
290 };
291
292 /*
293 * Compare a special character sequence - for efficiency, define
294 * something special for the maximum length available (TOKSCMAX).
295 * Note that the first character will always be equal, or the
296 * string wouldn't even get to the point of being tested by this
297 * macro.
298 */
299 #if TOKSCMAX == 3
300 # define toksceq(str1, str2, len1, len2) \
301 ((len2) >= (len1) \
302 && ((len1) == 1 \
303 || ((str1)[1] == (str2)[1] \
304 && ((len1) == 2 \
305 || (str1)[2] == (str2)[2]))))
306 #endif /* TOKSCMAX == 3 */
307 #ifndef toksceq
308 # define toksceq(str1, str2, len) (!memcmp(str1, str2, (size_t)(len)))
309 #endif /* toksceq */
310
311 /* special character sequence list table entry */
312 struct tokldef
313 {
314 int tokltyp; /* token type corresponding to sequence */
315 char toklstr[TOKSCMAX+1]; /* the text of the sequence */
316 };
317 typedef struct tokldef tokldef;
318
319 /* include path structure */
320 typedef struct tokpdef tokpdef;
321 struct tokpdef
322 {
323 tokpdef *tokpnxt; /* next path in list */
324 int tokplen; /* length of directory name */
325 char tokpdir[1]; /* directory to search */
326 };
327
328 /* #define symbol structure */
329 typedef struct tokdfdef tokdfdef;
330 struct tokdfdef
331 {
332 tokdfdef *nxt; /* next symbol in the same hash chain */
333 char *nm; /* name of the symbol */
334 int len; /* length of the symbol */
335 int explen; /* length of the expansion */
336 char expan[1]; /* expansion buffer */
337 };
338
339 /* #define hash table information */
340 #define TOKDFHSHSIZ 64
341 #define TOKDFHSHMASK 63
342
343 /* maximum #if nesting */
344 #define TOKIFNEST 64
345
346 /* #if state */
347 #define TOKIF_IF_YES 1 /* processing a true #if/#ifdef block */
348 #define TOKIF_IF_NO 2 /* processing a false #if/#ifdef block */
349 #define TOKIF_ELSE_YES 3 /* processing a true #else part */
350 #define TOKIF_ELSE_NO 4 /* processing a false #else part */
351
352 /* maximum macro expansion nesting */
353 #define TOKMACNEST 20
354
355 /* lexical analysis context */
356 struct tokcxdef
357 {
358 errcxdef *tokcxerr; /* error handling context */
359 mcmcxdef *tokcxmem; /* cache manager context */
360 struct dbgcxdef *tokcxdbg; /* debugger context */
361 lindef *tokcxlin; /* line source */
362 tokpdef *tokcxinc; /* head of include path list */
363 toktdef *tokcxstab; /* current head of symbol table chain */
364 void *tokcxscx; /* context for string storage callback functions */
365 ushort (*tokcxsst)(void *ctx);
366 /* start storing a string; return offset of string's storage */
367 void (*tokcxsad)(void *ctx, char *str, ushort len);
368 /* add characters to a string */
369 void (*tokcxsend)(void *ctx); /* finish storing string */
370 char *tokcxmsav[TOKMACNEST]; /* saved positions for macro expansion */
371 ushort tokcxmsvl[TOKMACNEST]; /* saved lengths for macro expansion */
372 int tokcxmlvl; /* macro nesting level */
373 int tokcxflg; /* flags */
374 # define TOKCXFINMAC 0x01 /* doing <<expr>> macro expansion */
375 # define TOKCXCASEFOLD 0x02 /* fold upper and lower case */
376 # define TOKCXFCMODE 0x04 /* parse using C operators */
377 # define TOKCXF_EMBED_PAREN_PRE 0x08 /* embedded expr - did '(' */
378 # define TOKCXF_EMBED_PAREN_AFT 0x10 /* embedded expr - must do ')' */
379 # define TOKCXFLIN2 0x20 /* new-style line records */
380 tokdef tokcxcur; /* current token */
381 char *tokcxbuf; /* buffer for long lines */
382 ushort tokcxbsz; /* size of long line buffer */
383 char *tokcxptr; /* pointer into line source */
384 ushort tokcxlen; /* length of text in buffer */
385 uchar tokcxinx[256]; /* special character indices */
386 tokdfdef *tokcxdf[TOKDFHSHSIZ]; /* hash table for #define symbols */
387 int tokcxifcnt; /* number of #endif's we expect to find */
388 char tokcxif[TOKIFNEST]; /* #if state for each nesting level */
389 int tokcxifcur; /* current #if state, obeying nesting */
390 struct linfdef *tokcxhdr; /* list of previously included headers */
391 tokscdef *tokcxsc[1]; /* special character table */
392 };
393 typedef struct tokcxdef tokcxdef;
394
395
396 /* allocate and initialize a lexical analysis context */
397 tokcxdef *tokcxini(errcxdef *errctx, mcmcxdef *mctx, tokldef *sctab);
398
399 /* add an include path to a token handling context */
400 void tokaddinc(tokcxdef *ctx, char *path, int pathlen);
401
402 /* compute the hash value of a string */
403 uint tokhsh(char *nam);
404
405 /*
406 * Fold case of a token if we're in case-insensitive mode. This should
407 * be called any time a token is constructed artificially; it need not
408 * be used the token is read through the tokenizer, because the
409 * tokenizer will always adjust a token as needed before returning it.
410 */
411 void tok_case_fold(tokcxdef *ctx, tokdef *tok);
412
413 /* initialize a hashed symbol table */
414 void tokthini(errcxdef *errctx, mcmcxdef *memctx, toktdef *toktab1);
415
416 /* add a symbol to a hashed symbol table */
417 void tokthadd(toktdef *toktab, char *name, int namel,
418 int typ, int val, int hash);
419
420 /* update a symbol in a hashed symbol table */
421 void tokthset(toktdef *toktab, toksdef *sym);
422
423 /* search a hashed symbol table for a symbol */
424 int tokthsea(toktdef *tab, char *name, int namel, int hash,
425 toksdef *ret);
426
427 /* call a function for each symbol in a hashed symbol table */
428 void toktheach(toktdef *tab, void (*cb)(void *ctx, toksdef *sym),
429 void *ctx);
430
431 /* find a symbol given type and value */
432 int tokthfind(toktdef *tab, int typ, uint val, toksdef *sym);
433
434 /* initialize a linear symbol table */
435 void toktlini(errcxdef *errctx, toktldef *toktab,
436 uchar *mem, uint siz);
437
438 /* add a symbol to a linear symbol table */
439 void toktladd(toktdef *toktab, char *name, int namel,
440 int typ, int val, int hash);
441
442 /* search a linear symbol table */
443 int toktlsea(toktdef *tab, char *name, int namel, int hash,
444 toksdef *ret);
445
446 /* update a symbol in a linear symbol table */
447 void toktlset(toktdef *toktab, toksdef *sym);
448
449 /* call a function for each symbol in a local symbol table */
450 void toktleach(toktdef *tab, void (*cb)(void *ctx, toksdef *sym),
451 void *ctx);
452
453 /* delete all symbols from a linear table */
454 void toktldel(toktldef *tab);
455
456 /* get next token, removing it from input stream */
457 int toknext(tokcxdef *ctx);
458
459 /* general function to get/peek at next token */
460 int tokget1(tokcxdef *ctx, tokdef *tok, int consume);
461
462 /* add a symbol to the #define symbol table */
463 void tok_add_define(tokcxdef *ctx, char *sym, int len,
464 char *expan, int explen);
465
466 /*
467 * add a symbol to the #define symbol table, folding case if we're
468 * operating in case-insensitive mode
469 */
470 void tok_add_define_cvtcase(tokcxdef *ctx, char *sym, int len,
471 char *expan, int explen);
472
473 /* add a symbol to the #define symbol table as a number */
474 void tok_add_define_num_cvtcase(tokcxdef *ctx, char *sym, int len, int num);
475
476 /* undefine a #define symbol */
477 void tok_del_define(tokcxdef *ctx, char *sym, int len);
478
479 /* read/write preprocessor symbols from/to a file */
480 void tok_read_defines(tokcxdef *ctx, osfildef *fp, errcxdef *ec);
481
482 /* write preprocessor state to a file */
483 void tok_write_defines(tokcxdef *ctx, osfildef *fp, errcxdef *ec);
484
485
486 /* determine if a char is a valid non-initial character in a symbol name */
487 #define TOKISSYM(c) \
488 (isalpha((uchar)(c)) || isdigit((uchar)(c)) || (c)=='_' || (c)=='$')
489
490 /* numeric conversion and checking macros */
491 #define TOKISHEX(c) \
492 (isdigit((uchar)(c))||((c)>='a'&&(c)<='f')||((c)>='A'&&(c)<='F'))
493 #define TOKISOCT(c) \
494 (isdigit((uchar)(c))&&!((c)=='8'||(c)=='9'))
495
496 #define TOKHEX2INT(c) \
497 (isdigit((uchar)c)?(c)-'0':((c)>='a'?(c)-'a'+10:(c)-'A'+10))
498 #define TOKOCT2INT(c) ((c)-'0')
499 #define TOKDEC2INT(c) ((c)-'0')
500
501 #endif /* TOK_INCLUDED */