PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/error.c

https://github.com/ChrisDodd/btyacc
C | 243 lines | 182 code | 54 blank | 7 comment | 10 complexity | be3a26b311740bbec7c71d530f781416 MD5 | raw file
  1. /*
  2. * routines for printing error messages
  3. */
  4. #include "defs.h"
  5. #include <stdarg.h>
  6. extern FILE *inc_file;
  7. extern char inc_file_name[];
  8. void FileError(char *fmt, ...);
  9. /*
  10. * VM: print error message with file coordinates.
  11. * Do it in style acceptable to emacs.
  12. */
  13. void FileError(char *fmt, ...) {
  14. va_list args;
  15. fprintf(stderr, "%s:%d: ", input_file->name, input_file->lineno);
  16. va_start(args, fmt);
  17. vfprintf(stderr, fmt, args);
  18. va_end(args);
  19. fprintf(stderr, "\n");
  20. }
  21. void fatal(char *msg)
  22. {
  23. fprintf(stderr, "fatal - %s\n", msg);
  24. done(2);
  25. }
  26. void no_space()
  27. {
  28. fprintf(stderr, "fatal - out of space\n");
  29. done(2);
  30. }
  31. void open_error(char *filename)
  32. {
  33. fprintf(stderr, "fatal - cannot open \"%s\"\n", filename);
  34. done(2);
  35. }
  36. void unexpected_EOF()
  37. {
  38. FileError("unexpected end-of-file");
  39. done(1);
  40. }
  41. void print_pos(char *st_line, char *st_cptr)
  42. {
  43. register char *s;
  44. if (st_line == 0) return;
  45. for (s = st_line; *s != '\n'; ++s)
  46. {
  47. if (isprint(*s) || *s == '\t')
  48. putc(*s, stderr);
  49. else
  50. putc('?', stderr);
  51. }
  52. putc('\n', stderr);
  53. for (s = st_line; s < st_cptr; ++s)
  54. {
  55. if (*s == '\t')
  56. putc('\t', stderr);
  57. else
  58. putc(' ', stderr);
  59. }
  60. putc('^', stderr);
  61. putc('\n', stderr);
  62. }
  63. int read_errs = 0;
  64. void error(int lineno, char *line, char *cptr, char *msg, ...)
  65. {
  66. char sbuf[512];
  67. va_list args;
  68. va_start(args, msg);
  69. vsprintf(sbuf, msg, args);
  70. va_end(args);
  71. FileError("%s", sbuf);
  72. read_errs++;
  73. }
  74. void syntax_error(int lineno, char *line, char *cptr) {
  75. error(lineno, line, cptr, "syntax error");
  76. exit(1);
  77. }
  78. void unterminated_comment(int lineno, char *line, char *cptr) {
  79. error(lineno, line, cptr, "unmatched /*");
  80. exit(1);
  81. }
  82. void unterminated_string(int lineno, char *line, char *cptr) {
  83. error(lineno, line, cptr, "unterminated string");
  84. exit(1);
  85. }
  86. void unterminated_text(int lineno, char *line, char *cptr) {
  87. error(lineno, line, cptr, "unmatched %%{");
  88. exit(1);
  89. }
  90. void unterminated_union(int lineno, char *line, char *cptr) {
  91. error(lineno, line, cptr, "unterminated %%union");
  92. exit(1);
  93. }
  94. void over_unionized(char *cptr) {
  95. error(input_file->lineno, line, cptr, "too many %%union declarations");
  96. exit(1);
  97. }
  98. void repeat_location_defined(char *cptr) {
  99. error(input_file->lineno, line, cptr, "too many %%location declarations");
  100. exit(1);
  101. }
  102. void illegal_tag(int lineno, char *line, char *cptr) {
  103. error(lineno, line, cptr, "illegal tag");
  104. }
  105. void illegal_character(char *cptr) {
  106. error(input_file->lineno, line, cptr, "illegal character");
  107. }
  108. void used_reserved(char *s) {
  109. error(input_file->lineno, 0, 0, "illegal use of reserved symbol %s", s);
  110. }
  111. void tokenized_start(char *s) {
  112. error(input_file->lineno, 0, 0, "the start symbol %s cannot be declared to be a token", s);
  113. }
  114. void retyped_warning(char *s) {
  115. FileError("the type of %s has been redeclared", s);
  116. }
  117. void reprec_warning(char *s) {
  118. FileError("the precedence of %s has been redeclared", s);
  119. }
  120. void revalued_warning(char *s) {
  121. FileError("the value of %s has been redeclared", s);
  122. }
  123. void terminal_start(char *s) {
  124. error(input_file->lineno, 0, 0, "the start symbol %s is a token", s);
  125. }
  126. void restarted_warning() {
  127. FileError("the start symbol has been redeclared");
  128. }
  129. void no_grammar() {
  130. error(input_file->lineno, 0, 0, "no grammar has been specified");
  131. }
  132. void terminal_lhs(int lineno) {
  133. error(lineno, 0, 0, "a token appears on the lhs of a production");
  134. }
  135. void prec_redeclared() {
  136. error(input_file->lineno, 0, 0, "conflicting %%prec specifiers");
  137. }
  138. void unterminated_action(int lineno, char *line, char *cptr) {
  139. error(lineno, line, cptr, "unterminated action");
  140. }
  141. void unterminated_arglist(int lineno, char *line, char *cptr) {
  142. error(lineno, line, cptr, "unterminated argument list");
  143. }
  144. void bad_formals() {
  145. error(input_file->lineno, 0, 0, "bad formal argument list");
  146. }
  147. void dollar_warning(int a_lineno, int i) {
  148. int slineno = input_file->lineno;
  149. input_file->lineno = a_lineno;
  150. FileError("$%d references beyond the end of the current rule", i);
  151. input_file->lineno = slineno;
  152. }
  153. void dollar_error(int lineno, char *line, char *cptr) {
  154. error(lineno, line, cptr, "illegal $-name");
  155. }
  156. void at_warning(int a_lineno, int i) {
  157. int slineno = input_file->lineno;
  158. input_file->lineno = a_lineno;
  159. FileError("@%d references beyond the end of the current rule", i);
  160. input_file->lineno = slineno;
  161. }
  162. void at_error(int lineno, char *line, char *cptr) {
  163. error(lineno, line, cptr, "illegal @-name");
  164. }
  165. void untyped_lhs() {
  166. error(input_file->lineno, 0, 0, "$$ is untyped");
  167. }
  168. void untyped_rhs(int i, char *s) {
  169. error(input_file->lineno, 0, 0, "$%d (%s) is untyped", i, s);
  170. }
  171. void unknown_rhs(int i) {
  172. error(input_file->lineno, 0, 0, "$%d is untyped (out of range)", i);
  173. }
  174. void default_action_warning(char *s) {
  175. FileError("the default action for %s assigns an undefined value to $$", s);
  176. }
  177. void inconsistent_trial_action_warning(char *s) {
  178. FileError("inconsistent trial actions for %s", s);
  179. }
  180. void undefined_goal(char *s) {
  181. error(input_file->lineno, 0, 0, "the start symbol %s is undefined", s);
  182. }
  183. void undefined_symbol_warning(char *s) {
  184. fprintf(stderr, "warning - the symbol %s is undefined\n", s);
  185. }
  186. void unused_destructor_warning(int lineno)
  187. {
  188. error(lineno, 0, 0, "unused %%destructor");
  189. }