home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / common / error.h < prev    next >
C/C++ Source or Header  |  2000-07-29  |  5KB  |  232 lines

  1. /*
  2.  * error.h -- routines for producing error messages.
  3.  *
  4.  * This source file contains the routines for issuing error messages.
  5.  * It is built by inclusion in ../icont/tlex.c and ../iconc/clex.c,
  6.  * with slight variations depending on whether "Iconc" is defined.
  7.  */
  8.  
  9. /*
  10.  * Prototype.
  11.  */
  12.  
  13. static    char    *mapterm    (int typ,struct node *val);
  14.  
  15. #if AMIGA && __SASC
  16.    extern void PostClip(char *file, int line, int number, char *text);
  17. #endif                /* AMIGA && __SASC */
  18.  
  19. /*
  20.  * yyerror produces syntax error messages.  tok is the offending token
  21.  *  (yychar), lval is yylval, and state is the parser's state.
  22.  *
  23.  * errtab is searched for the state, if it is found, the associated
  24.  *  message is produced; if the state isn't found, "syntax error"
  25.  *  is produced.
  26.  */
  27. void yyerror(tok, lval, state)
  28. int tok, state;
  29. nodeptr lval;
  30.    {
  31.    register struct errmsg *p;
  32.    int line;
  33. #ifdef ConsoleWindow
  34.    extern int silent;
  35. #endif                    /* ConsoleWindow */
  36.  
  37.    if (lval == NULL)
  38.       line = 0;
  39.    else
  40.       line = Line(lval);
  41.  
  42. #ifdef ConsoleWindow
  43.    if (!silent) {
  44. #endif                    /* ConsoleWindow */
  45.    if (tok_loc.n_file)
  46.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  47.    if (tok == EOFX)   /* special case end of file */
  48.       fprintf(stderr, "unexpected end of file\n");
  49.    else {
  50.       fprintf(stderr, "Line %d # ", line);
  51.       if (Col(lval))
  52.          fprintf(stderr, "\"%s\": ", mapterm(tok,lval));
  53.       for (p = errtab; p->e_state != state && p->e_state >= 0; p++) ;
  54.       fprintf(stderr, "%s\n", p->e_mesg);
  55. #if AMIGA && __SASC
  56.       if (tok_loc.n_file) PostClip(tok_loc.n_file, line, 0, p->e_mesg);
  57. #endif                /* AMIGA && __SASC */
  58.       }
  59. #ifdef ConsoleWindow
  60.       }
  61.    else if (flog != NULL) {
  62.    if (tok_loc.n_file)
  63.       fprintf(flog, "File %s; ", tok_loc.n_file);
  64.    if (tok == EOFX)   /* special case end of file */
  65.       fprintf(flog, "unexpected end of file\n");
  66.    else {
  67.       fprintf(flog, "Line %d # ", line);
  68.       if (Col(lval))
  69.          fprintf(flog, "\"%s\": ", mapterm(tok,lval));
  70.       for (p = errtab; p->e_state != state && p->e_state >= 0; p++) ;
  71.       fprintf(flog, "%s\n", p->e_mesg);
  72.       }
  73.       }
  74. #endif                    /* ConsoleWindow */
  75.    tfatals++;
  76.    nocode++;
  77.    }
  78.  
  79. /*
  80.  * mapterm finds a printable string for the given token type
  81.  *  and value.
  82.  */
  83. static char *mapterm(typ,val)
  84. int typ;
  85. nodeptr val;
  86.    {
  87.    register struct toktab *t;
  88.    register struct optab *ot;
  89.    register int i;
  90.  
  91.    i = typ;
  92.    if (i == IDENT || i == INTLIT || i == REALLIT || i == STRINGLIT ||
  93.       i == CSETLIT)
  94.          return Str0(val);
  95.    for (t = toktab; t->t_type != 0; t++)
  96.       if (t->t_type == i)
  97.          return t->t_word;
  98.    for (ot = optab; ot->tok.t_type != 0; ot++)
  99.       if (ot->tok.t_type == i)
  100.          return ot->tok.t_word;
  101.    return "???";
  102.    }
  103.  
  104. /*
  105.  * tfatal produces the translator error messages s1 and s2 (if nonnull).  The
  106.  *  location of the error is found in tok_loc.
  107.  */
  108. void tfatal(s1, s2)
  109. char *s1, *s2;
  110.    {
  111.  
  112.    if (tok_loc.n_file)
  113.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  114.    fprintf(stderr, "Line %d # ", tok_loc.n_line);
  115.    if (s2)
  116.       fprintf(stderr, "\"%s\": ", s2);
  117.    fprintf(stderr, "%s\n", s1);
  118.  
  119. #if AMIGA && __SASC
  120.    if (tok_loc.n_file) {
  121.       char text[512];
  122.       if (s2)
  123.          sprintf(text, "\"%s\": ", s2);
  124.       strcat(text, s1);
  125.       PostClip(tok_loc.n_file, tok_loc.n_line, 0, text);
  126.       }
  127. #endif                /* AMIGA && __SASC */
  128.  
  129.    tfatals++;
  130.    nocode++;
  131.    }
  132.  
  133. /*
  134.  * nfatal produces the error messages s1 and s2 (if nonnull), and associates
  135.  *  it with source location of node.
  136.  */
  137. void nfatal(n, s1, s2)
  138. nodeptr n;
  139. char *s1, *s2;
  140.    {
  141.  
  142.    if (n != NULL) {
  143.       fprintf(stderr, "File %s; ", File(n));
  144.       fprintf(stderr, "Line %d # ", Line(n));
  145.       }
  146.    if (s2)
  147.       fprintf(stderr, "\"%s\": ", s2);
  148.    fprintf(stderr, "%s\n", s1);
  149.  
  150. #if AMIGA && __SASC
  151.    if (n != NULL) {
  152.       char text[512];
  153.       if (s2)
  154.          sprintf(text, "\"%s\": ", s2);
  155.       strcat(text, s1);
  156.       PostClip(File(n), Line(n), 0, text);
  157.       }
  158. #endif                /* AMIGA && __SASC */
  159.  
  160.    tfatals++;
  161.    nocode++;
  162.    }
  163.  
  164. #ifdef Iconc
  165. /*
  166.  * twarn produces s1 and s2 (if nonnull) as translator warning messages.
  167.  *  The location of the error is found in tok_loc.
  168.  */
  169. void twarn(s1, s2)
  170. char *s1, *s2;
  171.    {
  172.  
  173.    if (tok_loc.n_file)
  174.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  175.    fprintf(stderr, "Line %d # ", tok_loc.n_line);
  176.    if (s2)
  177.       fprintf(stderr, "\"%s\": ", s2);
  178.    fprintf(stderr, "%s\n", s1);
  179.    twarns++;
  180.    }
  181. #endif                    /* Iconc */
  182.  
  183. /*
  184.  * tsyserr is called for fatal errors.  The message s is produced and the
  185.  *  translator exits.
  186.  */
  187. void tsyserr(s)
  188. char *s;
  189.    {
  190.  
  191.  
  192.    if (tok_loc.n_file)
  193.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  194.    fprintf(stderr, "Line %d # %s\n", in_line, s);
  195.  
  196.    exit(EXIT_FAILURE);
  197.    }
  198.  
  199. /*
  200.  * quit - immediate exit with error message
  201.  */
  202.  
  203. void quit(msg)
  204. char *msg;
  205.    {
  206.    quitf(msg,"");
  207.    }
  208.  
  209. /*
  210.  * quitf - immediate exit with message format and argument
  211.  */
  212. void quitf(msg,arg)
  213. char *msg, *arg;
  214.    {
  215.  
  216.  
  217.    extern char *progname;
  218.    fprintf(stderr,"%s: ",progname);
  219.    fprintf(stderr,msg,arg);
  220.    fprintf(stderr,"\n");
  221.  
  222. #if !defined(VarTran) && !defined(Iconc)
  223.    {
  224.       extern char *ofile;
  225.       if (ofile)
  226.      remove(ofile);            /* remove bad icode file */
  227.    }
  228. #endif                    /* !VarTran && !Iconc */
  229.  
  230.    exit(EXIT_FAILURE);
  231.    }
  232.