scan.c (10115B)
1 #include <ctype.h> 2 #include <stdarg.h> 3 #include <stdbool.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include "util.h" 8 #include "cc.h" 9 10 struct buffer { 11 unsigned char *str; 12 size_t len, cap; 13 }; 14 15 struct scanner { 16 int chr; 17 bool usebuf; 18 bool sawspace; 19 FILE *file; 20 struct location loc; 21 struct buffer buf; 22 struct scanner *next; 23 }; 24 25 static struct scanner *scanner; 26 27 static void 28 bufadd(struct buffer *b, int c) 29 { 30 if (b->len >= b->cap) { 31 b->cap = b->cap ? b->cap * 2 : 1 << 8; 32 b->str = xreallocarray(b->str, b->cap, 1); 33 } 34 b->str[b->len++] = c; 35 } 36 37 static char * 38 bufget(struct buffer *b) 39 { 40 char *s; 41 42 bufadd(b, '\0'); 43 s = xmalloc(b->len); 44 memcpy(s, b->str, b->len); 45 b->len = 0; 46 47 return s; 48 } 49 50 static void 51 nextchar(struct scanner *s) 52 { 53 int c; 54 55 if (s->usebuf) 56 bufadd(&s->buf, s->chr); 57 for (;;) { 58 s->chr = getc(s->file); 59 if (s->chr == '\n') { 60 ++s->loc.line, s->loc.col = 0; 61 break; 62 } 63 ++s->loc.col; 64 if (s->chr != '\\') 65 break; 66 c = getc(s->file); 67 if (c != '\n') { 68 ungetc(c, s->file); 69 break; 70 } 71 ++s->loc.line, s->loc.col = 0; 72 } 73 } 74 75 static int 76 op2(struct scanner *s, int t1, int t2) 77 { 78 nextchar(s); 79 if (s->chr != '=') 80 return t1; 81 nextchar(s); 82 return t2; 83 } 84 85 static int 86 op3(struct scanner *s, int t1, int t2, int t3) 87 { 88 int c; 89 90 c = s->chr; 91 nextchar(s); 92 if (s->chr == '=') { 93 nextchar(s); 94 return t2; 95 } 96 if (s->chr != c) 97 return t1; 98 nextchar(s); 99 return t3; 100 } 101 102 static int 103 op4(struct scanner *s, int t1, int t2, int t3, int t4) 104 { 105 int c; 106 107 c = s->chr; 108 nextchar(s); 109 if (s->chr == '=') { 110 nextchar(s); 111 return t2; 112 } 113 if (s->chr != c) 114 return t1; 115 nextchar(s); 116 if (s->chr != '=') 117 return t3; 118 nextchar(s); 119 return t4; 120 } 121 122 static int 123 ident(struct scanner *s) 124 { 125 int tok; 126 127 s->usebuf = true; 128 while (isalnum(s->chr) || s->chr == '_') 129 nextchar(s); 130 tok = tokenget(s->buf.str, s->buf.len); 131 s->usebuf = false; 132 s->buf.len = 0; 133 return tok; 134 } 135 136 static int 137 isodigit(int c) 138 { 139 return (unsigned)c - '0' < 8; 140 } 141 142 static enum tokenkind 143 number(struct scanner *s) 144 { 145 bool allowsign = false; 146 147 s->usebuf = true; 148 for (;;) { 149 nextchar(s); 150 switch (s->chr) { 151 case 'e': 152 case 'E': 153 case 'p': 154 case 'P': 155 allowsign = true; 156 break; 157 case '+': 158 case '-': 159 if (!allowsign) 160 goto done; 161 break; 162 case '.': 163 allowsign = false; 164 break; 165 case '\'': 166 nextchar(s); 167 --s->buf.len; /* ignore separator */ 168 /* fallthrough */ 169 default: 170 if (!isalnum(s->chr) && s->chr != '_') 171 goto done; 172 allowsign = false; 173 } 174 } 175 done: 176 return TNUMBER; 177 } 178 179 static void 180 escape(struct scanner *s) 181 { 182 nextchar(s); 183 if (s->chr == 'x') { 184 nextchar(s); 185 if (!isxdigit(s->chr)) 186 error(&s->loc, "invalid hexadecimal escape sequence"); 187 do 188 nextchar(s); 189 while (isxdigit(s->chr)); 190 } else if (isodigit(s->chr)) { 191 nextchar(s); 192 if (isodigit(s->chr)) { 193 nextchar(s); 194 if (isodigit(s->chr)) 195 nextchar(s); 196 } 197 } else if (s->chr == 'u' || s->chr == 'U') { 198 /* a universal character name: four or eight hexadecimal digits */ 199 int n = s->chr == 'u' ? 4 : 8; 200 201 nextchar(s); 202 while (n-- > 0) { 203 if (!isxdigit(s->chr)) 204 error(&s->loc, "invalid universal character name"); 205 nextchar(s); 206 } 207 } else if (strchr("'\"?\\abfnrtv", s->chr)) { 208 nextchar(s); 209 } else { 210 error(&s->loc, "invalid escape sequence"); 211 } 212 } 213 214 static enum tokenkind 215 charconst(struct scanner *s) 216 { 217 s->usebuf = true; 218 nextchar(s); 219 for (;;) { 220 switch (s->chr) { 221 case '\\': 222 escape(s); 223 break; 224 case '\'': 225 nextchar(s); 226 return TCHARCONST; 227 case '\n': 228 error(&s->loc, "newline in character constant"); 229 case EOF: 230 error(&s->loc, "EOF in character constant"); 231 default: 232 nextchar(s); 233 break; 234 } 235 } 236 } 237 238 static int 239 stringlit(struct scanner *s) 240 { 241 s->usebuf = true; 242 nextchar(s); 243 for (;;) { 244 switch (s->chr) { 245 case '\\': 246 escape(s); 247 break; 248 case '"': 249 nextchar(s); 250 return TSTRINGLIT; 251 case '\n': 252 error(&s->loc, "newline in string literal"); 253 case EOF: 254 error(&s->loc, "EOF in string literal"); 255 default: 256 nextchar(s); 257 break; 258 } 259 } 260 } 261 262 static bool 263 comment(struct scanner *s) 264 { 265 int last; 266 267 switch (s->chr) { 268 case '/': /* c++-style comment */ 269 do 270 nextchar(s); 271 while (s->chr != '\n' && s->chr != EOF); 272 break; 273 case '*': /* c-style comment */ 274 nextchar(s); 275 do { 276 last = s->chr; 277 nextchar(s); 278 if (s->chr == EOF) 279 error(&s->loc, "EOF in comment"); 280 } while (last != '*' || s->chr != '/'); 281 nextchar(s); 282 break; 283 default: 284 return false; 285 } 286 s->sawspace = true; 287 return true; 288 } 289 290 static int 291 scankind(struct scanner *s, struct location *loc) 292 { 293 enum tokenkind tok; 294 struct location oldloc; 295 296 again: 297 *loc = s->loc; 298 switch (s->chr) { 299 case ' ': 300 case '\t': 301 case '\f': 302 case '\v': 303 s->sawspace = true; 304 nextchar(s); 305 goto again; 306 case '!': 307 return op2(s, TLNOT, TNEQ); 308 case '"': 309 return stringlit(s); 310 case '#': 311 nextchar(s); 312 if (s->chr != '#') 313 return THASH; 314 nextchar(s); 315 return THASHHASH; 316 case '%': 317 return op2(s, TMOD, TMODASSIGN); 318 case '&': 319 return op3(s, TBAND, TBANDASSIGN, TLAND); 320 case '\'': 321 return charconst(s); 322 case '*': 323 return op2(s, TMUL, TMULASSIGN); 324 case '+': 325 return op3(s, TADD, TADDASSIGN, TINC); 326 case '-': 327 tok = op3(s, TSUB, TSUBASSIGN, TDEC); 328 if (tok != TSUB || s->chr != '>') 329 return tok; 330 nextchar(s); 331 return TARROW; 332 case '/': 333 tok = op2(s, TDIV, TDIVASSIGN); 334 if (tok == TDIV && comment(s)) 335 goto again; 336 return tok; 337 case '<': 338 return op4(s, TLESS, TLEQ, TSHL, TSHLASSIGN); 339 case '=': 340 return op2(s, TASSIGN, TEQL); 341 case '>': 342 return op4(s, TGREATER, TGEQ, TSHR, TSHRASSIGN); 343 case '^': 344 return op2(s, TXOR, TXORASSIGN); 345 case '|': 346 return op3(s, TBOR, TBORASSIGN, TLOR); 347 case '\n': 348 nextchar(s); 349 return TNEWLINE; 350 case '[': 351 nextchar(s); 352 return TLBRACK; 353 case ']': 354 nextchar(s); 355 return TRBRACK; 356 case '(': 357 nextchar(s); 358 return TLPAREN; 359 case ')': 360 nextchar(s); 361 return TRPAREN; 362 case '{': 363 nextchar(s); 364 return TLBRACE; 365 case '}': 366 nextchar(s); 367 return TRBRACE; 368 case '.': 369 nextchar(s); 370 if (isdigit(s->chr)) { 371 bufadd(&s->buf, '.'); 372 return number(s); 373 } 374 if (s->chr != '.') 375 return TPERIOD; 376 oldloc = s->loc; 377 nextchar(s); 378 if (s->chr != '.') { 379 ungetc(s->chr, s->file); 380 s->loc = oldloc; 381 s->chr = '.'; 382 return TPERIOD; 383 } 384 nextchar(s); 385 return TELLIPSIS; 386 case '~': 387 nextchar(s); 388 return TBNOT; 389 case '?': 390 nextchar(s); 391 return TQUESTION; 392 case ':': 393 nextchar(s); 394 if (s->chr != ':') 395 return TCOLON; 396 nextchar(s); 397 return TCOLONCOLON; 398 case ';': 399 nextchar(s); 400 return TSEMICOLON; 401 case ',': 402 nextchar(s); 403 return TCOMMA; 404 case 'L': 405 case 'U': 406 case 'u': 407 s->usebuf = true; 408 nextchar(s); 409 if (s->buf.str[0] == 'u' && s->chr == '8') 410 nextchar(s); 411 switch (s->chr) { 412 case '\'': 413 return charconst(s); 414 case '"': 415 return stringlit(s); 416 } 417 return ident(s); 418 case EOF: 419 return TEOF; 420 default: 421 if (isdigit(s->chr)) 422 return number(s); 423 if (isalpha(s->chr) || s->chr == '_') 424 return ident(s); 425 s->usebuf = true; 426 nextchar(s); 427 return TOTHER; 428 } 429 } 430 431 void 432 scanfrom(const char *name, FILE *file) 433 { 434 struct scanner *s; 435 436 s = xmalloc(sizeof(*s)); 437 s->file = file; 438 s->buf.str = NULL; 439 s->buf.len = 0; 440 s->buf.cap = 0; 441 s->usebuf = false; 442 s->loc.file = name; 443 s->loc.line = 1; 444 s->loc.col = 0; 445 s->next = scanner; 446 if (file) 447 nextchar(s); 448 scanner = s; 449 } 450 451 void 452 scanopen(void) 453 { 454 if (!scanner->file) { 455 scanner->file = fopen(scanner->loc.file, "r"); 456 if (!scanner->file) 457 fatal("open %s:", scanner->loc.file); 458 nextchar(scanner); 459 } 460 } 461 462 void 463 scansetloc(struct location loc) 464 { 465 scanner->loc = loc; 466 } 467 468 char * 469 scaninclude(bool *is_system) 470 { 471 struct scanner *s = scanner; 472 int delim; 473 char *name; 474 475 while (s->chr == ' ' || s->chr == '\t' || s->chr == '\f' || s->chr == '\v') 476 nextchar(s); 477 478 if (s->chr == '"') { 479 delim = '"'; 480 *is_system = false; 481 } else if (s->chr == '<') { 482 delim = '>'; 483 *is_system = true; 484 } else { 485 return NULL; 486 } 487 488 nextchar(s); 489 s->usebuf = true; 490 while (s->chr != delim && s->chr != '\n' && s->chr != EOF) { 491 nextchar(s); 492 } 493 s->usebuf = false; 494 495 if (s->chr != delim) { 496 free(s->buf.str); 497 s->buf.len = 0; 498 s->buf.cap = 0; 499 return NULL; 500 } 501 502 nextchar(s); 503 504 name = xmalloc(s->buf.len + 1); 505 memcpy(name, s->buf.str, s->buf.len); 506 name[s->buf.len] = '\0'; 507 s->buf.len = 0; 508 509 return name; 510 } 511 512 static void 513 scanclose(void) 514 { 515 fclose(scanner->file); 516 free(scanner->buf.str); 517 free(scanner); 518 } 519 520 void 521 scan(struct token *t) 522 { 523 struct scanner *next; 524 525 scanner->sawspace = false; 526 for (;;) { 527 t->kind = scankind(scanner, &t->loc); 528 if (t->kind != TEOF || !scanner->next) 529 break; 530 /* the file that ended is freed: where to go on is taken first */ 531 next = scanner->next; 532 scanclose(); 533 scanner = next; 534 scanopen(); 535 } 536 if (scanner->usebuf) { 537 t->lit = bufget(&scanner->buf); 538 scanner->usebuf = false; 539 } else { 540 t->lit = NULL; 541 } 542 t->space = scanner->sawspace; 543 t->hide = false; 544 }