PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/common/error.h

https://bitbucket.org/chemoelectric/cficon
C Header | 152 lines | 101 code | 13 blank | 38 comment | 34 complexity | fa09ff3663fa6d4b0b5de070968171ba MD5 | raw file
  1. /*
  2. * error.h -- routines for producing error messages.
  3. *
  4. * This source file contains the routines for issuing error messages.
  5. */
  6. /*
  7. * Prototype.
  8. */
  9. static char *mapterm (int typ,struct node *val);
  10. /*
  11. * yyerror produces syntax error messages. tok is the offending token
  12. * (yychar), lval is yylval, and state is the parser's state.
  13. *
  14. * errtab is searched for the state, if it is found, the associated
  15. * message is produced; if the state isn't found, "syntax error"
  16. * is produced.
  17. */
  18. void yyerror(tok, lval, state)
  19. int tok, state;
  20. nodeptr lval;
  21. {
  22. register struct errmsg *p;
  23. int line;
  24. if (lval == NULL)
  25. line = 0;
  26. else
  27. line = Line(lval);
  28. if (tok_loc.n_file)
  29. fprintf(stderr, "File %s; ", tok_loc.n_file);
  30. if (tok == EOFX) /* special case end of file */
  31. fprintf(stderr, "unexpected end of file\n");
  32. else {
  33. fprintf(stderr, "Line %d # ", line);
  34. if (Col(lval))
  35. fprintf(stderr, "\"%s\": ", mapterm(tok,lval));
  36. for (p = errtab; p->e_state != state && p->e_state >= 0; p++) ;
  37. fprintf(stderr, "%s\n", p->e_mesg);
  38. }
  39. tfatals++;
  40. nocode++;
  41. }
  42. /*
  43. * mapterm finds a printable string for the given token type
  44. * and value.
  45. */
  46. static char *mapterm(typ,val)
  47. int typ;
  48. nodeptr val;
  49. {
  50. register struct toktab *t;
  51. register struct optab *ot;
  52. register int i;
  53. i = typ;
  54. if (i == IDENT || i == INTLIT || i == REALLIT || i == STRINGLIT ||
  55. i == CSETLIT)
  56. return Str0(val);
  57. for (t = toktab; t->t_type != 0; t++)
  58. if (t->t_type == i)
  59. return t->t_word;
  60. for (ot = optab; ot->tok.t_type != 0; ot++)
  61. if (ot->tok.t_type == i)
  62. return ot->tok.t_word;
  63. return "???";
  64. }
  65. /*
  66. * tfatal produces the translator error messages s1 and s2 (if nonnull). The
  67. * location of the error is found in tok_loc.
  68. */
  69. void tfatal(s1, s2)
  70. char *s1, *s2;
  71. {
  72. if (tok_loc.n_file)
  73. fprintf(stderr, "File %s; ", tok_loc.n_file);
  74. fprintf(stderr, "Line %d # ", tok_loc.n_line);
  75. if (s2)
  76. fprintf(stderr, "\"%s\": ", s2);
  77. fprintf(stderr, "%s\n", s1);
  78. tfatals++;
  79. nocode++;
  80. }
  81. /*
  82. * nfatal produces the error messages s1 and s2 (if nonnull), and associates
  83. * it with source location of node.
  84. */
  85. void nfatal(n, s1, s2)
  86. nodeptr n;
  87. char *s1, *s2;
  88. {
  89. if (n != NULL) {
  90. fprintf(stderr, "File %s; ", File(n));
  91. fprintf(stderr, "Line %d # ", Line(n));
  92. }
  93. if (s2)
  94. fprintf(stderr, "\"%s\": ", s2);
  95. fprintf(stderr, "%s\n", s1);
  96. tfatals++;
  97. nocode++;
  98. }
  99. /*
  100. * tsyserr is called for fatal errors. The message s is produced and the
  101. * translator exits.
  102. */
  103. void tsyserr(s)
  104. char *s;
  105. {
  106. if (tok_loc.n_file)
  107. fprintf(stderr, "File %s; ", tok_loc.n_file);
  108. fprintf(stderr, "Line %d # %s\n", in_line, s);
  109. exit(EXIT_FAILURE);
  110. }
  111. /*
  112. * quit - immediate exit with error message
  113. */
  114. void quit(msg)
  115. char *msg;
  116. {
  117. quitf(msg,"");
  118. }
  119. /*
  120. * quitf - immediate exit with message format and argument
  121. */
  122. void quitf(msg,arg)
  123. char *msg, *arg;
  124. {
  125. extern char *progname;
  126. extern char *ofile;
  127. fprintf(stderr,"%s: ",progname);
  128. fprintf(stderr,msg,arg);
  129. fprintf(stderr,"\n");
  130. if (ofile)
  131. remove(ofile); /* remove bad icode file */
  132. exit(EXIT_FAILURE);
  133. }