| 1 | /* $Header: d:/cvsroot/tads/tads3/tctok.h,v 1.5 1999/07/11 00:46:59 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | tctok.h - TADS3 compiler tokenizer and preprocessor |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | The tokenizer is layered with the preprocessor, so that the preprocessor |
| 16 | can deal with include files, macro expansion, and preprocessor directives. |
| 17 | Modified |
| 18 | 04/12/99 MJRoberts - Creation |
| 19 | */ |
| 20 | |
| 21 | #ifndef TCTOK_H |
| 22 | #define TCTOK_H |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "os.h" |
| 29 | #include "t3std.h" |
| 30 | #include "utf8.h" |
| 31 | #include "vmhash.h" |
| 32 | #include "vmerr.h" |
| 33 | #include "tcerr.h" |
| 34 | #include "tcerrnum.h" |
| 35 | |
| 36 | |
| 37 | /* ------------------------------------------------------------------------ */ |
| 38 | /* |
| 39 | * Constants |
| 40 | */ |
| 41 | |
| 42 | /* maximum length of a symbol name, in characters */ |
| 43 | const size_t TOK_SYM_MAX_LEN = 80; |
| 44 | |
| 45 | /* |
| 46 | * Maximum buffer required to hold a symbol, in bytes. Each UTF-8 |
| 47 | * character may take up three bytes, plus we need a null terminator |
| 48 | * byte. |
| 49 | */ |
| 50 | const size_t TOK_SYM_MAX_BUFFER = (3*TOK_SYM_MAX_LEN + 1); |
| 51 | |
| 52 | /* maximum #if nesting level */ |
| 53 | const size_t TOK_MAX_IF_NESTING = 100; |
| 54 | |
| 55 | /* maximum number of parameters per macro */ |
| 56 | const int TOK_MAX_MACRO_ARGS = 128; |
| 57 | |
| 58 | /* |
| 59 | * Special token flag characters - these are a characters that can't |
| 60 | * occur in an input file (we guarantee this by converting any |
| 61 | * occurrences of this character to a space on reading input). We use |
| 62 | * these to flag certain special properties of tokens in the input |
| 63 | * buffer. |
| 64 | * |
| 65 | * We use ASCII characters in the control range (0x01 (^A) through 0x1A |
| 66 | * (^Z), excluding 0x09 (tab), 0x0A (LF), 0x0D (CR), and 0x0C (Page |
| 67 | * Feed); a well-formed source file would never use any of these |
| 68 | * characters in input. Even if it does, we won't get confused, since |
| 69 | * we'll always translate these to a space if we find them in input; but |
| 70 | * choosing characters that *should* never occur in valid input will |
| 71 | * ensure that we never alter the meaning of valid source by this |
| 72 | * translation. |
| 73 | */ |
| 74 | |
| 75 | /* |
| 76 | * macro parameter flag - we use this in the internal storage of a |
| 77 | * #define expansion to flag where the formal parameters are mentioned, |
| 78 | * so that we can substitute the actuals when expanding the macro |
| 79 | */ |
| 80 | const char TOK_MACRO_FORMAL_FLAG = 0x01; |
| 81 | |
| 82 | /* |
| 83 | * Token fully expanded flag. Whenever we detect that a particular |
| 84 | * token has been fully expanded in the course of a particular macro |
| 85 | * expansion, we'll insert this byte before the token; on subsequent |
| 86 | * re-scans, whenever we see this flag, we'll realize that the token |
| 87 | * needs no further consideration of expansion. |
| 88 | */ |
| 89 | const char TOK_FULLY_EXPANDED_FLAG = 0x02; |
| 90 | |
| 91 | /* |
| 92 | * Macro substitution end marker. Each time we expand a macro, we'll |
| 93 | * insert immediately after the macro expansion a special pseudo-token, |
| 94 | * consisting of this flag followed by a pointer to the symbol table |
| 95 | * entry for the symbol expanded. As we expand macros, we'll check to |
| 96 | * see if any of these special flags appear in the buffer after the |
| 97 | * macro about to be expanded. If we find such a flag matching the |
| 98 | * symbol about to be expanded, we'll know the symbol has already been |
| 99 | * fully expanded on a previous scan and thus must not be expanded |
| 100 | * again. |
| 101 | */ |
| 102 | const char TOK_MACRO_EXP_END = 0x03; |
| 103 | |
| 104 | /* |
| 105 | * End-of-line flag. This serves as a local end-of-file marker for |
| 106 | * preprocessor lines. Because preprocessor lines must be considered in |
| 107 | * isolation, we need some way when parsing one to tell the tokenizer |
| 108 | * not to try to read another line when it reaches the end of the |
| 109 | * current line. This flag serves this purpose: when the tokenizer |
| 110 | * encounters one of these flags, it will simply return end-of-file |
| 111 | * until the caller explicitly reads a new source line. |
| 112 | */ |
| 113 | const char TOK_END_PP_LINE = 0x04; |
| 114 | |
| 115 | /* |
| 116 | * "#foreach" marker flag. This marks the presence of a #foreach token in |
| 117 | * a macro's expansion. We leave the text of the expansion area intact, |
| 118 | * but we replace the #foreach token with this marker character. |
| 119 | */ |
| 120 | const char TOK_MACRO_FOREACH_FLAG = 0x05; |
| 121 | |
| 122 | /* |
| 123 | * "#argcount" marker flag. This marks the presence of a #argcount token |
| 124 | * in a macro's expansion. |
| 125 | */ |
| 126 | const char TOK_MACRO_ARGCOUNT_FLAG = 0x06; |
| 127 | |
| 128 | /* |
| 129 | * "#ifempty" and #ifnempty" marker flags |
| 130 | */ |
| 131 | const char TOK_MACRO_IFEMPTY_FLAG = 0x07; |
| 132 | const char TOK_MACRO_IFNEMPTY_FLAG = 0x08; |
| 133 | |
| 134 | /* |
| 135 | * Macro format version number. The compiler sets up a predefined macro |
| 136 | * (__TADS_MACRO_FORMAT_VERSION) with this information. Since 3.1, the |
| 137 | * macro table is visible to user code via t3GetGlobalSymbols() (using the |
| 138 | * T3PreprocMacros selector), and this information includes the parsed |
| 139 | * format with the embedded flag codes. The macro information can be used |
| 140 | * in DynamicFunc compilation at run-time. If we ever make incompatible |
| 141 | * changes to the internal format, future interpreters will have to |
| 142 | * recognize older versions so that they can make the necessary |
| 143 | * translations. By embedding the version information in the table, we |
| 144 | * make this recognition possible. |
| 145 | */ |
| 146 | #define TCTOK_MACRO_FORMAT_VERSION 1 |
| 147 | |
| 148 | |
| 149 | /* ------------------------------------------------------------------------ */ |
| 150 | /* |
| 151 | * Macro table. This is a virtualized version of our basic hash table, to |
| 152 | * allow specialized versions that provide views on top of other table |
| 153 | * structures. |
| 154 | */ |
| 155 | class CTcMacroTable |
| 156 | { |
| 157 | public: |
| 158 | virtual ~CTcMacroTable() { } |
| 159 | |
| 160 | /* add an entry */ |
| 161 | virtual void add(CVmHashEntry *entry) = 0; |
| 162 | |
| 163 | /* remove an entry */ |
| 164 | virtual void remove(CVmHashEntry *entry) = 0; |
| 165 | |
| 166 | /* find an entry */ |
| 167 | virtual class CVmHashEntry *find(const char *str, size_t len) = 0; |
| 168 | |
| 169 | /* enumerate entries */ |
| 170 | virtual void enum_entries( |
| 171 | void (*func)(void *ctx, class CVmHashEntry *entry), void *ctx) = 0; |
| 172 | |
| 173 | /* dump the hash table for debugging purposes */ |
| 174 | virtual void debug_dump() = 0; |
| 175 | }; |
| 176 | |
| 177 | /* ------------------------------------------------------------------------ */ |
| 178 | /* |
| 179 | * #if state |
| 180 | */ |
| 181 | enum tok_if_t |
| 182 | { |
| 183 | TOKIF_NONE, /* not in a #if block at all */ |
| 184 | TOKIF_IF_YES, /* processing a true #if branch */ |
| 185 | TOKIF_IF_NO, /* processing a false #if branch */ |
| 186 | TOKIF_IF_DONE, /* done with true #if/#elif; skip #elif's and #else */ |
| 187 | TOKIF_ELSE_YES, /* processing a true #else branch */ |
| 188 | TOKIF_ELSE_NO /* processing a false #else branch */ |
| 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * #if stack entry |
| 193 | */ |
| 194 | struct tok_if_info_t |
| 195 | { |
| 196 | /* state */ |
| 197 | tok_if_t state; |
| 198 | |
| 199 | /* file descriptor and line number of starting #if */ |
| 200 | class CTcTokFileDesc *desc; |
| 201 | long linenum; |
| 202 | }; |
| 203 | |
| 204 | /* ------------------------------------------------------------------------ */ |
| 205 | /* |
| 206 | * Token Types |
| 207 | */ |
| 208 | |
| 209 | enum tc_toktyp_t |
| 210 | { |
| 211 | TOKT_INVALID, /* invalid token */ |
| 212 | TOKT_NULLTOK, /* null token - caller should read another token */ |
| 213 | TOKT_EOF, /* end of file */ |
| 214 | TOKT_MACRO_FORMAL, /* formal parameter replacement placeholder */ |
| 215 | TOKT_MACRO_FOREACH, /* macro varargs #foreach placeholder */ |
| 216 | TOKT_MACRO_ARGCOUNT, /* macro varargs #argcount placeholder */ |
| 217 | TOKT_MACRO_IFEMPTY, /* #ifempty macro placeholder */ |
| 218 | TOKT_MACRO_IFNEMPTY, /* #ifnempty macro placeholder */ |
| 219 | TOKT_SYM, /* symbolic name */ |
| 220 | TOKT_INT, /* integer */ |
| 221 | TOKT_SSTR, /* single-quoted string */ |
| 222 | TOKT_SSTR_START, /* start of an sstring with embedding - '...<< */ |
| 223 | TOKT_SSTR_MID, /* middle of an sstring with embedding - >>...<< */ |
| 224 | TOKT_SSTR_END, /* end of an sstring with embedding - >>...' */ |
| 225 | TOKT_DSTR, /* double-quoted string */ |
| 226 | TOKT_DSTR_START, /* start of a dstring with embedding - "...<< */ |
| 227 | TOKT_DSTR_MID, /* middle of a dstring with embedding - >>...<< */ |
| 228 | TOKT_DSTR_END, /* end of a dstring with embedding - >>..." */ |
| 229 | TOKT_RESTR, /* regular expression string - R'...' or R"..." */ |
| 230 | TOKT_LPAR, /* left paren '(' */ |
| 231 | TOKT_RPAR, /* right paren ')' */ |
| 232 | TOKT_COMMA, /* comma ',' */ |
| 233 | TOKT_DOT, /* period '.' */ |
| 234 | TOKT_LBRACE, /* left brace '{' */ |
| 235 | TOKT_RBRACE, /* right brace '}' */ |
| 236 | TOKT_LBRACK, /* left square bracket '[' */ |
| 237 | TOKT_RBRACK, /* right square bracket ']' */ |
| 238 | TOKT_EQ, /* equals sign '=' */ |
| 239 | TOKT_EQEQ, /* double-equals sign '==' */ |
| 240 | TOKT_ASI, /* colon-equals assignment operator ':=' */ |
| 241 | TOKT_PLUS, /* plus sign '+' */ |
| 242 | TOKT_MINUS, /* minus sign '-' */ |
| 243 | TOKT_TIMES, /* multiplication symbol '*' */ |
| 244 | TOKT_DIV, /* division symbol '/' */ |
| 245 | TOKT_MOD, /* modulo '%' */ |
| 246 | TOKT_GT, /* greater-than sign '>' */ |
| 247 | TOKT_LT, /* less-than sign '<' */ |
| 248 | TOKT_GE, /* greater-or-equal sign '>=' */ |
| 249 | TOKT_LE, /* less-or-equal sign '<=' */ |
| 250 | TOKT_NE, /* not-equals sign '!=' or '<>' */ |
| 251 | TOKT_ARROW, /* arrow symbol '->' */ |
| 252 | TOKT_COLON, /* colon ':' */ |
| 253 | TOKT_SEM, /* semicolon ';' */ |
| 254 | TOKT_AND, /* bitwise AND '&' */ |
| 255 | TOKT_ANDAND, /* logical AND '&&' */ |
| 256 | TOKT_OR, /* bitwise OR '|' */ |
| 257 | TOKT_OROR, /* logical OR '||' */ |
| 258 | TOKT_XOR, /* bitwise XOR '^' */ |
| 259 | TOKT_SHL, /* shift left '<<' */ |
| 260 | TOKT_ASHR, /* arithmetic shift right '>>' */ |
| 261 | TOKT_LSHR, /* logical shift right '>>>' */ |
| 262 | TOKT_INC, /* increment '++' */ |
| 263 | TOKT_DEC, /* decrement '--' */ |
| 264 | TOKT_PLUSEQ, /* plus-equals '+=' */ |
| 265 | TOKT_MINEQ, /* minus-equals '-=' */ |
| 266 | TOKT_TIMESEQ, /* times-equals '*=' */ |
| 267 | TOKT_DIVEQ, /* divide-equals '/=' */ |
| 268 | TOKT_MODEQ, /* mod-equals '%=' */ |
| 269 | TOKT_ANDEQ, /* and-equals '&=' */ |
| 270 | TOKT_OREQ, /* or-equals '|=' */ |
| 271 | TOKT_XOREQ, /* xor-equals '^=' */ |
| 272 | TOKT_SHLEQ, /* shift-left-and-assign '<<=' */ |
| 273 | TOKT_ASHREQ, /* arithmetic shift-right-and-assign '>>=' */ |
| 274 | TOKT_LSHREQ, /* logical shift-right-and-assign '>>>=' */ |
| 275 | TOKT_NOT, /* logical not '!' */ |
| 276 | TOKT_BNOT, /* bitwise not '~' */ |
| 277 | TOKT_POUND, /* pound '#' */ |
| 278 | TOKT_POUNDPOUND, /* double-pound '##' */ |
| 279 | TOKT_POUNDAT, /* pound-at '#@' */ |
| 280 | TOKT_ELLIPSIS, /* ellipsis '...' */ |
| 281 | TOKT_QUESTION, /* question mark '?' */ |
| 282 | TOKT_QQ, /* double question mark '??' */ |
| 283 | TOKT_COLONCOLON, /* double-colon '::' */ |
| 284 | TOKT_FLOAT, /* floating-point number */ |
| 285 | TOKT_BIGINT, /* an integer promoted to a float due to overflow */ |
| 286 | TOKT_AT, /* at-sign */ |
| 287 | TOKT_DOTDOT, /* range marker '..' */ |
| 288 | TOKT_FMTSPEC, /* sprintf format spec for <<%fmt expr>> */ |
| 289 | |
| 290 | /* keywords */ |
| 291 | TOKT_SELF, |
| 292 | TOKT_INHERITED, |
| 293 | TOKT_ARGCOUNT, |
| 294 | TOKT_IF, |
| 295 | TOKT_ELSE, |
| 296 | TOKT_FOR, |
| 297 | TOKT_WHILE, |
| 298 | TOKT_DO, |
| 299 | TOKT_SWITCH, |
| 300 | TOKT_CASE, |
| 301 | TOKT_DEFAULT, |
| 302 | TOKT_GOTO, |
| 303 | TOKT_BREAK, |
| 304 | TOKT_CONTINUE, |
| 305 | TOKT_FUNCTION, |
| 306 | TOKT_RETURN, |
| 307 | TOKT_LOCAL, |
| 308 | TOKT_OBJECT, |
| 309 | TOKT_NIL, |
| 310 | TOKT_TRUE, |
| 311 | TOKT_PASS, |
| 312 | TOKT_EXTERNAL, |
| 313 | TOKT_EXTERN, |
| 314 | TOKT_FORMATSTRING, |
| 315 | TOKT_CLASS, |
| 316 | TOKT_REPLACE, |
| 317 | TOKT_MODIFY, |
| 318 | TOKT_NEW, |
| 319 | TOKT_DELETE, |
| 320 | TOKT_THROW, |
| 321 | TOKT_TRY, |
| 322 | TOKT_CATCH, |
| 323 | TOKT_FINALLY, |
| 324 | TOKT_INTRINSIC, |
| 325 | TOKT_DICTIONARY, |
| 326 | TOKT_GRAMMAR, |
| 327 | TOKT_ENUM, |
| 328 | TOKT_TEMPLATE, |
| 329 | TOKT_STATIC, |
| 330 | TOKT_FOREACH, |
| 331 | TOKT_EXPORT, |
| 332 | TOKT_DELEGATED, |
| 333 | TOKT_TARGETPROP, |
| 334 | TOKT_PROPERTYSET, |
| 335 | TOKT_TARGETOBJ, |
| 336 | TOKT_DEFININGOBJ, |
| 337 | TOKT_TRANSIENT, |
| 338 | TOKT_REPLACED, |
| 339 | TOKT_PROPERTY, |
| 340 | TOKT_OPERATOR, |
| 341 | TOKT_METHOD, |
| 342 | TOKT_INVOKEE |
| 343 | |
| 344 | /* type names - formerly reserved but later withdrawn */ |
| 345 | // TOKT_VOID, |
| 346 | // TOKT_INTKW, |
| 347 | // TOKT_STRING, |
| 348 | // TOKT_LIST, |
| 349 | // TOKT_BOOLEAN, |
| 350 | // TOKT_ANY |
| 351 | }; |
| 352 | |
| 353 | /* ------------------------------------------------------------------------ */ |
| 354 | /* |
| 355 | * Source Block. As we read the source file, we need to keep quoted |
| 356 | * strings and symbol names around for later reference, in case they're |
| 357 | * needed after reading more tokens and flushing the line buffer. We'll |
| 358 | * copy needed text into our source blocks, which we keep in memory |
| 359 | * throughout the compilation, so that we can be certain we can |
| 360 | * reference these strings at any time. |
| 361 | */ |
| 362 | |
| 363 | /* size of a source block */ |
| 364 | const size_t TCTOK_SRC_BLOCK_SIZE = 50000; |
| 365 | |
| 366 | /* source block class */ |
| 367 | class CTcTokSrcBlock |
| 368 | { |
| 369 | public: |
| 370 | CTcTokSrcBlock() |
| 371 | { |
| 372 | /* no next block yet */ |
| 373 | nxt_ = 0; |
| 374 | } |
| 375 | |
| 376 | ~CTcTokSrcBlock() |
| 377 | { |
| 378 | /* delete the next block in line */ |
| 379 | if (nxt_ != 0) |
| 380 | delete nxt_; |
| 381 | } |
| 382 | |
| 383 | /* get/set the next block */ |
| 384 | CTcTokSrcBlock *get_next() const { return nxt_; } |
| 385 | void set_next(CTcTokSrcBlock *blk) { nxt_ = blk; } |
| 386 | |
| 387 | /* get a pointer to the block's buffer */ |
| 388 | char *get_buf() { return buf_; } |
| 389 | |
| 390 | private: |
| 391 | /* the next block in the list */ |
| 392 | CTcTokSrcBlock *nxt_; |
| 393 | |
| 394 | /* bytes of the list entry */ |
| 395 | char buf_[TCTOK_SRC_BLOCK_SIZE]; |
| 396 | }; |
| 397 | |
| 398 | |
| 399 | /* ------------------------------------------------------------------------ */ |
| 400 | /* |
| 401 | * String Buffer. We use these buffers for reading input lines and |
| 402 | * expanding macros. |
| 403 | */ |
| 404 | class CTcTokString |
| 405 | { |
| 406 | public: |
| 407 | CTcTokString() |
| 408 | { |
| 409 | /* no buffer yet */ |
| 410 | buf_ = 0; |
| 411 | buf_len_ = 0; |
| 412 | buf_size_ = 0; |
| 413 | } |
| 414 | |
| 415 | virtual ~CTcTokString() |
| 416 | { |
| 417 | /* delete our buffer */ |
| 418 | if (buf_ != 0) |
| 419 | t3free(buf_); |
| 420 | } |
| 421 | |
| 422 | /* ensure that a given amount of space if available */ |
| 423 | virtual void ensure_space(size_t siz) |
| 424 | { |
| 425 | /* make sure there's room for the requested size plus a null byte */ |
| 426 | if (buf_size_ < siz + 1) |
| 427 | { |
| 428 | /* increase to the next 4k increment */ |
| 429 | buf_size_ = (siz + 4095 + 1) & ~4095; |
| 430 | |
| 431 | /* allocate or re-allocate the buffer */ |
| 432 | if (buf_ == 0) |
| 433 | buf_ = (char *)t3malloc(buf_size_); |
| 434 | else |
| 435 | buf_ = (char *)t3realloc(buf_, buf_size_); |
| 436 | |
| 437 | /* throw an error if that failed */ |
| 438 | if (buf_ == 0) |
| 439 | err_throw(TCERR_NO_STRBUF_MEM); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /* expand the buffer */ |
| 444 | void expand() |
| 445 | { |
| 446 | /* expand to the next 4k increment */ |
| 447 | ensure_space(buf_size_ + 4096); |
| 448 | } |
| 449 | |
| 450 | /* get the text and the length of the text */ |
| 451 | const char *get_text() const { return buf_; } |
| 452 | size_t get_text_len() const { return buf_len_; } |
| 453 | |
| 454 | /* get the end of the text */ |
| 455 | const char *get_text_end() const { return buf_ + buf_len_; } |
| 456 | |
| 457 | /* append text to the buffer */ |
| 458 | virtual void append(const char *p) { append(p, strlen(p)); } |
| 459 | virtual void append(const char *p, size_t len) |
| 460 | { |
| 461 | /* make sure we have space available */ |
| 462 | ensure_space(buf_len_ + len); |
| 463 | |
| 464 | /* copy the text onto the end of our buffer */ |
| 465 | memcpy(buf_ + buf_len_, p, len); |
| 466 | |
| 467 | /* add it to the length of the text */ |
| 468 | buf_len_ += len; |
| 469 | |
| 470 | /* null-terminte it */ |
| 471 | buf_[buf_len_] = '\0'; |
| 472 | } |
| 473 | |
| 474 | /* prepend text */ |
| 475 | virtual void prepend(const char *p) { prepend(p, strlen(p)); } |
| 476 | virtual void prepend(const char *p, size_t len) |
| 477 | { |
| 478 | /* make sure we have enough space */ |
| 479 | ensure_space(buf_len_ + len); |
| 480 | |
| 481 | /* |
| 482 | * move the existing text (including the null terminator) up in the |
| 483 | * buffer to make room for the prepended text |
| 484 | */ |
| 485 | memmove(buf_ + len, buf_, buf_len_ + 1); |
| 486 | |
| 487 | /* copy the new text to the start of the buffer */ |
| 488 | memcpy(buf_, p, len); |
| 489 | |
| 490 | /* count the new size */ |
| 491 | buf_len_ += len; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Append a string to the buffer, enclosing the text in single or |
| 496 | * double quote (as given by 'qu', which must be either '"' or '\'') |
| 497 | * and backslash-escaping any occurrences of the same quote character |
| 498 | * found within the string. |
| 499 | */ |
| 500 | void append_qu(char qu, const char *p) { append_qu(qu, p, strlen(p)); } |
| 501 | void append_qu(char qu, const char *p, size_t len) |
| 502 | { |
| 503 | const char *start; |
| 504 | |
| 505 | /* append the open quote */ |
| 506 | append(&qu, 1); |
| 507 | |
| 508 | /* scan for quotes we'll need to escape */ |
| 509 | while (len != 0) |
| 510 | { |
| 511 | size_t rem; |
| 512 | |
| 513 | /* skip to the next quote */ |
| 514 | for (start = p, rem = len ; rem != 0 && *p != qu ; ++p, --rem) ; |
| 515 | |
| 516 | /* insert the chunk up to the quote */ |
| 517 | if (p != start) |
| 518 | append(start, p - start); |
| 519 | |
| 520 | /* if we did find a quote, append it with a backslash escape */ |
| 521 | if (rem != 0) |
| 522 | { |
| 523 | /* append the backslash and the quote */ |
| 524 | append("\\", 1); |
| 525 | append(&qu, 1); |
| 526 | |
| 527 | /* skip the quote in the source */ |
| 528 | ++p; |
| 529 | --rem; |
| 530 | } |
| 531 | |
| 532 | /* we now only have 'rem' left to consider */ |
| 533 | len = rem; |
| 534 | } |
| 535 | |
| 536 | /* finally, append the closing quote */ |
| 537 | append(&qu, 1); |
| 538 | } |
| 539 | |
| 540 | /* insert text into the buffer at the given offset */ |
| 541 | virtual void insert(int ofs, const char *p, size_t len) |
| 542 | { |
| 543 | /* check to see if there's anything after the insertion point */ |
| 544 | if ((size_t)ofs >= buf_len_) |
| 545 | { |
| 546 | /* |
| 547 | * there's nothing after the insertion point, so this is simply |
| 548 | * equivalent to 'append' - go do the append, and we're done |
| 549 | */ |
| 550 | append(p, len); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | /* ensure there's space for the added text */ |
| 555 | ensure_space(buf_len_ + len); |
| 556 | |
| 557 | /* |
| 558 | * Move the existing text after the insertion point just far enough |
| 559 | * to make room for the new text. Include the null terminator. |
| 560 | */ |
| 561 | memmove(buf_ + ofs + len, buf_ + ofs, buf_len_ - ofs + 1); |
| 562 | |
| 563 | /* copy the new text in at the given offset */ |
| 564 | memcpy(buf_ + ofs, p, len); |
| 565 | |
| 566 | /* include the new text in our length */ |
| 567 | buf_len_ += len; |
| 568 | } |
| 569 | |
| 570 | /* copy text into the buffer, replacing existing text */ |
| 571 | virtual void copy(const char *p, size_t len) |
| 572 | { |
| 573 | /* ensure we have enough space */ |
| 574 | ensure_space(len); |
| 575 | |
| 576 | /* copy the text */ |
| 577 | memcpy(buf_, p, len); |
| 578 | |
| 579 | /* set our length */ |
| 580 | buf_len_ = len; |
| 581 | |
| 582 | /* null-terminate it */ |
| 583 | buf_[buf_len_] = '\0'; |
| 584 | } |
| 585 | |
| 586 | /* clear any existing text */ |
| 587 | virtual void clear_text() |
| 588 | { |
| 589 | /* zero the length */ |
| 590 | buf_len_ = 0; |
| 591 | |
| 592 | /* put a null terminator at the start of the buffer if possible */ |
| 593 | if (buf_size_ > 0) |
| 594 | buf_[0] = '\0'; |
| 595 | } |
| 596 | |
| 597 | /* get the buffer, for copying text directly into it */ |
| 598 | virtual char *get_buf() const { return buf_; } |
| 599 | size_t get_buf_size() const { return buf_size_; } |
| 600 | |
| 601 | /* |
| 602 | * Set the text length - use this after copying directly into the |
| 603 | * buffer to set the length, excluding the null terminator. We'll |
| 604 | * add a null terminator at the given length. |
| 605 | */ |
| 606 | virtual void set_text_len(size_t len) |
| 607 | { |
| 608 | /* set the new length */ |
| 609 | buf_len_ = len; |
| 610 | |
| 611 | /* add a null terminator after the new length */ |
| 612 | if (len < buf_size_) |
| 613 | buf_[len] = '\0'; |
| 614 | } |
| 615 | |
| 616 | protected: |
| 617 | /* buffer */ |
| 618 | char *buf_; |
| 619 | |
| 620 | /* size of the buffer */ |
| 621 | size_t buf_size_; |
| 622 | |
| 623 | /* length of the text in the buffer (excluding trailing null) */ |
| 624 | size_t buf_len_; |
| 625 | }; |
| 626 | |
| 627 | |
| 628 | /* |
| 629 | * String buffer subclass for a non-allocated string that merely |
| 630 | * references another buffer. This can be used anywhere a CTcString is |
| 631 | * required, but does not require any allocation. |
| 632 | * |
| 633 | * These objects can only be used in 'const' contexts: the underlying |
| 634 | * buffer cannot be changed or expanded, since we do not own the |
| 635 | * underlying buffer. |
| 636 | */ |
| 637 | class CTcTokStringRef: public CTcTokString |
| 638 | { |
| 639 | public: |
| 640 | CTcTokStringRef() |
| 641 | { |
| 642 | /* we have no referenced buffer yet */ |
| 643 | buf_ = 0; |
| 644 | buf_size_ = 0; |
| 645 | buf_len_ = 0; |
| 646 | } |
| 647 | |
| 648 | ~CTcTokStringRef() |
| 649 | { |
| 650 | /* we don't own the underlying buffer, so simply forget about it */ |
| 651 | buf_ = 0; |
| 652 | } |
| 653 | |
| 654 | /* we can't make any changes to the underlying buffer */ |
| 655 | void ensure_space(size_t) { } |
| 656 | void append(const char *) { assert(FALSE); } |
| 657 | void append(const char *, size_t) { assert(FALSE); } |
| 658 | void prepend(const char *) { assert(FALSE); } |
| 659 | void prepend(const char *, size_t) { assert(FALSE); } |
| 660 | void insert(int, const char *, size_t) { assert(FALSE); } |
| 661 | void copy(const char *, size_t) { assert(FALSE); } |
| 662 | void clear_text() { assert(FALSE); } |
| 663 | char *get_buf() const { assert(FALSE); return 0; } |
| 664 | void set_text_len(size_t) { assert(FALSE); } |
| 665 | |
| 666 | /* set my underlying buffer */ |
| 667 | void set_buffer(const char *buf, size_t len) |
| 668 | { |
| 669 | buf_ = (char *)buf; |
| 670 | buf_size_ = len + 1; |
| 671 | buf_len_ = len; |
| 672 | } |
| 673 | }; |
| 674 | |
| 675 | /* ------------------------------------------------------------------------ */ |
| 676 | /* |
| 677 | * String embedding context. This keeps track of the token structure for |
| 678 | * embedded expressions within strings using << >>. |
| 679 | */ |
| 680 | struct tok_embed_level |
| 681 | { |
| 682 | /* parenthesis depth within the expression */ |
| 683 | int parens; |
| 684 | |
| 685 | /* token type to switch back to on ending the string */ |
| 686 | tc_toktyp_t endtok; |
| 687 | |
| 688 | /* the quote character for the enclosing string */ |
| 689 | wchar_t qu; |
| 690 | |
| 691 | /* true -> the enclosing string is a triple-quoted string */ |
| 692 | int triple; |
| 693 | |
| 694 | void enter(wchar_t qu, int triple) |
| 695 | { |
| 696 | this->parens = 0; |
| 697 | this->endtok = (qu == '"' ? TOKT_DSTR_END : TOKT_SSTR_END); |
| 698 | this->qu = qu; |
| 699 | this->triple = triple; |
| 700 | } |
| 701 | }; |
| 702 | struct tok_embed_ctx |
| 703 | { |
| 704 | tok_embed_ctx() { reset(); } |
| 705 | |
| 706 | void reset() |
| 707 | { |
| 708 | level = 0; |
| 709 | s = 0; |
| 710 | } |
| 711 | |
| 712 | void start_expr(wchar_t qu, int triple, int report); |
| 713 | |
| 714 | void end_expr() |
| 715 | { |
| 716 | if (level > 0) |
| 717 | --level; |
| 718 | if (level == 0) |
| 719 | s = 0; |
| 720 | else if (level < countof(stk)) |
| 721 | s = stk + level - 1; |
| 722 | } |
| 723 | |
| 724 | /* are we in an embedded expression? */ |
| 725 | int in_expr() const { return level != 0; } |
| 726 | |
| 727 | /* paren nesting at current level */ |
| 728 | int parens() const { return in_expr() ? s->parens : 0; } |
| 729 | |
| 730 | /* inc/dec paren nesting level */ |
| 731 | void parens(int inc) |
| 732 | { |
| 733 | if (in_expr()) |
| 734 | { |
| 735 | if ((s->parens += inc) < 0) |
| 736 | s->parens = 0; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /* ending token type at current level */ |
| 741 | tc_toktyp_t endtok() const { return in_expr() ? s->endtok : TOKT_INVALID; } |
| 742 | |
| 743 | /* ending quote at current level */ |
| 744 | wchar_t qu() const { return in_expr() ? s->qu : 0; } |
| 745 | |
| 746 | /* ending quote is triple quote at current level */ |
| 747 | int triple() const { return in_expr() ? s->triple : FALSE; } |
| 748 | |
| 749 | /* nesting level */ |
| 750 | int level; |
| 751 | |
| 752 | /* stack pointer */ |
| 753 | tok_embed_level *s; |
| 754 | |
| 755 | /* stack */ |
| 756 | tok_embed_level stk[10]; |
| 757 | }; |
| 758 | |
| 759 | /* ------------------------------------------------------------------------ */ |
| 760 | /* |
| 761 | * Token |
| 762 | */ |
| 763 | class CTcToken |
| 764 | { |
| 765 | public: |
| 766 | CTcToken() { } |
| 767 | CTcToken(tc_toktyp_t typ) : typ_(typ) { } |
| 768 | |
| 769 | /* get/set the token type */ |
| 770 | tc_toktyp_t gettyp() const { return typ_; } |
| 771 | void settyp(tc_toktyp_t typ) { typ_ = typ; } |
| 772 | |
| 773 | /* get/set the fully-expanded flag */ |
| 774 | int get_fully_expanded() const { return fully_expanded_; } |
| 775 | void set_fully_expanded(int flag) { fully_expanded_ = flag; } |
| 776 | |
| 777 | /* get/set the text pointer */ |
| 778 | const char *get_text() const { return text_; } |
| 779 | size_t get_text_len() const { return text_len_; } |
| 780 | void set_text(const char *txt, size_t len) |
| 781 | { |
| 782 | text_ = txt; |
| 783 | text_len_ = len; |
| 784 | } |
| 785 | |
| 786 | /* get/set the integer value */ |
| 787 | ulong get_int_val() const { return int_val_; } |
| 788 | void set_int_val(ulong val) |
| 789 | { |
| 790 | typ_ = TOKT_INT; |
| 791 | int_val_ = val; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * compare the text to the given string - returns true if the text |
| 796 | * matches, false if not |
| 797 | */ |
| 798 | int text_matches(const char *txt, size_t len) const |
| 799 | { |
| 800 | return (len == text_len_ && memcmp(txt, text_, len) == 0); |
| 801 | } |
| 802 | int text_matches(const char *txt) const |
| 803 | { |
| 804 | return text_matches(txt, txt != 0 ? strlen(txt) : 0); |
| 805 | } |
| 806 | |
| 807 | /* copy from a another token */ |
| 808 | void set(const CTcToken &tok) |
| 809 | { |
| 810 | typ_ = tok.typ_; |
| 811 | text_ = tok.text_; |
| 812 | text_len_ = tok.text_len_; |
| 813 | int_val_ = tok.int_val_; |
| 814 | fully_expanded_ = tok.fully_expanded_; |
| 815 | } |
| 816 | |
| 817 | private: |
| 818 | /* token type */ |
| 819 | tc_toktyp_t typ_; |
| 820 | |
| 821 | /* |
| 822 | * Pointer to the token's text. This is a pointer into the |
| 823 | * tokenizer's symbol table or into the token list itself, so this |
| 824 | * pointer is valid as long as the tokenizer and its token list are |
| 825 | * valid. |
| 826 | */ |
| 827 | const char *text_; |
| 828 | size_t text_len_; |
| 829 | |
| 830 | /* integer value - valid when the token type is TOKT_INT */ |
| 831 | ulong int_val_; |
| 832 | |
| 833 | /* |
| 834 | * flag: the token has been fully expanded, and should not be |
| 835 | * expanded further on any subsequent rescan for macros |
| 836 | */ |
| 837 | uint fully_expanded_ : 1; |
| 838 | }; |
| 839 | |
| 840 | /* |
| 841 | * Token list entry. This is a generic linked list element containing a |
| 842 | * token. |
| 843 | */ |
| 844 | class CTcTokenEle: public CTcToken |
| 845 | { |
| 846 | public: |
| 847 | CTcTokenEle() { nxt_ = prv_ = 0; } |
| 848 | |
| 849 | /* get/set the next element */ |
| 850 | CTcTokenEle *getnxt() const { return nxt_; } |
| 851 | void setnxt(CTcTokenEle *nxt) { nxt_ = nxt; } |
| 852 | |
| 853 | /* get/set the previous element */ |
| 854 | CTcTokenEle *getprv() const { return prv_; } |
| 855 | void setprv(CTcTokenEle *prv) { prv_ = prv; } |
| 856 | |
| 857 | protected: |
| 858 | /* next/previous token in list */ |
| 859 | CTcTokenEle *nxt_; |
| 860 | CTcTokenEle *prv_; |
| 861 | }; |
| 862 | |
| 863 | /* ------------------------------------------------------------------------ */ |
| 864 | /* |
| 865 | * Macro Expansion Resource object. This object is a collection of |
| 866 | * resources that are needed for a macro expansion. To avoid frequent |
| 867 | * allocating and freeing of these resources, we keep a pool of these |
| 868 | * objects around so that we can re-use them as needed. We'll |
| 869 | * dynamically expand the pool as necessary, so this doesn't impose any |
| 870 | * pre-set limits; it simply avoids lots of memory allocation activity. |
| 871 | */ |
| 872 | class CTcMacroRsc |
| 873 | { |
| 874 | public: |
| 875 | CTcMacroRsc() |
| 876 | { |
| 877 | /* we're not in any lists yet */ |
| 878 | next_avail_ = 0; |
| 879 | next_ = 0; |
| 880 | } |
| 881 | |
| 882 | /* buffer for expansion of the whole line */ |
| 883 | CTcTokString line_exp_; |
| 884 | |
| 885 | /* buffer for expansion of current macro on line */ |
| 886 | CTcTokString macro_exp_; |
| 887 | |
| 888 | /* buffer for expansion of an actual parameter value */ |
| 889 | CTcTokString actual_exp_buf_; |
| 890 | |
| 891 | /* next resource object in the "available" list */ |
| 892 | CTcMacroRsc *next_avail_; |
| 893 | |
| 894 | /* next resource object in the master list */ |
| 895 | CTcMacroRsc *next_; |
| 896 | }; |
| 897 | |
| 898 | |
| 899 | /* ------------------------------------------------------------------------ */ |
| 900 | /* |
| 901 | * Abstract token source interface. This is used to allow external code |
| 902 | * to inject their own substreams into the main token stream. |
| 903 | */ |
| 904 | class CTcTokenSource |
| 905 | { |
| 906 | public: |
| 907 | virtual ~CTcTokenSource() { } |
| 908 | |
| 909 | /* |
| 910 | * Get the next token from the source. Returns null if there are no |
| 911 | * more tokens. |
| 912 | */ |
| 913 | virtual const CTcToken *get_next_token() = 0; |
| 914 | |
| 915 | /* set the enclosing external token source and current token */ |
| 916 | void set_enclosing_source(CTcTokenSource *src, const CTcToken *tok) |
| 917 | { |
| 918 | /* remember the enclosing source */ |
| 919 | enclosing_src_ = src; |
| 920 | |
| 921 | /* remember the current token */ |
| 922 | enclosing_curtok_ = *tok; |
| 923 | } |
| 924 | |
| 925 | /* get the enclosing external token source */ |
| 926 | CTcTokenSource *get_enclosing_source() const |
| 927 | { return enclosing_src_; } |
| 928 | |
| 929 | /* get the token that was current when this source was inserted */ |
| 930 | const CTcToken *get_enclosing_curtok() const |
| 931 | { return &enclosing_curtok_; } |
| 932 | |
| 933 | protected: |
| 934 | /* the enclosing external token source */ |
| 935 | CTcTokenSource *enclosing_src_; |
| 936 | |
| 937 | /* |
| 938 | * the current token in effect enclosing this source - this is the |
| 939 | * token that comes immediately after the source's tokens, because a |
| 940 | * source is inserted before the current token |
| 941 | */ |
| 942 | CTcToken enclosing_curtok_; |
| 943 | }; |
| 944 | |
| 945 | |
| 946 | /* ------------------------------------------------------------------------ */ |
| 947 | /* |
| 948 | * Newline spacing modes. The newline spacing mode controls how line |
| 949 | * breaks are handled within strings. |
| 950 | */ |
| 951 | |
| 952 | enum newline_spacing_mode_t |
| 953 | { |
| 954 | /* delete: a newline and immediately following whitespace are deleted */ |
| 955 | NEWLINE_SPACING_DELETE = 0, |
| 956 | |
| 957 | /* |
| 958 | * collapse: a newline and immediately following whitespace are |
| 959 | * replaced with a single space character |
| 960 | */ |
| 961 | NEWLINE_SPACING_COLLAPSE = 1, |
| 962 | |
| 963 | /* |
| 964 | * preserve: newlines and subsequent whitespace are preserved exactly |
| 965 | * as written in the source code |
| 966 | */ |
| 967 | NEWLINE_SPACING_PRESERVE = 2 |
| 968 | }; |
| 969 | |
| 970 | |
| 971 | /* ------------------------------------------------------------------------ */ |
| 972 | /* |
| 973 | * Tokenizer. This object reads a file and constructs a representation |
| 974 | * of the file as a token list in memory. The tokenizer interprets |
| 975 | * preprocessor directives and expands macros. |
| 976 | */ |
| 977 | class CTcTokenizer |
| 978 | { |
| 979 | friend class CTcHashEntryPpDefine; |
| 980 | |
| 981 | public: |
| 982 | /* |
| 983 | * Create the tokenizer and start reading from the given file. The |
| 984 | * default character set is generally specified by the user (on the |
| 985 | * compiler command line, for example), or obtained from the |
| 986 | * operating system. |
| 987 | */ |
| 988 | CTcTokenizer(class CResLoader *res_loader, const char *default_charset); |
| 989 | |
| 990 | /* destroy the tokenizer */ |
| 991 | ~CTcTokenizer(); |
| 992 | |
| 993 | /* |
| 994 | * Reset the tokenizer. Deletes the current source object and all |
| 995 | * saved token text. This can be used after compilation of a unit |
| 996 | * is completed and the intermediate parser state can be completely |
| 997 | * discarded. |
| 998 | */ |
| 999 | void reset(); |
| 1000 | |
| 1001 | /* |
| 1002 | * Set the source file. 'src_filename' is the fully-resolved local |
| 1003 | * filename of the source file; 'orig_name' is the original name as |
| 1004 | * given on the command line, in the makefile, or wherever it came |
| 1005 | * from. We keep track of the original name so that we can pass |
| 1006 | * information to the debugger indicating the name as it was originally |
| 1007 | * given; this is more useful than the resolved filename, because we |
| 1008 | * might want to run the debugger on another machine with a different |
| 1009 | * local directory structure. |
| 1010 | */ |
| 1011 | int set_source(const char *src_filename, const char *orig_name); |
| 1012 | |
| 1013 | /* set the source to a memory buffer */ |
| 1014 | void set_source_buf(const char *buf) |
| 1015 | { set_source_buf(buf, buf != 0 ? strlen(buf) : 0); } |
| 1016 | |
| 1017 | void set_source_buf(const char *buf, size_t len); |
| 1018 | |
| 1019 | /* |
| 1020 | * Add a #include directory to the include path. We search the |
| 1021 | * include path in the order in which they were defined. |
| 1022 | */ |
| 1023 | void add_inc_path(const char *path); |
| 1024 | |
| 1025 | /* |
| 1026 | * Set preprocess-only mode. In this mode, we'll retain |
| 1027 | * preprocessor directives that will be needed if the preprocessed |
| 1028 | * result is itself compiled; for example, we'll retain #line, |
| 1029 | * #pragma C, #error, and #pragma message directives. |
| 1030 | */ |
| 1031 | void set_mode_pp_only(int flag) { pp_only_mode_ = flag; } |
| 1032 | |
| 1033 | /* |
| 1034 | * Set list-includes mode. In this mode, we'll simply scan source |
| 1035 | * files and write to the standard output a list of the names of all |
| 1036 | * of the #include files. |
| 1037 | */ |
| 1038 | void set_list_includes_mode(int flag) { list_includes_mode_ = flag; } |
| 1039 | |
| 1040 | /* |
| 1041 | * Get/set the test-report mode. In this mode, we'll expand __FILE__ |
| 1042 | * macros with the root name only. |
| 1043 | */ |
| 1044 | int get_test_report_mode() const { return test_report_mode_; } |
| 1045 | void set_test_report_mode(int flag) { test_report_mode_ = flag; } |
| 1046 | |
| 1047 | /* enable or disable preprocessing directives */ |
| 1048 | void enable_pp(int enable) { allow_pp_ = enable; } |
| 1049 | |
| 1050 | /* get the type of the current token */ |
| 1051 | tc_toktyp_t cur() const { return curtok_.gettyp(); } |
| 1052 | |
| 1053 | /* get the next token, reading a new line of source if necessary */ |
| 1054 | tc_toktyp_t next(); |
| 1055 | |
| 1056 | /* |
| 1057 | * Un-get the current token and back up to the previous token. The |
| 1058 | * previous token becomes the current token, and the current token is |
| 1059 | * saved on an internal stack to be fetched again after the returned |
| 1060 | * token. |
| 1061 | * |
| 1062 | * With no arguments, this backs up to the internally stored previous |
| 1063 | * token. This allows callers to look ahead by one token without |
| 1064 | * making a list of saved tokens. |
| 1065 | * |
| 1066 | * With an argument, this backs up to a prior token saved by the |
| 1067 | * caller. This can be used to back up arbitrarily many tokens - it's |
| 1068 | * up to the caller to save ungettable tokens and push them back into |
| 1069 | * the stream. Tokens must be ungotten in reverse order (e.g., get A, |
| 1070 | * B, C, D, unget D, C, B, A). Note that the argument is the PREVIOUS |
| 1071 | * token to restore, not the current token. |
| 1072 | * |
| 1073 | * Tokens un-got with this routine are accessible only to next(), not |
| 1074 | * to any of the lower-level token readers. |
| 1075 | */ |
| 1076 | void unget(); |
| 1077 | void unget(const CTcToken *prv); |
| 1078 | |
| 1079 | /* |
| 1080 | * Push a token into the token stream. The given token becomes the |
| 1081 | * next token to be retrieved. This doesn't affect the current token. |
| 1082 | */ |
| 1083 | void push(const CTcToken *tok); |
| 1084 | |
| 1085 | /* get the current token */ |
| 1086 | const class CTcToken *getcur() const { return &curtok_; } |
| 1087 | |
| 1088 | /* |
| 1089 | * Copy the current token. This makes a copy of the token's text in |
| 1090 | * tokenizer source memory, to ensure that the reference to the text |
| 1091 | * buffer the caller is keeping will remain valid forever. |
| 1092 | */ |
| 1093 | const class CTcToken *copycur(); |
| 1094 | |
| 1095 | /* make a safely storable copy of a given token */ |
| 1096 | void copytok(class CTcToken *dst, const class CTcToken *src); |
| 1097 | |
| 1098 | /* check to see if the current token matches the given text */ |
| 1099 | int cur_tok_matches(const char *txt, size_t len); |
| 1100 | int cur_tok_matches(const char *txt) |
| 1101 | { return cur_tok_matches(txt, strlen(txt)); } |
| 1102 | |
| 1103 | /* |
| 1104 | * Try looking ahead to match a pair of symbols. If the current token |
| 1105 | * is a TOKT_SYM token matching sym1, read the next token to see if |
| 1106 | * it's another TOKT_SYM matching sym2. If so, skip the second token |
| 1107 | * and return true. If not, unget tthe second (so the original token |
| 1108 | * is current again) and return false. |
| 1109 | */ |
| 1110 | int look_ahead(const char *sym1, const char *sym2); |
| 1111 | |
| 1112 | /* |
| 1113 | * peek ahead - same as look_ahead, but doesn't skip anything even on |
| 1114 | * successfully matching the token pair |
| 1115 | */ |
| 1116 | int peek_ahead(const char *sym1, const char *sym2); |
| 1117 | |
| 1118 | /* |
| 1119 | * Set an external token source. We'll read tokens from this source |
| 1120 | * until it is exhausted, at which point we'll revert to the enclosing |
| 1121 | * source. |
| 1122 | * |
| 1123 | * The new source is inserted before the current token, so the current |
| 1124 | * token will become current once again when this source is exhausted. |
| 1125 | * We'll automatically advance to the next token, which (unless we |
| 1126 | * have an ungotten token stashed) will go to the first token in the |
| 1127 | * new source. |
| 1128 | */ |
| 1129 | void set_external_source(CTcTokenSource *src) |
| 1130 | { |
| 1131 | /* |
| 1132 | * store the old source in the new source, so we can restore the |
| 1133 | * old source when we have exhausted the new source |
| 1134 | */ |
| 1135 | src->set_enclosing_source(ext_src_, &curtok_); |
| 1136 | |
| 1137 | /* set the new external source */ |
| 1138 | ext_src_ = src; |
| 1139 | |
| 1140 | /* skip to the next token */ |
| 1141 | next(); |
| 1142 | } |
| 1143 | |
| 1144 | /* clear all external sources, returning to the real token stream */ |
| 1145 | void clear_external_sources(); |
| 1146 | |
| 1147 | /* |
| 1148 | * assume that we should have found '>>' sequence after an embedded |
| 1149 | * expression in a string - used by parsers to resynchronize after |
| 1150 | * an apparent syntax error |
| 1151 | */ |
| 1152 | void assume_missing_str_cont(); |
| 1153 | |
| 1154 | /* define a macro */ |
| 1155 | void add_define(const char *sym, size_t len, const char *expansion, |
| 1156 | size_t expan_len); |
| 1157 | |
| 1158 | void add_define(const char *sym, const char *expansion, size_t expan_len) |
| 1159 | { add_define(sym, strlen(sym), expansion, expan_len); } |
| 1160 | |
| 1161 | void add_define(const char *sym, const char *expansion) |
| 1162 | { add_define(sym, strlen(sym), expansion, strlen(expansion)); } |
| 1163 | |
| 1164 | /* add a macro, given the symbol entry */ |
| 1165 | void add_define(class CTcHashEntryPp *entry); |
| 1166 | |
| 1167 | /* undefine a previously defined macro */ |
| 1168 | void undefine(const char *sym, size_t len); |
| 1169 | void undefine(const char *sym) { undefine(sym, strlen(sym)); } |
| 1170 | |
| 1171 | /* find a #define symbol */ |
| 1172 | class CTcHashEntryPp *find_define(const char *sym, size_t len) const; |
| 1173 | |
| 1174 | /* find an #undef symbol */ |
| 1175 | class CTcHashEntryPp *find_undef(const char *sym, size_t len) const; |
| 1176 | |
| 1177 | /* enumerate all of the #define symbols through a callback */ |
| 1178 | void enum_defines(void (*func)(void *ctx, class CTcHashEntryPp *entry), |
| 1179 | void *ctx); |
| 1180 | |
| 1181 | /* read the next line and handle preprocessor directives */ |
| 1182 | int read_line_pp(); |
| 1183 | |
| 1184 | /* get the file descriptor and line number of the last line read */ |
| 1185 | class CTcTokFileDesc *get_last_desc() const { return last_desc_; } |
| 1186 | long get_last_linenum() const { return last_linenum_; } |
| 1187 | void get_last_pos(class CTcTokFileDesc **desc, long *linenum) const |
| 1188 | { |
| 1189 | *desc = last_desc_; |
| 1190 | *linenum = last_linenum_; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * set the current file descriptor and line number -- this can be |
| 1195 | * used to force the line position to a previously-saved value |
| 1196 | * (during code generation, for example) for error-reporting and |
| 1197 | * debug-record purposes |
| 1198 | */ |
| 1199 | void set_line_info(class CTcTokFileDesc *desc, long linenum) |
| 1200 | { |
| 1201 | last_desc_ = desc; |
| 1202 | last_linenum_ = linenum; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * Parse a preprocessor constant expression. We always parse out of |
| 1207 | * the macro expansion buffer (expbuf_), but the caller must set p_ |
| 1208 | * to point to the starting point on the expansion line prior to |
| 1209 | * calling this routine. |
| 1210 | * |
| 1211 | * If 'read_first' is true, we'll read a token into curtok_ before |
| 1212 | * parsing; otherwise, we'll assume the caller has already primed |
| 1213 | * the pump by reading the first token. |
| 1214 | * |
| 1215 | * If 'last_on_line' is true, we'll flag an error if anything is |
| 1216 | * left on the line after we finish parsing the expression. |
| 1217 | * |
| 1218 | * If 'add_line_ending' is true, we'll add an end-of-line marker to |
| 1219 | * the expansion buffer, so that the tokenizer won't attempt to read |
| 1220 | * past the end of the line. Since a preprocessor expression must |
| 1221 | * be contained entirely on a single logical line, we must never try |
| 1222 | * to read past the end of the current line when parsing a |
| 1223 | * preprocessor expression. |
| 1224 | */ |
| 1225 | int pp_parse_expr(class CTcConstVal *result, |
| 1226 | int read_first, int last_on_line, int add_line_ending); |
| 1227 | |
| 1228 | /* log an error, optionally with parameters */ |
| 1229 | static void log_error(int errnum, ...); |
| 1230 | |
| 1231 | /* |
| 1232 | * log an error with the current token text as the parameter, |
| 1233 | * suitable for a "%.*s" format list entry (hence we'll provide two |
| 1234 | * parameters: an integer with the length of the token text, and a |
| 1235 | * pointer to the token text string) |
| 1236 | */ |
| 1237 | void log_error_curtok(int errnum); |
| 1238 | |
| 1239 | /* log a warning, optionally with parameters */ |
| 1240 | static void log_warning(int errnum, ...); |
| 1241 | |
| 1242 | /* log a pedantic warning */ |
| 1243 | static void log_pedantic(int errnum, ...); |
| 1244 | |
| 1245 | /* log a warning with the current token as the parameter */ |
| 1246 | void log_warning_curtok(int errnum); |
| 1247 | |
| 1248 | /* log a warning or error for the current token */ |
| 1249 | void log_error_or_warning_curtok(tc_severity_t sev, int errnum); |
| 1250 | |
| 1251 | /* log a warning or error for a given token */ |
| 1252 | void log_error_or_warning_with_tok(tc_severity_t sev, int errnum, |
| 1253 | const CTcToken *tok); |
| 1254 | |
| 1255 | /* |
| 1256 | * log then throw a fatal error (this is different from an internal |
| 1257 | * error in that it indicates an unrecoverable error in the input; |
| 1258 | * an internal error indicates that something is wrong with the |
| 1259 | * compiler itself) |
| 1260 | */ |
| 1261 | static void throw_fatal_error(int errnum, ...); |
| 1262 | |
| 1263 | /* |
| 1264 | * log then throw an internal error (internal errors are always |
| 1265 | * fatal: these indicate that something has gone wrong in the |
| 1266 | * compiler, and are equivalent to an assert failure) |
| 1267 | */ |
| 1268 | static void throw_internal_error(int errnum, ...); |
| 1269 | |
| 1270 | /* display a string/number value */ |
| 1271 | void msg_str(const char *str, size_t len) const; |
| 1272 | void msg_long(long val) const; |
| 1273 | |
| 1274 | /* get the current line */ |
| 1275 | const char *get_cur_line() const { return linebuf_.get_text(); } |
| 1276 | size_t get_cur_line_len() const { return linebuf_.get_text_len(); } |
| 1277 | |
| 1278 | /* get/set the #define hash table */ |
| 1279 | class CTcMacroTable *get_defines_table() const { return defines_; } |
| 1280 | class CTcMacroTable *set_defines_table(class CTcMacroTable *tab) |
| 1281 | { |
| 1282 | CTcMacroTable *old_tab = defines_; |
| 1283 | defines_ = tab; |
| 1284 | return old_tab; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * look up a token as a keyword; returns true and fills in 'kw' with |
| 1289 | * the keyword token ID if the token is in fact a keyword, or |
| 1290 | * returns false if it's not a keyword |
| 1291 | */ |
| 1292 | int look_up_keyword(const CTcToken *tok, tc_toktyp_t *kw); |
| 1293 | |
| 1294 | /* |
| 1295 | * Get the next token on the line, filling in the token object. |
| 1296 | * Advances the pointer to the character immediately following the |
| 1297 | * token. |
| 1298 | * |
| 1299 | * If the token is a string, and the string contains backslash |
| 1300 | * sequences, we'll modify the source string by translating each |
| 1301 | * backslash sequences; for example, a "\n" sequence is changed into an |
| 1302 | * ASCII 10. |
| 1303 | * |
| 1304 | * 'expanding' indicates whether or not we're in the initial macro |
| 1305 | * expansion pass. If this is true, we'll suppress error messages |
| 1306 | * during this pass, as we'll encounter the same tokens again when we |
| 1307 | * parse the expanded form of the line. |
| 1308 | */ |
| 1309 | static tc_toktyp_t next_on_line(utf8_ptr *p, CTcToken *tok, |
| 1310 | tok_embed_ctx *ec, int expanding); |
| 1311 | |
| 1312 | /* |
| 1313 | * Get the text of an operator token. Returns a pointer to a |
| 1314 | * constant, static, null-terminated string, suitable for use in |
| 1315 | * error messages. |
| 1316 | */ |
| 1317 | static const char *get_op_text(tc_toktyp_t op); |
| 1318 | |
| 1319 | /* |
| 1320 | * Store text in the source list. Text stored here is available |
| 1321 | * throughout compilation. This routine automatically reserves the |
| 1322 | * space needed, so do not call 'reserve' or 'commit' separately. |
| 1323 | */ |
| 1324 | const char *store_source(const char *txt, size_t len); |
| 1325 | |
| 1326 | /* reserve space for text in the source list */ |
| 1327 | void reserve_source(size_t len); |
| 1328 | |
| 1329 | /* |
| 1330 | * Store a piece of text into pre-reserved space in the source list. |
| 1331 | * This can be used to build up a string from several pieces. You must |
| 1332 | * call 'reserve' first to allocate the space, and you must explicitly |
| 1333 | * add a null terminator at the end of the string. Do not call |
| 1334 | * 'commit'; this automatically commits the space as each substring is |
| 1335 | * added. |
| 1336 | */ |
| 1337 | const char *store_source_partial(const char *txt, size_t len); |
| 1338 | |
| 1339 | /* |
| 1340 | * Get the index of the next source file descriptor that will be |
| 1341 | * created. The linker can use this information to fix up |
| 1342 | * references to file descriptors in an object file when loading |
| 1343 | * multiple object files. |
| 1344 | */ |
| 1345 | int get_next_filedesc_index() const { return next_filedesc_id_; } |
| 1346 | |
| 1347 | /* get the number of source file descriptors in the master list */ |
| 1348 | int get_filedesc_count() const { return next_filedesc_id_; } |
| 1349 | |
| 1350 | /* get the file descriptor at the given (0-based) index */ |
| 1351 | class CTcTokFileDesc *get_filedesc(size_t idx) const |
| 1352 | { |
| 1353 | /* return the array entry at the index, if the index is valid */ |
| 1354 | return (idx < desc_list_cnt_ ? desc_list_[idx] : 0); |
| 1355 | } |
| 1356 | |
| 1357 | /* get the head of the master source file descriptor list */ |
| 1358 | class CTcTokFileDesc *get_first_filedesc() const { return desc_head_; } |
| 1359 | |
| 1360 | /* |
| 1361 | * Create a new file descriptor and add it to the master list. This |
| 1362 | * creates the new descriptor unconditionally, even if a descriptor |
| 1363 | * for the same source file already exists. |
| 1364 | */ |
| 1365 | class CTcTokFileDesc *create_file_desc(const char *fname, size_t len) |
| 1366 | { return get_file_desc(fname, len, TRUE, fname, len); } |
| 1367 | |
| 1368 | /* |
| 1369 | * Set the string capture file. Once this is set, we'll write the |
| 1370 | * contents of each string token that we encounter to this file, |
| 1371 | * with a newline after each token. |
| 1372 | */ |
| 1373 | void set_string_capture(osfildef *fp); |
| 1374 | |
| 1375 | /* write macros to a file, for debugger use */ |
| 1376 | void write_macros_to_file_for_debug(class CVmFile *fp); |
| 1377 | |
| 1378 | /* |
| 1379 | * Load macros from a file. If any errors occur, we'll flag them |
| 1380 | * through the error handler object and return a non-zero value. |
| 1381 | * Returns zero on success. |
| 1382 | */ |
| 1383 | int load_macros_from_file(class CVmStream *fp, |
| 1384 | class CTcTokLoadMacErr *err_handler); |
| 1385 | |
| 1386 | /* receive notification that the compiler is done with all parsing */ |
| 1387 | void parsing_done() |
| 1388 | { |
| 1389 | /* forget any input file position */ |
| 1390 | set_line_info(0, 0); |
| 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * Stuff text into the tokenizer source stream. The new text is |
| 1395 | * inserted at the current read pointer, so that the next token we |
| 1396 | * fetch will come from the start of the inserted text. If 'expand' is |
| 1397 | * true, we'll expand macros in the text; if not, we'll insert the text |
| 1398 | * exactly as is with no macro expansion. |
| 1399 | */ |
| 1400 | void stuff_text(const char *txt, size_t len, int expand); |
| 1401 | |
| 1402 | private: |
| 1403 | /* skip whitespace and token markers */ |
| 1404 | static void skip_ws_and_markers(utf8_ptr *p); |
| 1405 | |
| 1406 | /* |
| 1407 | * get the next token on the line; if we go past the end of the |
| 1408 | * string buffer, we'll return EOF |
| 1409 | */ |
| 1410 | static tc_toktyp_t next_on_line(const CTcTokString *srcbuf, utf8_ptr *p, |
| 1411 | CTcToken *tok, tok_embed_ctx *ec, |
| 1412 | int expanding); |
| 1413 | |
| 1414 | /* |
| 1415 | * get the next token on the current line, updating the internal |
| 1416 | * character position pointer to point just past the token, and filling |
| 1417 | * in the internal current token object with the toen data |
| 1418 | */ |
| 1419 | tc_toktyp_t next_on_line() |
| 1420 | { return next_on_line(&p_, &curtok_, 0, FALSE); } |
| 1421 | |
| 1422 | /* get the next token on the line, with string translation */ |
| 1423 | tc_toktyp_t next_on_line_xlat(tok_embed_ctx *ec) |
| 1424 | { return next_on_line_xlat(&p_, &curtok_, ec); } |
| 1425 | |
| 1426 | /* |
| 1427 | * get the next token, translating strings and storing string and |
| 1428 | * symbol text in the source block list |
| 1429 | */ |
| 1430 | tc_toktyp_t next_on_line_xlat_keep(); |
| 1431 | |
| 1432 | /* |
| 1433 | * get the next token on the line, translating strings to internal |
| 1434 | * format |
| 1435 | */ |
| 1436 | tc_toktyp_t next_on_line_xlat(utf8_ptr *p, CTcToken *tok, |
| 1437 | tok_embed_ctx *ec); |
| 1438 | |
| 1439 | /* |
| 1440 | * translate a string to internal format by converting escape |
| 1441 | * sequences; overwrites the original buffer |
| 1442 | */ |
| 1443 | tc_toktyp_t xlat_string(utf8_ptr *p, CTcToken *tok, tok_embed_ctx *ec); |
| 1444 | |
| 1445 | /* |
| 1446 | * translate a string into a given buffer; if 'force_embed_end' is |
| 1447 | * true, we'll act as though we're continuing the string after the |
| 1448 | * '>>' after an embedded expression, no matter what the actual |
| 1449 | * input looks like |
| 1450 | */ |
| 1451 | tc_toktyp_t xlat_string_to(char *dst, utf8_ptr *p, CTcToken *tok, |
| 1452 | tok_embed_ctx *ec, int force_embed_end); |
| 1453 | |
| 1454 | /* |
| 1455 | * Translate a string, saving the translated version in the source |
| 1456 | * block list. If 'force_end_embed' is true, we'll act as though we |
| 1457 | * were looking at '>>' (or, more precisely, we'll act as though |
| 1458 | * '>>' immediately preceded the current input), regardless of what |
| 1459 | * the actual input looks like. |
| 1460 | */ |
| 1461 | tc_toktyp_t xlat_string_to_src(tok_embed_ctx *ec, int force_end_embed); |
| 1462 | |
| 1463 | /* |
| 1464 | * is the given token type "safe" (i.e., stored in the tokenizer source |
| 1465 | * list, so that its text pointer is permanently valid; non-safe tokens |
| 1466 | * have valid text pointers only until the next token fetch) |
| 1467 | */ |
| 1468 | int is_tok_safe(tc_toktyp_t typ); |
| 1469 | |
| 1470 | /* initialize the source block list */ |
| 1471 | void init_src_block_list(); |
| 1472 | |
| 1473 | /* delete current source file, including all including parents */ |
| 1474 | void delete_source(); |
| 1475 | |
| 1476 | /* |
| 1477 | * Read the next line; processes comments, but does not expand macros |
| 1478 | * or parse preprocessor directives. This always reads into linebuf_; |
| 1479 | * the return value is the offset within linebuf_ of the new text. A |
| 1480 | * return value of -1 indicates that we're at end of file. |
| 1481 | */ |
| 1482 | int read_line(int append); |
| 1483 | |
| 1484 | /* |
| 1485 | * Set the source read pointer to the start of a new line, given the |
| 1486 | * CTcTokString object containing the buffer, and the offset within |
| 1487 | * that buffer. |
| 1488 | */ |
| 1489 | void start_new_line(CTcTokString *str, int ofs) |
| 1490 | { |
| 1491 | /* remember the buffer we're reading out of */ |
| 1492 | curbuf_ = str; |
| 1493 | |
| 1494 | /* set the read pointer to the start of the new line's text */ |
| 1495 | p_.set((char *)str->get_text() + ofs); |
| 1496 | } |
| 1497 | |
| 1498 | /* unsplice text from the current line and make it the next line */ |
| 1499 | void unsplice_line(const char *new_line_start); |
| 1500 | |
| 1501 | /* |
| 1502 | * Commit space in the source list - this is used when text is directly |
| 1503 | * stored after reserving space. The size reserved may be greater than |
| 1504 | * the size committed, because it is sometimes more efficient to make a |
| 1505 | * guess that may overestimate the amount we actually end up needing. |
| 1506 | */ |
| 1507 | void commit_source(size_t len); |
| 1508 | |
| 1509 | /* parse a string */ |
| 1510 | static tc_toktyp_t tokenize_string( |
| 1511 | utf8_ptr *p, CTcToken *tok, tok_embed_ctx *ec, int expanding_macros); |
| 1512 | |
| 1513 | /* process comments */ |
| 1514 | void process_comments(size_t start_ofs); |
| 1515 | |
| 1516 | /* splice lines for a string that runs across multiple lines */ |
| 1517 | void splice_string(); |
| 1518 | |
| 1519 | /* scan a sprintf format spec */ |
| 1520 | void scan_sprintf_spec(utf8_ptr *p); |
| 1521 | |
| 1522 | /* translate a \ escape sequence */ |
| 1523 | void xlat_escape(utf8_ptr *dst, utf8_ptr *p, wchar_t qu, int triple); |
| 1524 | |
| 1525 | /* skip an ordinary character or \ escape sequence */ |
| 1526 | void skip_escape(utf8_ptr *p); |
| 1527 | |
| 1528 | /* translate escape sequences in a string segment */ |
| 1529 | void xlat_escapes(utf8_ptr *dstp, const utf8_ptr *srcp, |
| 1530 | const utf8_ptr *endp); |
| 1531 | |
| 1532 | /* expand macros in the current line */ |
| 1533 | int expand_macros_curline(int read_more, int allow_defined, |
| 1534 | int append_to_expbuf); |
| 1535 | |
| 1536 | /* |
| 1537 | * Expand the macros in the given text, filling in the given |
| 1538 | * CTcTokString with the results. The expansion will clear out any |
| 1539 | * existing text in the result buffer. Returns zero on success, or |
| 1540 | * non-zero on error. |
| 1541 | */ |
| 1542 | int expand_macros(class CTcTokString *dest, const char *str, size_t len) |
| 1543 | { |
| 1544 | CTcTokStringRef srcbuf; |
| 1545 | |
| 1546 | /* set up a CTcTokString for the source */ |
| 1547 | srcbuf.set_buffer(str, len); |
| 1548 | |
| 1549 | /* go expand macros */ |
| 1550 | return expand_macros(&srcbuf, 0, dest, FALSE, FALSE, FALSE); |
| 1551 | } |
| 1552 | |
| 1553 | /* expand all of the macros in the given text */ |
| 1554 | int expand_macros(class CTcTokString *srcbuf, utf8_ptr *src, |
| 1555 | class CTcTokString *expbuf, int read_more, |
| 1556 | int allow_defined, int append); |
| 1557 | |
| 1558 | /* expand the macro at the current token on the current line */ |
| 1559 | int expand_macro(class CTcMacroRsc *res, class CTcTokString *expbuf, |
| 1560 | const class CTcTokString *srcbuf, utf8_ptr *src, |
| 1561 | size_t macro_srcbuf_ofs, CTcHashEntryPp *entry, |
| 1562 | int read_more, int allow_defined, int *expanded); |
| 1563 | |
| 1564 | /* |
| 1565 | * Remove our special expansion flags from an expanded macro buffer. |
| 1566 | * This can be called after all expansion has been completed to clean |
| 1567 | * up the buffer for human consumption. |
| 1568 | */ |
| 1569 | void remove_expansion_flags(CTcTokString *buf); |
| 1570 | |
| 1571 | /* scan for a prior expansion of a macro within the current context */ |
| 1572 | static int scan_for_prior_expansion(utf8_ptr src, const char *src_end, |
| 1573 | const class CTcHashEntryPp *entry); |
| 1574 | |
| 1575 | /* remove end-of-macro-expansion flags from a buffer */ |
| 1576 | static void remove_end_markers(class CTcTokString *buf); |
| 1577 | |
| 1578 | /* change a buffer to use individual token full-expansion markers */ |
| 1579 | void mark_full_exp_tokens(CTcTokString *dstbuf, |
| 1580 | const class CTcTokString *srcbuf, |
| 1581 | int append) const; |
| 1582 | |
| 1583 | /* allocate a macro expansion resource */ |
| 1584 | class CTcMacroRsc *alloc_macro_rsc(); |
| 1585 | |
| 1586 | /* release a macro expansion resource */ |
| 1587 | void release_macro_rsc(class CTcMacroRsc *rsc); |
| 1588 | |
| 1589 | /* |
| 1590 | * Parse the actual parameters to a macro. Fills in argofs[] and |
| 1591 | * arglen[] with the offsets (from srcbuf->get_buf()) and lengths, |
| 1592 | * respectively, of each actual parameter's text. |
| 1593 | */ |
| 1594 | int parse_macro_actuals(const class CTcTokString *srcbuf, utf8_ptr *src, |
| 1595 | const CTcHashEntryPp *macro_entry, |
| 1596 | size_t argofs[TOK_MAX_MACRO_ARGS], |
| 1597 | size_t arglen[TOK_MAX_MACRO_ARGS], |
| 1598 | int read_more, int *found_actuals); |
| 1599 | |
| 1600 | /* splice the next line for reading more macro actuals */ |
| 1601 | tc_toktyp_t actual_splice_next_line(const CTcTokString *srcbuf, |
| 1602 | utf8_ptr *src, CTcToken *tok); |
| 1603 | |
| 1604 | /* substitute the actual parameters in a macro's expansion */ |
| 1605 | int substitute_macro_actuals(class CTcMacroRsc *rsc, |
| 1606 | class CTcTokString *subexp, |
| 1607 | CTcHashEntryPp *macro_entry, |
| 1608 | const class CTcTokString *srcbuf, |
| 1609 | const size_t *argofs, const size_t *arglen, |
| 1610 | int allow_defined); |
| 1611 | |
| 1612 | /* stringize a macro actual parameter into an expansion buffer */ |
| 1613 | void stringize_macro_actual(class CTcTokString *expbuf, |
| 1614 | const char *actual_val, size_t actual_len, |
| 1615 | char quote_char, int add_open_quote, |
| 1616 | int add_close_quote); |
| 1617 | |
| 1618 | /* skip a delimited macro expansion area (#foreach, #ifempty, etc) */ |
| 1619 | void skip_delimited_group(utf8_ptr *p, int parts_to_skip); |
| 1620 | |
| 1621 | /* expand a defined() preprocessor operator */ |
| 1622 | int expand_defined(class CTcTokString *subexp, |
| 1623 | const class CTcTokString *srcbuf, utf8_ptr *src); |
| 1624 | |
| 1625 | /* add a file to the list of files to be included only once */ |
| 1626 | void add_include_once(const char *fname); |
| 1627 | |
| 1628 | /* find a file in the list of files to be included only once */ |
| 1629 | int find_include_once(const char *fname); |
| 1630 | |
| 1631 | /* process a #pragma directive */ |
| 1632 | void pp_pragma(); |
| 1633 | |
| 1634 | /* process a #charset directive */ |
| 1635 | void pp_charset(); |
| 1636 | |
| 1637 | /* process a #include directive */ |
| 1638 | void pp_include(); |
| 1639 | |
| 1640 | /* process a #define directive */ |
| 1641 | void pp_define(); |
| 1642 | |
| 1643 | /* process a #if directive */ |
| 1644 | void pp_if(); |
| 1645 | |
| 1646 | /* process a #ifdef directive */ |
| 1647 | void pp_ifdef(); |
| 1648 | |
| 1649 | /* process a #ifdef directive */ |
| 1650 | void pp_ifndef(); |
| 1651 | |
| 1652 | /* process a #ifdef or #ifndef */ |
| 1653 | void pp_ifdef_or_ifndef(int sense); |
| 1654 | |
| 1655 | /* process a #else directive */ |
| 1656 | void pp_else(); |
| 1657 | |
| 1658 | /* process a #elif directive */ |
| 1659 | void pp_elif(); |
| 1660 | |
| 1661 | /* process a #endif directive */ |
| 1662 | void pp_endif(); |
| 1663 | |
| 1664 | /* process a #error directive */ |
| 1665 | void pp_error(); |
| 1666 | |
| 1667 | /* process a #undef directive */ |
| 1668 | void pp_undef(); |
| 1669 | |
| 1670 | /* process a #line directive */ |
| 1671 | void pp_line(); |
| 1672 | |
| 1673 | /* get a lone identifier for a preprocessor directive */ |
| 1674 | int pp_get_lone_ident(char *buf, size_t bufl); |
| 1675 | |
| 1676 | /* process a #pragma C directive */ |
| 1677 | // void pragma_c(); - not currently used |
| 1678 | |
| 1679 | /* process a #pragma once directive */ |
| 1680 | void pragma_once(); |
| 1681 | |
| 1682 | /* process a #pragma all_once directive */ |
| 1683 | void pragma_all_once(); |
| 1684 | |
| 1685 | /* process a #pragma message directive */ |
| 1686 | void pragma_message(); |
| 1687 | |
| 1688 | /* process a #pragma newline_spacing(on/off) directive */ |
| 1689 | void pragma_newline_spacing(); |
| 1690 | |
| 1691 | /* process a #pragma sourceTextGroup directive */ |
| 1692 | void pragma_source_text_group(); |
| 1693 | |
| 1694 | /* |
| 1695 | * Determine if we're in a false #if branch. If we're inside a #if |
| 1696 | * block, and the state is either IF_NO, IF_DONE, or ELSE_NO, or |
| 1697 | * we're inside a #if nested within any negative branch, we're in a |
| 1698 | * not-taken branch of a #if block. |
| 1699 | */ |
| 1700 | int in_false_if() const |
| 1701 | { |
| 1702 | return (if_sp_ != 0 |
| 1703 | && (if_false_level_ != 0 |
| 1704 | || if_stack_[if_sp_ - 1].state == TOKIF_IF_NO |
| 1705 | || if_stack_[if_sp_ - 1].state == TOKIF_IF_DONE |
| 1706 | || if_stack_[if_sp_ - 1].state == TOKIF_ELSE_NO)); |
| 1707 | } |
| 1708 | |
| 1709 | /* push a new #if level with the given state */ |
| 1710 | void push_if(tok_if_t state); |
| 1711 | |
| 1712 | /* get the current #if state */ |
| 1713 | tok_if_t get_if_state() const |
| 1714 | { |
| 1715 | if (if_sp_ == 0) |
| 1716 | return TOKIF_NONE; |
| 1717 | else |
| 1718 | return if_stack_[if_sp_ - 1].state; |
| 1719 | } |
| 1720 | |
| 1721 | /* switch the current #if level to the given state */ |
| 1722 | void change_if_state(tok_if_t state) |
| 1723 | { |
| 1724 | if (if_sp_ != 0) |
| 1725 | if_stack_[if_sp_ - 1].state = state; |
| 1726 | } |
| 1727 | |
| 1728 | /* pop the current #if level */ |
| 1729 | void pop_if(); |
| 1730 | |
| 1731 | /* |
| 1732 | * Find or create a descriptor for the given filename. 'fname' is |
| 1733 | * the full file system path specifying the file. 'orig_fname' is |
| 1734 | * the filename as originally specified by the user, if different; |
| 1735 | * in the case of #include files, this indicates the name that was |
| 1736 | * specified in the directive itself, whereas 'fname' is the actual |
| 1737 | * filename that resulted from searching the include path for the |
| 1738 | * given name. |
| 1739 | */ |
| 1740 | class CTcTokFileDesc *get_file_desc(const char *fname, size_t fname_len, |
| 1741 | int always_create, |
| 1742 | const char *orig_fname, |
| 1743 | size_t orig_fname_len); |
| 1744 | |
| 1745 | /* clear the line buffer */ |
| 1746 | void clear_linebuf(); |
| 1747 | |
| 1748 | /* flag: ALL_ONCE mode - we include each file only once */ |
| 1749 | unsigned int all_once_ : 1; |
| 1750 | |
| 1751 | /* flag: warn on ignoring a redundant #include file */ |
| 1752 | unsigned int warn_on_ignore_incl_ : 1; |
| 1753 | |
| 1754 | /* |
| 1755 | * Flag: in preprocess-only mode. In this mode, we'll leave certain |
| 1756 | * preprocessor directives intact in the source, since they'll be |
| 1757 | * needed in a subsequent compilation of the preprocessed source. |
| 1758 | * For example, we'll leave #line directives, #pragma C, #error, and |
| 1759 | * #pragma message directives in the preprocessed result. |
| 1760 | */ |
| 1761 | unsigned int pp_only_mode_ : 1; |
| 1762 | |
| 1763 | /* |
| 1764 | * Flag: in test reporting mode. In this mode, we'll expand __FILE__ |
| 1765 | * macros with the root name only. |
| 1766 | */ |
| 1767 | unsigned int test_report_mode_ : 1; |
| 1768 | |
| 1769 | /* |
| 1770 | * Flag: in preprocess-for-includes mode. In this mode, we'll do |
| 1771 | * nothing except run the preprocessor and generate a list of the |
| 1772 | * header files that are included, along with header files they |
| 1773 | * include, and so on. |
| 1774 | */ |
| 1775 | unsigned int list_includes_mode_ : 1; |
| 1776 | |
| 1777 | /* |
| 1778 | * flag: we're parsing a preprocessor constant expression (for a |
| 1779 | * #if, for example; this doesn't apply to simple macro expansion) |
| 1780 | */ |
| 1781 | unsigned int in_pp_expr_ : 1; |
| 1782 | |
| 1783 | /* mode for handling newlines in strings */ |
| 1784 | newline_spacing_mode_t string_newline_spacing_; |
| 1785 | |
| 1786 | /* resource loader */ |
| 1787 | class CResLoader *res_loader_; |
| 1788 | |
| 1789 | /* |
| 1790 | * name of our default character set - this is generally specified |
| 1791 | * by the user (on the compiler command line, for example), or |
| 1792 | * obtained from the operating system |
| 1793 | */ |
| 1794 | char *default_charset_; |
| 1795 | |
| 1796 | /* input (to unicode) character mapper for the default character set */ |
| 1797 | class CCharmapToUni *default_mapper_; |
| 1798 | |
| 1799 | /* head of list of previously-included files */ |
| 1800 | struct tctok_incfile_t *prev_includes_; |
| 1801 | |
| 1802 | /* head and tail of include path list */ |
| 1803 | struct tctok_incpath_t *incpath_head_; |
| 1804 | struct tctok_incpath_t *incpath_tail_; |
| 1805 | |
| 1806 | /* file descriptor and line number of last line read */ |
| 1807 | class CTcTokFileDesc *last_desc_; |
| 1808 | long last_linenum_; |
| 1809 | |
| 1810 | /* file descriptor and line number of last line appended */ |
| 1811 | class CTcTokFileDesc *appended_desc_; |
| 1812 | long appended_linenum_; |
| 1813 | |
| 1814 | /* current input stream */ |
| 1815 | class CTcTokStream *str_; |
| 1816 | |
| 1817 | /* master list of file descriptors */ |
| 1818 | class CTcTokFileDesc *desc_head_; |
| 1819 | class CTcTokFileDesc *desc_tail_; |
| 1820 | |
| 1821 | /* |
| 1822 | * array of file descriptors (we keep the list in both an array and |
| 1823 | * a linked list, since we need both sequential and indexed access; |
| 1824 | * this isn't a lot of trouble since we never need to remove an |
| 1825 | * entry from the list) |
| 1826 | */ |
| 1827 | class CTcTokFileDesc **desc_list_; |
| 1828 | |
| 1829 | /* number of entries in desc_list_ */ |
| 1830 | size_t desc_list_cnt_; |
| 1831 | |
| 1832 | /* number of slots allocated in desc_list_ array */ |
| 1833 | size_t desc_list_alo_; |
| 1834 | |
| 1835 | /* next file descriptor ID to be assigned */ |
| 1836 | int next_filedesc_id_; |
| 1837 | |
| 1838 | /* pointer to current position in current line */ |
| 1839 | utf8_ptr p_; |
| 1840 | |
| 1841 | /* |
| 1842 | * The CTcTokString object containing the current line. This is the |
| 1843 | * buffer object we're currently reading from, and will be either |
| 1844 | * linebuf_ or expbuf_. p_ always points into this buffer. |
| 1845 | */ |
| 1846 | CTcTokString *curbuf_; |
| 1847 | |
| 1848 | /* raw file input buffer */ |
| 1849 | CTcTokString linebuf_; |
| 1850 | |
| 1851 | /* |
| 1852 | * unsplice buffer - we'll put any unspliced text into this buffer, |
| 1853 | * then read it back at the next read_line() |
| 1854 | */ |
| 1855 | CTcTokString unsplicebuf_; |
| 1856 | |
| 1857 | /* macro expansion buffer */ |
| 1858 | CTcTokString expbuf_; |
| 1859 | |
| 1860 | /* macro definition parsing buffer */ |
| 1861 | CTcTokString defbuf_; |
| 1862 | |
| 1863 | /* |
| 1864 | * Flag: in a string. If this is '\0', we're not in a string; |
| 1865 | * otherwise, this is the quote character that ends the string. |
| 1866 | */ |
| 1867 | wchar_t in_quote_; |
| 1868 | |
| 1869 | /* flag: the in_quote_ string we're in is triple quoted */ |
| 1870 | int in_triple_; |
| 1871 | |
| 1872 | /* embedded expression context in comment */ |
| 1873 | tok_embed_ctx comment_in_embedding_; |
| 1874 | |
| 1875 | /* embedded expression context within a macro expansion */ |
| 1876 | tok_embed_ctx macro_in_embedding_; |
| 1877 | |
| 1878 | /* embedded expression context for the main token stream */ |
| 1879 | tok_embed_ctx main_in_embedding_; |
| 1880 | |
| 1881 | /* |
| 1882 | * #if state stack. if_sp_ is the index of the next nesting slot; |
| 1883 | * if if_sp_ is zero, it means that we're not in a #if at all. |
| 1884 | * |
| 1885 | * Separately, the if_false_level_ is the level of #if's contained |
| 1886 | * within a false #if branch. This is separate because, once we're |
| 1887 | * in a false #if branch, everything within it is false. |
| 1888 | */ |
| 1889 | int if_sp_; |
| 1890 | tok_if_info_t if_stack_[TOK_MAX_IF_NESTING]; |
| 1891 | int if_false_level_; |
| 1892 | |
| 1893 | /* source block list head */ |
| 1894 | CTcTokSrcBlock *src_head_; |
| 1895 | |
| 1896 | /* current (and last) source block */ |
| 1897 | CTcTokSrcBlock *src_cur_; |
| 1898 | |
| 1899 | /* pointer to next available byte in the current source block */ |
| 1900 | char *src_ptr_; |
| 1901 | |
| 1902 | /* number of bytes remaining in the current source block */ |
| 1903 | size_t src_rem_; |
| 1904 | |
| 1905 | /* current token */ |
| 1906 | CTcToken curtok_; |
| 1907 | |
| 1908 | /* previous token (for unget) */ |
| 1909 | CTcToken prvtok_; |
| 1910 | |
| 1911 | /* head of allocated list of unget token slots */ |
| 1912 | CTcTokenEle *unget_head_; |
| 1913 | |
| 1914 | /* |
| 1915 | * Last unget token. This is a pointer into the unget list, to the |
| 1916 | * element containing the last ungotten token. The next next() will |
| 1917 | * reinstate this token and move the pointer back into the list; the |
| 1918 | * next unget() will move the pointer forward and fill in the next |
| 1919 | * element. This is null if there are no ungotten tokens. |
| 1920 | */ |
| 1921 | CTcTokenEle *unget_cur_; |
| 1922 | |
| 1923 | /* the external token source, if any */ |
| 1924 | CTcTokenSource *ext_src_; |
| 1925 | |
| 1926 | /* symbol table for #define symbols */ |
| 1927 | class CTcMacroTable *defines_; |
| 1928 | |
| 1929 | /* |
| 1930 | * symbol table for symbols explicitly undefined; we keep track of |
| 1931 | * these so that we can exclude anything ever undefined from the debug |
| 1932 | * macro records, since only static global macros can be handled in the |
| 1933 | * debug records |
| 1934 | */ |
| 1935 | class CVmHashTable *undefs_; |
| 1936 | |
| 1937 | /* symbol table for TADS keywords */ |
| 1938 | class CVmHashTable *kw_; |
| 1939 | |
| 1940 | /* head of macro resource pool list */ |
| 1941 | class CTcMacroRsc *macro_res_head_; |
| 1942 | |
| 1943 | /* head of list of available macro resources */ |
| 1944 | class CTcMacroRsc *macro_res_avail_; |
| 1945 | |
| 1946 | /* |
| 1947 | * string capture file - if this is non-null, we'll capture all of |
| 1948 | * the strings we read to this file, one string per line |
| 1949 | */ |
| 1950 | class CVmDataSource *string_fp_; |
| 1951 | |
| 1952 | /* character mapper for writing to the string capture file */ |
| 1953 | class CCharmapToLocal *string_fp_map_; |
| 1954 | |
| 1955 | /* true -> allow preprocessor directives */ |
| 1956 | unsigned int allow_pp_; |
| 1957 | }; |
| 1958 | |
| 1959 | /* ------------------------------------------------------------------------ */ |
| 1960 | /* |
| 1961 | * Error handler interface. Callers of load_macros_from_file() in |
| 1962 | * CTcTokenizer must provide an implementation of this interface to handle |
| 1963 | * errors that occur while loading macros. |
| 1964 | */ |
| 1965 | class CTcTokLoadMacErr |
| 1966 | { |
| 1967 | public: |
| 1968 | /* |
| 1969 | * Flag an error. The error codes are taken from the following list: |
| 1970 | * |
| 1971 | * 1 - a macro name symbol in the file is too long (it exceeds the |
| 1972 | * maximum symbol length for the preprocessor) |
| 1973 | * |
| 1974 | * 2 - a formal parameter name is too long |
| 1975 | */ |
| 1976 | virtual void log_error(int err) = 0; |
| 1977 | }; |
| 1978 | |
| 1979 | /* ------------------------------------------------------------------------ */ |
| 1980 | /* |
| 1981 | * Tokenizer File Descriptor. Each unique source file has a separate |
| 1982 | * file descriptor, which keeps track of the file's name. |
| 1983 | */ |
| 1984 | class CTcTokFileDesc |
| 1985 | { |
| 1986 | public: |
| 1987 | /* create a file descriptor */ |
| 1988 | CTcTokFileDesc(const char *fname, size_t fname_len, int index, |
| 1989 | CTcTokFileDesc *orig_desc, |
| 1990 | const char *orig_fname, size_t orig_fname_len); |
| 1991 | |
| 1992 | /* delete the descriptor */ |
| 1993 | ~CTcTokFileDesc(); |
| 1994 | |
| 1995 | /* get the filename */ |
| 1996 | const char *get_fname() const { return fname_; } |
| 1997 | |
| 1998 | /* get the original filename string */ |
| 1999 | const char *get_orig_fname() const { return orig_fname_; } |
| 2000 | |
| 2001 | /* |
| 2002 | * get the filename as a double-quoted string (backslashes and |
| 2003 | * double-quotes will be escaped with backslashes) |
| 2004 | */ |
| 2005 | const char *get_dquoted_fname() const { return dquoted_fname_; } |
| 2006 | |
| 2007 | /* |
| 2008 | * get the root filename (i.e., with no path prefix) as a |
| 2009 | * double-quoted string |
| 2010 | */ |
| 2011 | const char *get_dquoted_rootname() const { return dquoted_rootname_; } |
| 2012 | |
| 2013 | /* get the filename as a single-quoted string */ |
| 2014 | const char *get_squoted_fname() const { return squoted_fname_; } |
| 2015 | |
| 2016 | /* get the root filename as a single-quoted string */ |
| 2017 | const char *get_squoted_rootname() const { return squoted_rootname_; } |
| 2018 | |
| 2019 | /* get/set the next file descriptor in the descriptor chain */ |
| 2020 | CTcTokFileDesc *get_next() const { return next_; } |
| 2021 | void set_next(CTcTokFileDesc *nxt) { next_ = nxt; } |
| 2022 | |
| 2023 | /* get my index in the master list */ |
| 2024 | int get_index() const { return index_; } |
| 2025 | |
| 2026 | /* get the original descriptor for this file in the list */ |
| 2027 | CTcTokFileDesc *get_orig() const { return orig_; } |
| 2028 | |
| 2029 | /* |
| 2030 | * get the list index of the original entry (returns my own list |
| 2031 | * index if I am the original entry) |
| 2032 | */ |
| 2033 | int get_orig_index() const |
| 2034 | { return orig_ == 0 ? index_ : orig_->get_index(); } |
| 2035 | |
| 2036 | /* |
| 2037 | * Add a source line position to our list. We keep an index of the |
| 2038 | * byte-code address for each executable source line, so that |
| 2039 | * debuggers can find the compiled code corresponding to a source |
| 2040 | * location. The image builder gives us this information during the |
| 2041 | * linking process. The address is the absolute location in the |
| 2042 | * image file of the executable code for the given source line (the |
| 2043 | * first line in the file is numbered 1). |
| 2044 | */ |
| 2045 | void add_source_line(ulong linenum, ulong line_addr); |
| 2046 | |
| 2047 | /* |
| 2048 | * Enumerate the source lines, calling the callback for each one. |
| 2049 | * We will only enumerate source lines which actually have an |
| 2050 | * associated code location - source lines that generated no |
| 2051 | * executable code are skipped. We'll enumerate the lines in |
| 2052 | * ascending order of line number, and each line number will appear |
| 2053 | * only once. |
| 2054 | */ |
| 2055 | void enum_source_lines(void (*cbfunc)(void *ctx, ulong linenum, |
| 2056 | ulong byte_code_addr), |
| 2057 | void *cbctx); |
| 2058 | |
| 2059 | private: |
| 2060 | /* index in the master list */ |
| 2061 | int index_; |
| 2062 | |
| 2063 | /* filename string - this is the actual file system filename */ |
| 2064 | char *fname_; |
| 2065 | |
| 2066 | /* |
| 2067 | * original filename string, if different from fname_ - this is the |
| 2068 | * filename as specified by the user, before it was adjusted with |
| 2069 | * include paths or other extra location information |
| 2070 | */ |
| 2071 | char *orig_fname_; |
| 2072 | |
| 2073 | /* double-quoted version of the filename */ |
| 2074 | char *dquoted_fname_; |
| 2075 | |
| 2076 | /* single-quoted version of the filename */ |
| 2077 | char *squoted_fname_; |
| 2078 | |
| 2079 | /* single-quoted version of the root filename */ |
| 2080 | char *squoted_rootname_; |
| 2081 | |
| 2082 | /* double-quoted version of the root filename */ |
| 2083 | char *dquoted_rootname_; |
| 2084 | |
| 2085 | /* next descriptor in the master descriptor list */ |
| 2086 | CTcTokFileDesc *next_; |
| 2087 | |
| 2088 | /* |
| 2089 | * The original file descriptor with the same filename. If we |
| 2090 | * create multiple descriptors for the same filename (because, for |
| 2091 | * example, the same header is included in several different object |
| 2092 | * files), we'll keep track of the original descriptor for the file |
| 2093 | * in all of the copies. |
| 2094 | */ |
| 2095 | CTcTokFileDesc *orig_; |
| 2096 | |
| 2097 | /* source line pages */ |
| 2098 | struct CTcTokSrcPage **src_pages_; |
| 2099 | |
| 2100 | /* number of source line page slots allocated */ |
| 2101 | size_t src_pages_alo_; |
| 2102 | }; |
| 2103 | |
| 2104 | |
| 2105 | /* ------------------------------------------------------------------------ */ |
| 2106 | /* |
| 2107 | * Tokenizer Input Stream |
| 2108 | */ |
| 2109 | class CTcTokStream |
| 2110 | { |
| 2111 | public: |
| 2112 | /* create a token stream */ |
| 2113 | CTcTokStream(class CTcTokFileDesc *desc, class CTcSrcObject *src, |
| 2114 | CTcTokStream *parent, int charset_error, |
| 2115 | int init_if_level); |
| 2116 | |
| 2117 | /* delete the stream */ |
| 2118 | ~CTcTokStream(); |
| 2119 | |
| 2120 | /* get/set the associated file descriptor */ |
| 2121 | class CTcTokFileDesc *get_desc() const { return desc_; } |
| 2122 | void set_desc(class CTcTokFileDesc *desc) { desc_ = desc; } |
| 2123 | |
| 2124 | /* get the underlying source file */ |
| 2125 | class CTcSrcObject *get_src() const { return src_; } |
| 2126 | |
| 2127 | /* get the line number of the next line to be read */ |
| 2128 | long get_next_linenum() const { return next_linenum_; } |
| 2129 | |
| 2130 | /* set the next line number */ |
| 2131 | void set_next_linenum(long l) { next_linenum_ = l; } |
| 2132 | |
| 2133 | /* get the enclosing stream */ |
| 2134 | CTcTokStream *get_parent() const { return parent_; } |
| 2135 | |
| 2136 | /* count having read a line */ |
| 2137 | void count_line() { ++next_linenum_; } |
| 2138 | |
| 2139 | /* was there a #charset error when opening the file? */ |
| 2140 | int get_charset_error() const { return charset_error_; } |
| 2141 | |
| 2142 | /* get/set the in-comment status */ |
| 2143 | int is_in_comment() const { return in_comment_; } |
| 2144 | void set_in_comment(int f) { in_comment_ = f; } |
| 2145 | |
| 2146 | /* get/set the pragma C mode */ |
| 2147 | // int is_pragma_c() const { return pragma_c_; } |
| 2148 | // void set_pragma_c(int f) { pragma_c_ = f; } |
| 2149 | |
| 2150 | /* get/set if nesting level at the start of the file */ |
| 2151 | int get_init_if_level() const { return init_if_level_; } |
| 2152 | void set_init_if_level(int level) { init_if_level_ = level; } |
| 2153 | |
| 2154 | /* get/set the newline spacing mode */ |
| 2155 | newline_spacing_mode_t get_newline_spacing() const |
| 2156 | { return newline_spacing_; } |
| 2157 | void set_newline_spacing(newline_spacing_mode_t f) |
| 2158 | { newline_spacing_ = f; } |
| 2159 | |
| 2160 | private: |
| 2161 | /* file descriptor associated with this file */ |
| 2162 | class CTcTokFileDesc *desc_; |
| 2163 | |
| 2164 | /* the underlying source reader */ |
| 2165 | class CTcSrcObject *src_; |
| 2166 | |
| 2167 | /* |
| 2168 | * the enclosing stream - this is the stream that #include'd the |
| 2169 | * current stream |
| 2170 | */ |
| 2171 | CTcTokStream *parent_; |
| 2172 | |
| 2173 | /* line number of next line to be read */ |
| 2174 | ulong next_linenum_; |
| 2175 | |
| 2176 | /* #if nesting level at the start of the file */ |
| 2177 | int init_if_level_; |
| 2178 | |
| 2179 | /* newline_spacing mode when the stream was stacked */ |
| 2180 | newline_spacing_mode_t newline_spacing_; |
| 2181 | |
| 2182 | /* flag: we were unable to load the map in the #charset directive */ |
| 2183 | uint charset_error_ : 1; |
| 2184 | |
| 2185 | /* the stream is in a multi-line comment */ |
| 2186 | uint in_comment_ : 1; |
| 2187 | |
| 2188 | /* flag: we're in #pragma C+ mode */ |
| 2189 | // uint pragma_c_ : 1; - #pragma C is not currently used |
| 2190 | }; |
| 2191 | |
| 2192 | /* ------------------------------------------------------------------------ */ |
| 2193 | /* |
| 2194 | * Keyword Hash Table Entry |
| 2195 | */ |
| 2196 | class CTcHashEntryKw: public CVmHashEntryCS |
| 2197 | { |
| 2198 | public: |
| 2199 | CTcHashEntryKw(const textchar_t *str, tc_toktyp_t tokid) |
| 2200 | : CVmHashEntryCS(str, strlen(str), FALSE) |
| 2201 | { |
| 2202 | /* save the token ID for the keyword */ |
| 2203 | tokid_ = tokid; |
| 2204 | } |
| 2205 | |
| 2206 | /* get the token ID */ |
| 2207 | tc_toktyp_t get_tok_id() const { return tokid_; } |
| 2208 | |
| 2209 | private: |
| 2210 | /* our token ID */ |
| 2211 | tc_toktyp_t tokid_; |
| 2212 | }; |
| 2213 | |
| 2214 | /* ------------------------------------------------------------------------ */ |
| 2215 | /* |
| 2216 | * basic #define symbol table entry |
| 2217 | */ |
| 2218 | class CTcHashEntryPp: public CVmHashEntryCS |
| 2219 | { |
| 2220 | public: |
| 2221 | CTcHashEntryPp(const textchar_t *str, size_t len, int copy) |
| 2222 | : CVmHashEntryCS(str, len, copy) |
| 2223 | { |
| 2224 | /* by default, we have no arguments */ |
| 2225 | has_args_ = FALSE; |
| 2226 | has_varargs_ = FALSE; |
| 2227 | argc_ = 0; |
| 2228 | argv_ = 0; |
| 2229 | params_table_ = 0; |
| 2230 | } |
| 2231 | |
| 2232 | /* get the expansion text */ |
| 2233 | virtual const char *get_expansion() const = 0; |
| 2234 | virtual size_t get_expan_len() const = 0; |
| 2235 | |
| 2236 | /* get the original expansion text, before parsing */ |
| 2237 | virtual const char *get_orig_expansion() const { return get_expansion(); } |
| 2238 | virtual size_t get_orig_expan_len() const { return get_expan_len(); } |
| 2239 | |
| 2240 | /* certain special macros (__LINE__, __FILE__) aren't undef'able */ |
| 2241 | virtual int is_undefable() const { return TRUE; } |
| 2242 | |
| 2243 | /* |
| 2244 | * most macros are real symbols, created by #define's, but some are |
| 2245 | * special pseudo-macros, like __LINE__ and __FILE__, that the |
| 2246 | * preprocessor provides |
| 2247 | */ |
| 2248 | virtual int is_pseudo() const { return FALSE; } |
| 2249 | |
| 2250 | /* does the macro have an argument list? */ |
| 2251 | int has_args() const { return has_args_; } |
| 2252 | |
| 2253 | /* get the number of arguments */ |
| 2254 | int get_argc() const { return argc_; } |
| 2255 | |
| 2256 | /* do we have a variable number of arguments? */ |
| 2257 | int has_varargs() const { return has_varargs_; } |
| 2258 | |
| 2259 | /* |
| 2260 | * get the minimum number of allowed arguments - if we have varargs, |
| 2261 | * this is one less than the number of formals listed, since the last |
| 2262 | * formal can correspond to any number of actuals, including zero |
| 2263 | */ |
| 2264 | int get_min_argc() const { return has_varargs_ ? argc_ - 1 : argc_; } |
| 2265 | |
| 2266 | /* get the name of an argument by position (0 = first argument) */ |
| 2267 | const char *get_arg_name(int idx) const { return argv_[idx]; } |
| 2268 | |
| 2269 | /* get the parameter hash table entry for the parameter */ |
| 2270 | class CTcHashEntryPpArg *get_arg_entry(int idx) const |
| 2271 | { return arg_entry_[idx]; } |
| 2272 | |
| 2273 | /* get the parameters hash table */ |
| 2274 | const CVmHashTable *get_params_table() const { return params_table_; } |
| 2275 | |
| 2276 | protected: |
| 2277 | /* argument list */ |
| 2278 | char **argv_; |
| 2279 | |
| 2280 | /* list of parameter hash entries */ |
| 2281 | class CTcHashEntryPpArg **arg_entry_; |
| 2282 | |
| 2283 | /* parameter hash table */ |
| 2284 | CVmHashTable *params_table_; |
| 2285 | |
| 2286 | /* argument count */ |
| 2287 | int argc_; |
| 2288 | |
| 2289 | /* flag: the macro has a parameter list */ |
| 2290 | uint has_args_ : 1; |
| 2291 | |
| 2292 | /* |
| 2293 | * flag: the parameter list takes a variable number of arguments; if |
| 2294 | * this is set, then argc_ is one greater than the minimum number of |
| 2295 | * arguments required, and the last formal receives the varying part |
| 2296 | * of the actual parameter list, which can contain zero or more |
| 2297 | * actuals |
| 2298 | */ |
| 2299 | uint has_varargs_ : 1; |
| 2300 | }; |
| 2301 | |
| 2302 | /* |
| 2303 | * #define symbol hash table entry |
| 2304 | */ |
| 2305 | class CTcHashEntryPpDefine: public CTcHashEntryPp |
| 2306 | { |
| 2307 | public: |
| 2308 | /* |
| 2309 | * Create the hash entry. argc is the number of arguments to the |
| 2310 | * macro, and argv is an array of pointers to null-terminated |
| 2311 | * strings with the argument names, in the order defined in the |
| 2312 | * macro. |
| 2313 | * |
| 2314 | * If has_args is false, the macro does not take a parameter list at |
| 2315 | * all. Note that it is possible for has_args to be true and argc |
| 2316 | * to be zero, because a macro can be defined to take an argument |
| 2317 | * list with no arguments (i.e., empty parens). A macro with an |
| 2318 | * empty argument list is distinct from a macro with no argument |
| 2319 | * list: in the former case, the empty parens are required, and are |
| 2320 | * removed from the input stream and replaced with the macro's |
| 2321 | * expansion. |
| 2322 | * |
| 2323 | * We'll make a copy of the argument list vector, strings, and |
| 2324 | * expansion text, so the caller is free to forget all of that after |
| 2325 | * creating the entry instance. |
| 2326 | */ |
| 2327 | CTcHashEntryPpDefine(const textchar_t *str, size_t len, int copy, |
| 2328 | int has_args, int argc, int has_varargs, |
| 2329 | const char **argv, const size_t *argvlen, |
| 2330 | const char *expansion, size_t expan_len); |
| 2331 | |
| 2332 | ~CTcHashEntryPpDefine(); |
| 2333 | |
| 2334 | /* |
| 2335 | * Get the expansion text and its length. This is the parsed version |
| 2336 | * of the expansion, with occurrences of the formal parameters replaced |
| 2337 | * by TOK_MACRO_FORMAL_FLAG sequences, and the various '#' sequences |
| 2338 | * replaced by their corresponding flags. |
| 2339 | */ |
| 2340 | const char *get_expansion() const { return expan_; } |
| 2341 | size_t get_expan_len() const { return expan_len_; } |
| 2342 | |
| 2343 | /* get the original expansion - the original text before parsing */ |
| 2344 | const char *get_orig_expansion() const { return orig_expan_; } |
| 2345 | size_t get_orig_expan_len() const { return orig_expan_len_; } |
| 2346 | |
| 2347 | private: |
| 2348 | /* parse the expansion */ |
| 2349 | void parse_expansion(const size_t *argvlen); |
| 2350 | |
| 2351 | /* expansion (parsed version) */ |
| 2352 | char *expan_; |
| 2353 | size_t expan_len_; |
| 2354 | |
| 2355 | /* original expansion (before parsing) */ |
| 2356 | char *orig_expan_; |
| 2357 | size_t orig_expan_len_; |
| 2358 | }; |
| 2359 | |
| 2360 | |
| 2361 | /* |
| 2362 | * Hash table entry for __FILE__ and __LINE__ |
| 2363 | */ |
| 2364 | class CTcHashEntryPpSpecial: public CTcHashEntryPp |
| 2365 | { |
| 2366 | public: |
| 2367 | CTcHashEntryPpSpecial(CTcTokenizer *tok, const char *str) |
| 2368 | : CTcHashEntryPp(str, strlen(str), FALSE) |
| 2369 | { |
| 2370 | /* remember my tokenizer */ |
| 2371 | tok_ = tok; |
| 2372 | } |
| 2373 | |
| 2374 | /* these special macros are not undef'able */ |
| 2375 | virtual int is_undefable() const { return FALSE; } |
| 2376 | |
| 2377 | /* special macros are pseudo-macros provided by the preprocessor */ |
| 2378 | virtual int is_pseudo() const { return TRUE; } |
| 2379 | |
| 2380 | protected: |
| 2381 | /* my tokenizer */ |
| 2382 | CTcTokenizer *tok_; |
| 2383 | }; |
| 2384 | |
| 2385 | class CTcHashEntryPpFILE: public CTcHashEntryPpSpecial |
| 2386 | { |
| 2387 | public: |
| 2388 | CTcHashEntryPpFILE(CTcTokenizer *tok) |
| 2389 | : CTcHashEntryPpSpecial(tok, "__FILE__") { } |
| 2390 | |
| 2391 | /* our expansion is the current filename, in single quotes */ |
| 2392 | const char *get_expansion() const { return get_base_text(); } |
| 2393 | size_t get_expan_len() const { return strlen(get_base_text()); } |
| 2394 | |
| 2395 | private: |
| 2396 | /* get our expansion base text */ |
| 2397 | const char *get_base_text() const |
| 2398 | { |
| 2399 | /* |
| 2400 | * if we're in test-report mode, use the root name only; |
| 2401 | * otherwise, use the full name with path |
| 2402 | */ |
| 2403 | if (tok_->get_last_desc() == 0) |
| 2404 | return ""; |
| 2405 | else if (tok_->get_test_report_mode()) |
| 2406 | return tok_->get_last_desc()->get_squoted_rootname(); |
| 2407 | else |
| 2408 | return tok_->get_last_desc()->get_squoted_fname(); |
| 2409 | } |
| 2410 | }; |
| 2411 | |
| 2412 | class CTcHashEntryPpLINE: public CTcHashEntryPpSpecial |
| 2413 | { |
| 2414 | public: |
| 2415 | CTcHashEntryPpLINE(CTcTokenizer *tok) |
| 2416 | : CTcHashEntryPpSpecial(tok, "__LINE__") { } |
| 2417 | |
| 2418 | /* our expansion is the line number as a decimal string */ |
| 2419 | const char *get_expansion() const |
| 2420 | { gen_expansion(tok_); return buf_; } |
| 2421 | size_t get_expan_len() const |
| 2422 | { gen_expansion(tok_); return strlen(buf_); } |
| 2423 | |
| 2424 | private: |
| 2425 | /* generate the expansion text into our internal buffer */ |
| 2426 | static void gen_expansion(CTcTokenizer *tok) |
| 2427 | { sprintf(buf_, "%ld", tok->get_last_linenum()); } |
| 2428 | |
| 2429 | /* internal buffer */ |
| 2430 | static char buf_[20]; |
| 2431 | }; |
| 2432 | |
| 2433 | |
| 2434 | /* |
| 2435 | * Hash entry for preprocessor arguments |
| 2436 | */ |
| 2437 | class CTcHashEntryPpArg: public CVmHashEntryCS |
| 2438 | { |
| 2439 | public: |
| 2440 | CTcHashEntryPpArg(const char *str, size_t len, int copy, int argnum) |
| 2441 | : CVmHashEntryCS(str, len, copy) |
| 2442 | { |
| 2443 | /* remember the argument number */ |
| 2444 | argnum_ = argnum; |
| 2445 | } |
| 2446 | |
| 2447 | /* get my argument number */ |
| 2448 | int get_argnum() const { return argnum_; } |
| 2449 | |
| 2450 | private: |
| 2451 | /* argument number */ |
| 2452 | int argnum_; |
| 2453 | }; |
| 2454 | |
| 2455 | |
| 2456 | /* ------------------------------------------------------------------------ */ |
| 2457 | /* |
| 2458 | * Previously-included file list entry. Each time we include a file, |
| 2459 | * we'll add an entry to a list of files; in the future, we'll consult |
| 2460 | * this list to ensure that we don't include the same file again. |
| 2461 | */ |
| 2462 | struct tctok_incfile_t |
| 2463 | { |
| 2464 | /* next entry in the list of previously-included files */ |
| 2465 | tctok_incfile_t *nxt; |
| 2466 | |
| 2467 | /* name of this file (we'll allocate memory to hold the name) */ |
| 2468 | char fname[1]; |
| 2469 | }; |
| 2470 | |
| 2471 | /* ------------------------------------------------------------------------ */ |
| 2472 | /* |
| 2473 | * Include path list entry. This structure defines one include path; we |
| 2474 | * maintain a list of these structures. |
| 2475 | */ |
| 2476 | struct tctok_incpath_t |
| 2477 | { |
| 2478 | /* next entry in the list */ |
| 2479 | tctok_incpath_t *nxt; |
| 2480 | |
| 2481 | /* path */ |
| 2482 | char path[1]; |
| 2483 | }; |
| 2484 | |
| 2485 | #endif /* TCTOK_H */ |
| 2486 |