yaccmsgs.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {
  2. TP Yacc message and error handling module 2-5-91 AG
  3. Note: this module should be USEd by any module using the heap during
  4. initialization, since it installs a heap error handler (which
  5. terminates the program with fatal error `memory overflow').
  6. Copyright (c) 1990-92 Albert Graef <[email protected]>
  7. Copyright (C) 1996 Berend de Boer <[email protected]>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. $Revision: 1.3 $
  20. $Modtime: 96-07-31 14:50 $
  21. $History: YACCMSGS.PAS $
  22. *
  23. * ***************** Version 2 *****************
  24. * User: Berend Date: 96-10-10 Time: 21:16
  25. * Updated in $/Lex and Yacc/tply
  26. * Updated for protected mode, windows and Delphi 1.X and 2.X.
  27. }
  28. unit YaccMsgs;
  29. interface
  30. var errors, warnings : Integer;
  31. (* - current error and warning count *)
  32. procedure error(msg : String);
  33. (* - print current input line and error message (pos denotes position to
  34. mark in source file line) *)
  35. procedure warning(msg : String);
  36. (* - print warning message *)
  37. procedure fatal(msg : String);
  38. (* - writes a fatal error message, erases Yacc output file and terminates
  39. the program with errorlevel 1 *)
  40. const
  41. (* sign-on and usage message: *)
  42. sign_on = 'TP Yacc Version 4.1a [April 2000], Copyright (c) 1990-2000 Albert Graef';
  43. {$ifdef Unix}
  44. usage = 'Usage: pyacc [options] yacc-file[.y] [output-file[.pas]]';
  45. {$else}
  46. usage = 'Usage: yacc [options] yacc-file[.y] [output-file[.pas]]';
  47. {$endif}
  48. options = 'Options: -v verbose, -d debug';
  49. (* command line error messages: *)
  50. invalid_option = 'invalid option ';
  51. illegal_no_args = 'illegal number of parameters';
  52. (* syntax errors: *)
  53. open_comment_at_eof = '101: open comment at end of file';
  54. missing_string_terminator = '102: missing string terminator';
  55. rcurl_expected = '103: %} expected';
  56. rbrace_expected = '104: } expected';
  57. rangle_expected = '105: > expected';
  58. ident_expected = '106: identifier expected';
  59. error_in_def = '110: error in definition';
  60. error_in_rule = '111: error in rule';
  61. syntax_error = '112: syntax error';
  62. unexpected_eof = '113: unexpected end of file';
  63. (* semantic errors: *)
  64. nonterm_expected = '201: nonterminal expected';
  65. literal_expected = '202: literal expected';
  66. double_tokennum_def = '203: literal already defined';
  67. unknown_identifier = '204: unknown identifier';
  68. type_error = '205: type error';
  69. range_error = '206: range error';
  70. empty_grammar = '207: empty grammar?';
  71. (* fatal errors: *)
  72. cannot_open_file = 'FATAL: cannot open file ';
  73. write_error = 'FATAL: write error';
  74. mem_overflow = 'FATAL: memory overflow';
  75. intset_overflow = 'FATAL: integer set overflow';
  76. sym_table_overflow = 'FATAL: symbol table overflow';
  77. nt_table_overflow = 'FATAL: nonterminal table overflow';
  78. lit_table_overflow = 'FATAL: literal table overflow';
  79. type_table_overflow = 'FATAL: type table overflow';
  80. prec_table_overflow = 'FATAL: precedence table overflow';
  81. rule_table_overflow = 'FATAL: rule table overflow';
  82. state_table_overflow = 'FATAL: state table overflow';
  83. item_table_overflow = 'FATAL: item table overflow';
  84. trans_table_overflow = 'FATAL: transition table overflow';
  85. redn_table_overflow = 'FATAL: reduction table overflow';
  86. implementation
  87. uses YaccBase;
  88. procedure position(var f : Text;
  89. lineNo : integer;
  90. line : String;
  91. pos : integer);
  92. (* writes a position mark of the form
  93. lineno: line
  94. ^
  95. on f with the caret ^ positioned at pos in line
  96. a subsequent write starts at the next line, indented with tab *)
  97. var
  98. line1, line2 : String;
  99. begin
  100. (* this hack handles tab characters in line: *)
  101. line1 := intStr(lineNo)+': '+line;
  102. line2 := blankStr(intStr(lineNo)+': '+copy(line, 1, pos-1));
  103. writeln(f, line1);
  104. writeln(f, line2, '^');
  105. write(f, tab)
  106. end(*position*);
  107. procedure error(msg : String);
  108. begin
  109. inc(errors);
  110. writeln;
  111. position(output, lno, line, cno-tokleng);
  112. writeln(msg);
  113. writeln(yylst);
  114. position(yylst, lno, line, cno-tokleng);
  115. writeln(yylst, msg);
  116. if ioresult<>0 then ;
  117. end(*error*);
  118. procedure warning(msg : String);
  119. begin
  120. inc(warnings);
  121. writeln;
  122. position(output, lno, line, cno-tokleng);
  123. writeln(msg);
  124. writeln(yylst);
  125. position(yylst, lno, line, cno-tokleng);
  126. writeln(yylst, msg);
  127. if ioresult<>0 then ;
  128. end(*warning*);
  129. procedure fatal(msg : String);
  130. begin
  131. writeln;
  132. writeln(msg);
  133. close(yyin); close(yyout); close(yylst); erase(yyout);
  134. halt(1)
  135. end(*fatal*);
  136. {$ifndef fpc}
  137. {$IFNDEF Win32}
  138. function heapErrorHandler ( size : Word ) : Integer; far;
  139. begin
  140. if size>0 then
  141. fatal(mem_overflow) (* never returns *)
  142. else
  143. heapErrorHandler := 1
  144. end(*heapErrorHandler*);
  145. {$ENDIF}
  146. {$endif}
  147. begin
  148. errors := 0; warnings := 0;
  149. {$ifndef fpc}
  150. {$IFNDEF Win32}
  151. (* install heap error handler: *)
  152. heapError := @heapErrorHandler;
  153. {$ENDIF}
  154. {$endif}
  155. end(*YaccMsgs*).