aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

token.c (4081B)


      1 #include <assert.h>
      2 #include <ctype.h>
      3 #include <limits.h>
      4 #include <stdarg.h>
      5 #include <stdbool.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include "util.h"
     10 #include "cc.h"
     11 
     12 struct token        tok;
     13 static struct map   tokmap; /* maps string to token */
     14 static struct array tokstr; /* maps token to string */
     15 
     16 void
     17 tokeninit(void)
     18 {
     19   static const unsigned char strings[] =
     20 #define TOKEN(t, s) s "\0"
     21 #include "tokens.h"
     22 #undef TOKEN
     23       ;
     24   const unsigned char *s, *e;
     25   struct mapkey        k;
     26   size_t               l, i;
     27 
     28   mapinit(&tokmap, 1024);
     29   for (s = strings, e = strings + sizeof strings - 1; s != e; s += l + 1) {
     30     l = strlen((char *)s);
     31     if (l) {
     32       mapkey(&k, s, l);
     33       mapput(&tokmap, &k, &i);
     34       tokmap.vals[i].i = tokstr.len / sizeof(void *);
     35       arrayaddptr(&tokstr, (void *)s);
     36     } else {
     37       arrayaddptr(&tokstr, NULL);
     38     }
     39   }
     40 }
     41 
     42 int
     43 tokenget(const void *str, size_t len)
     44 {
     45   static char  *pos, *end;
     46   struct mapkey k;
     47   size_t        i;
     48   char         *buf;
     49 
     50   mapkey(&k, str, len);
     51   if (mapput(&tokmap, &k, &i)) {
     52     if (len > 8192)
     53       fatal("token is too long");
     54     if (INT_MAX < tokstr.len / sizeof(void *))
     55       fatal("too many tokens");
     56     if (!pos || (size_t)(end - pos) < len + 1) {
     57       buf = xmalloc(8192);
     58       pos = buf;
     59       end = buf + 8192;
     60     }
     61     memcpy(pos, str, len);
     62     pos[len]           = '\0';
     63     tokmap.keys[i].str = pos;
     64     tokmap.vals[i].i   = tokstr.len / sizeof(void *);
     65     arrayaddptr(&tokstr, pos);
     66     pos += len + 1;
     67   }
     68   return tokmap.vals[i].i;
     69 }
     70 
     71 char *
     72 tokenstr(enum tokenkind kind)
     73 {
     74   assert(kind < tokstr.len / sizeof(void *));
     75   return ((void **)tokstr.val)[kind];
     76 }
     77 
     78 void
     79 tokenprint(const struct token *t, FILE *f)
     80 {
     81   const char *str;
     82 
     83   if (t->space)
     84     fputc(' ', f);
     85   switch (t->kind) {
     86     case TNUMBER:
     87     case TCHARCONST:
     88     case TSTRINGLIT:
     89       str = t->lit;
     90       break;
     91     case TNEWLINE:
     92       str = "\n";
     93       break;
     94     case TEOF:
     95       return;
     96     default:
     97       str = tokenstr(t->kind);
     98   }
     99   if (!str)
    100     fatal("cannot print token %d", t->kind);
    101   fputs(str, f);
    102 }
    103 
    104 static void
    105 tokendesc(char *buf, size_t len, enum tokenkind kind, const char *lit)
    106 {
    107   const char *class;
    108   bool quote = true;
    109 
    110   assert(kind < tokstr.len / sizeof(void *));
    111   switch (kind) {
    112     case TEOF:
    113       class = "EOF";
    114       break;
    115     case TNUMBER:
    116       class = "number";
    117       quote = true;
    118       break;
    119     case TCHARCONST:
    120       class = "character";
    121       quote = false;
    122       break;
    123     case TSTRINGLIT:
    124       class = "string";
    125       quote = false;
    126       break;
    127     case TNEWLINE:
    128       class = "newline";
    129       break;
    130     case TOTHER:
    131       class = NULL;
    132       break;
    133     default:
    134       if (kind >= TIDENT)
    135         class = "identifier", quote = true;
    136       else
    137         class = NULL;
    138       lit = ((void **)tokstr.val)[kind];
    139   }
    140   if (class && lit)
    141     snprintf(buf, len, quote ? "%s '%s'" : "%s %s", class, lit);
    142   else if (class)
    143     snprintf(buf, len, "%s", class);
    144   else if (kind == TOTHER && !isprint(*(unsigned char *)lit))
    145     snprintf(buf, len, "<U+%04x>", *(unsigned char *)lit);
    146   else if (lit)
    147     snprintf(buf, len, "'%s'", lit);
    148   else
    149     snprintf(buf, len, "<unknown>");
    150 }
    151 
    152 char *
    153 tokencheck(const struct token *t, enum tokenkind kind, const char *msg)
    154 {
    155   char want[64], got[64];
    156 
    157   if (kind == TPPIDENT || kind == TIDENT) {
    158     if (t->kind < kind) {
    159       strcpy(want, "identifier");
    160       goto err;
    161     }
    162     return tokenstr(t->kind);
    163   }
    164   if (t->kind != kind && (kind != TIDENT || t->kind < TIDENT)) {
    165     tokendesc(want, sizeof(want), kind, NULL);
    166   err:
    167     tokendesc(got, sizeof(got), t->kind, t->lit);
    168     error(&t->loc, "expected %s %s, saw %s", want, msg, got);
    169   }
    170   return t->lit;
    171 }
    172 
    173 void
    174 diagloc(const struct location *loc)
    175 {
    176   fprintf(stderr, "%s:%zu:%zu: ", loc->file, loc->line, loc->col);
    177 }
    178 
    179 void
    180 error(const struct location *loc, const char *fmt, ...)
    181 {
    182   va_list ap;
    183 
    184   diagloc(loc);
    185   fputs("error: ", stderr);
    186   va_start(ap, fmt);
    187   vfprintf(stderr, fmt, ap);
    188   va_end(ap);
    189   putc('\n', stderr);
    190   exit(1);
    191 }