home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / src / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-11  |  2.5 KB  |  137 lines

  1. #include "c.h"
  2.  
  3. static void printtoken ARGS((void));
  4. int errcnt   = 0;
  5. int errlimit = 20;
  6. int warningCount = 0;
  7. char kind[] = {
  8. #define xx(a,b,c,d,e,f,g) f,
  9. #define yy(a,b,c,d,e,f,g) f,
  10. #include "token.h"
  11. };
  12. int wflag;        /* != 0 to suppress warning messages */
  13.  
  14. void test(int tok,char set[])
  15. {
  16.     if (t == tok)
  17.         t = gettok();
  18.     else {
  19.         expect(tok);
  20.         skipto(tok, set);
  21.         if (t == tok)
  22.             t = gettok();
  23.     }
  24. }
  25. void expect(int tok)
  26. {
  27.     if (t == tok)
  28.         t = gettok();
  29.     else {
  30.         error("syntax error; found");
  31.         printtoken();
  32.  
  33.         fprint(2, " expecting `%k'\n", tok);
  34.     }
  35. }
  36. void error(char *fmt, ...) {
  37.     va_list ap;
  38.  
  39.     if (errcnt++ >= errlimit) {
  40.         errcnt = -1;
  41.         error("too many errors\n");
  42.         exit(1);
  43.     }
  44.     va_init(ap, fmt);
  45.     fprint(2,"Error ");
  46.     if (firstfile != file && firstfile && *firstfile)
  47.         fprint(2, "%s ", firstfile);
  48.     fprint(2, "%w ", &src);
  49.     vfprint(2, fmt, ap);
  50.     va_end(ap);
  51. }
  52.  
  53. void skipto(int tok,char set[])
  54. {
  55.     int n;
  56.     char *s;
  57.  
  58.     assert(set);
  59.     for (n = 0; t != EOI && t != tok; t = gettok()) {
  60.         for (s = set; *s && kind[t] != *s; s++)
  61.             ;
  62.         if (kind[t] == *s)
  63.             break;
  64.         if (n++ == 0)
  65.             error("skipping");
  66.         if (n <= 8)
  67.             printtoken();
  68.         else if (n == 9)
  69.             fprint(2, " ...");
  70.     }
  71.     if (n > 8) {
  72.         fprint(2, " up to");
  73.         printtoken();
  74.     }
  75.     if (n > 0)
  76.         fprint(2, "\n");
  77. }
  78. /* fatal - issue fatal error message and exit */
  79. int fatal(char *name,char *fmt,int n)
  80. {
  81.     *bp++ = '\n';
  82.     outflush();
  83.     errcnt = -1;
  84.     error("compiler error in %s--", name);
  85.     fprint(2, fmt, n);
  86.     exit(1);
  87.     return 0;
  88. }
  89.  
  90. /* printtoken - print current token preceeded by a space */
  91. static void printtoken(void) 
  92. {
  93.     switch (t) {
  94.     case ID: fprint(2, " `%s'", token); break;
  95.     case ICON:
  96.         if (*token == '\'') {
  97.             char *s;
  98.     case SCON:    fprint(2, " ");
  99.             for (s = token; *s && s - token < 20; s++)
  100.                 if (*s < ' ' || *s >= 0177)
  101.                     fprint(2, "\\%o", *s);
  102.                 else
  103.                     fprint(2, "%c", *s);
  104.             if (*s)
  105.                 fprint(2, " ...");
  106.             else
  107.                 fprint(2, "%c", *token);
  108.             break;
  109.         } /* else fall thru */
  110.     case FCON: {
  111.         char c = *cp;
  112.         *cp = 0;
  113.         fprint(2, " `%s'", token);
  114.         *cp = c;
  115.         break;
  116.         }
  117.     case '`': case '\'': fprint(2, " \"%k\"", t); break;
  118.     default: fprint(2, " `%k'", t);
  119.     }
  120. }
  121.  
  122. /* warning - issue warning error message */
  123. void warning (char *fmt, ...) {
  124.     va_list ap;
  125.  
  126.     va_init(ap, fmt);
  127.     if (wflag == 0) {
  128.         fprint(2,"Warning ");
  129.         if (firstfile != file && firstfile && *firstfile)
  130.             fprint(2, "%s: ", firstfile);
  131.         fprint(2, "%w ", &src);
  132.         vfprint(2, fmt, ap);
  133.         warningCount++;
  134.     }
  135.     va_end(ap);
  136. }
  137.