home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / common / error.h < prev    next >
C/C++ Source or Header  |  1996-03-22  |  4KB  |  184 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. hidden    char    *mapterm    Params((int typ,struct node *val));
  14.  
  15. /*
  16.  * yyerror produces syntax error messages.  tok is the offending token
  17.  *  (yychar), lval is yylval, and state is the parser's state.
  18.  *
  19.  * errtab is searched for the state, if it is found, the associated
  20.  *  message is produced; if the state isn't found, "syntax error"
  21.  *  is produced.
  22.  */
  23. novalue yyerror(tok, lval, state)
  24. int tok, state;
  25. nodeptr lval;
  26.    {
  27.    register struct errmsg *p;
  28.    int line;
  29.  
  30.    if (lval == NULL)
  31.       line = 0;
  32.    else
  33.       line = Line(lval);
  34.  
  35.  
  36.    if (tok_loc.n_file)
  37.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  38.    if (tok == EOFX)   /* special case end of file */
  39.       fprintf(stderr, "unexpected end of file\n");
  40.    else {
  41.       fprintf(stderr, "Line %d # ", line);
  42.       if (Col(lval))
  43.          fprintf(stderr, "\"%s\": ", mapterm(tok,lval));
  44.       for (p = errtab; p->e_state != state && p->e_state >= 0; p++) ;
  45.       fprintf(stderr, "%s\n", p->e_mesg);
  46.       }
  47.    tfatals++;
  48.    nocode++;
  49.    }
  50.  
  51. /*
  52.  * mapterm finds a printable string for the given token type
  53.  *  and value.
  54.  */
  55. static char *mapterm(typ,val)
  56. int typ;
  57. nodeptr val;
  58.    {
  59.    register struct toktab *t;
  60.    register struct optab *ot;
  61.    register int i;
  62.  
  63.    i = typ;
  64.    if (i == IDENT || i == INTLIT || i == REALLIT || i == STRINGLIT ||
  65.       i == CSETLIT)
  66.          return Str0(val);
  67.    for (t = toktab; t->t_type != 0; t++)
  68.       if (t->t_type == i)
  69.          return t->t_word;
  70.    for (ot = optab; ot->tok.t_type != 0; ot++)
  71.       if (ot->tok.t_type == i)
  72.          return ot->tok.t_word;
  73.    return "???";
  74.    }
  75.  
  76. /*
  77.  * tfatal produces the translator error messages s1 and s2 (if nonnull).  The
  78.  *  location of the error is found in tok_loc.
  79.  */
  80. novalue tfatal(s1, s2)
  81. char *s1, *s2;
  82.    {
  83.  
  84.    if (tok_loc.n_file)
  85.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  86.    fprintf(stderr, "Line %d # ", tok_loc.n_line);
  87.    if (s2)
  88.       fprintf(stderr, "\"%s\": ", s2);
  89.    fprintf(stderr, "%s\n", s1);
  90.  
  91.    tfatals++;
  92.    nocode++;
  93.    }
  94.  
  95. /*
  96.  * nfatal produces the error messages s1 and s2 (if nonnull), and associates
  97.  *  it with source location of node.
  98.  */
  99. novalue nfatal(n, s1, s2)
  100. nodeptr n;
  101. char *s1, *s2;
  102.    {
  103.  
  104.    if (n != NULL) {
  105.       fprintf(stderr, "File %s; ", File(n));
  106.       fprintf(stderr, "Line %d # ", Line(n));
  107.       }
  108.    if (s2)
  109.       fprintf(stderr, "\"%s\": ", s2);
  110.    fprintf(stderr, "%s\n", s1);
  111.  
  112.    tfatals++;
  113.    nocode++;
  114.    }
  115.  
  116. #ifdef Iconc
  117. /*
  118.  * twarn produces s1 and s2 (if nonnull) as translator warning messages.
  119.  *  The location of the error is found in tok_loc.
  120.  */
  121. novalue twarn(s1, s2)
  122. char *s1, *s2;
  123.    {
  124.  
  125.    if (tok_loc.n_file)
  126.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  127.    fprintf(stderr, "Line %d # ", tok_loc.n_line);
  128.    if (s2)
  129.       fprintf(stderr, "\"%s\": ", s2);
  130.    fprintf(stderr, "%s\n", s1);
  131.    twarns++;
  132.    }
  133. #endif                    /* Iconc */
  134.  
  135. /*
  136.  * tsyserr is called for fatal errors.  The message s is produced and the
  137.  *  translator exits.
  138.  */
  139. novalue tsyserr(s)
  140. char *s;
  141.    {
  142.  
  143.  
  144.    if (tok_loc.n_file)
  145.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  146.    fprintf(stderr, "Line %d # %s\n", in_line, s);
  147.  
  148.    exit(ErrorExit);
  149.    }
  150.  
  151. /*
  152.  * quit - immediate exit with error message
  153.  */
  154.  
  155. novalue quit(msg)
  156. char *msg;
  157.    {
  158.    quitf(msg,"");
  159.    }
  160.  
  161. /*
  162.  * quitf - immediate exit with message format and argument
  163.  */
  164. novalue quitf(msg,arg)
  165. char *msg, *arg;
  166.    {
  167.  
  168.  
  169.    extern char *progname;
  170.    fprintf(stderr,"%s: ",progname);
  171.    fprintf(stderr,msg,arg);
  172.    fprintf(stderr,"\n");
  173.  
  174. #if !defined(VarTran) && !defined(Iconc)
  175.    {
  176.       extern char *ofile;
  177.       if (ofile)
  178.      unlink(ofile);            /* remove bad icode file */
  179.    }
  180. #endif                    /* !VarTran && !Iconc */
  181.  
  182.    exit(ErrorExit);
  183.    }
  184.