pbase.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Contains some helper routines for the parser
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pbase;
  19. interface
  20. uses
  21. cobjects,tokens,globals,symtable
  22. {$ifdef fixLeaksOnError}
  23. ,comphook
  24. {$endif fixLeaksOnError}
  25. {$IFDEF NEWST}
  26. ,symbols,defs
  27. {$ENDIF NEWST}
  28. ;
  29. const
  30. { true, if we are after an assignement }
  31. afterassignment : boolean = false;
  32. { sspecial for handling procedure vars }
  33. getprocvar : boolean = false;
  34. getprocvardef : pprocvardef = nil;
  35. var
  36. { size of data segment, set by proc_unit or proc_program }
  37. datasize : longint;
  38. { for operators }
  39. optoken : ttoken;
  40. opsym : pvarsym;
  41. { symtable were unit references are stored }
  42. refsymtable : psymtable;
  43. { true, if only routine headers should be parsed }
  44. parse_only : boolean;
  45. { true, if we should ignore an equal in const x : 1..2=2 }
  46. ignore_equal : boolean;
  47. {$ifdef fixLeaksOnError}
  48. { not worth it to make a pstack, there's only one data field (a pointer). }
  49. { in the interface, because pmodules and psub also use it for their names }
  50. var strContStack: TStack;
  51. pbase_old_do_stop: tstopprocedure;
  52. {$endif fixLeaksOnError}
  53. function tokenstring(i : ttoken):string;
  54. { consumes token i, if the current token is unequal i }
  55. { a syntax error is written }
  56. procedure consume(i : ttoken);
  57. {Tries to consume the token i, and returns true if it was consumed:
  58. if token=i.}
  59. function try_to_consume(i:Ttoken):boolean;
  60. { consumes all tokens til atoken (for error recovering }
  61. procedure consume_all_until(atoken : ttoken);
  62. { consumes tokens while they are semicolons }
  63. procedure emptystats;
  64. { reads a list of identifiers into a string container }
  65. function idlist : pstringcontainer;
  66. { just for an accurate position of the end of a procedure (PM) }
  67. var
  68. last_endtoken_filepos: tfileposinfo;
  69. implementation
  70. uses
  71. files,scanner,systems,verbose;
  72. function tokenstring(i : ttoken):string;
  73. begin
  74. tokenstring:=tokeninfo^[i].str;
  75. end;
  76. { consumes token i, write error if token is different }
  77. procedure consume(i : ttoken);
  78. begin
  79. if (token<>i) and (idtoken<>i) then
  80. if token=_id then
  81. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  82. else
  83. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  84. else
  85. begin
  86. if token=_END then
  87. last_endtoken_filepos:=tokenpos;
  88. current_scanner^.readtoken;
  89. end;
  90. end;
  91. function try_to_consume(i:Ttoken):boolean;
  92. begin
  93. try_to_consume:=false;
  94. if (token=i) or (idtoken=i) then
  95. begin
  96. try_to_consume:=true;
  97. if token=_END then
  98. last_endtoken_filepos:=tokenpos;
  99. current_scanner^.readtoken;
  100. end;
  101. end;
  102. procedure consume_all_until(atoken : ttoken);
  103. begin
  104. while (token<>atoken) and (idtoken<>atoken) do
  105. begin
  106. Consume(token);
  107. if token=_EOF then
  108. begin
  109. Consume(atoken);
  110. Message(scan_f_end_of_file);
  111. exit;
  112. end;
  113. end;
  114. end;
  115. procedure emptystats;
  116. begin
  117. repeat
  118. until not try_to_consume(_SEMICOLON);
  119. end;
  120. { reads a list of identifiers into a string container }
  121. function idlist : pstringcontainer;
  122. var
  123. sc : pstringcontainer;
  124. begin
  125. sc:=new(pstringcontainer,init);
  126. repeat
  127. sc^.insert_with_tokeninfo(pattern,
  128. tokenpos);
  129. consume(_id);
  130. if token=_COMMA then consume(_COMMA)
  131. else break
  132. until false;
  133. idlist:=sc;
  134. end;
  135. {$ifdef fixLeaksOnError}
  136. procedure pbase_do_stop; {$ifdef tp} far; {$endif tp}
  137. var names: PStringContainer;
  138. begin
  139. names := PStringContainer(strContStack.pop);
  140. while names <> nil do
  141. begin
  142. dispose(names,done);
  143. names := PStringContainer(strContStack.pop);
  144. end;
  145. strContStack.done;
  146. do_stop := pbase_old_do_stop;
  147. {$ifdef tp}
  148. do_stop;
  149. {$else tp}
  150. do_stop();
  151. {$endif tp}
  152. end;
  153. begin
  154. strContStack.init;
  155. pbase_old_do_stop := do_stop;
  156. do_stop := {$ifndef tp}@{$endif}pbase_do_stop;
  157. {$endif fixLeaksOnError}
  158. end.
  159. {
  160. $Log$
  161. Revision 1.31 2000-03-11 21:11:24 daniel
  162. * Ported hcgdata to new symtable.
  163. * Alignment code changed as suggested by Peter
  164. + Usage of my is operator replacement, is_object
  165. Revision 1.30 2000/02/09 13:22:56 peter
  166. * log truncated
  167. Revision 1.29 2000/01/11 17:16:04 jonas
  168. * removed a lot of memory leaks when an error is encountered (caused by
  169. procinfo and pstringcontainers). There are still plenty left though :)
  170. Revision 1.28 2000/01/07 01:14:28 peter
  171. * updated copyright to 2000
  172. Revision 1.27 1999/11/06 14:34:21 peter
  173. * truncated log to 20 revs
  174. Revision 1.26 1999/10/01 08:02:46 peter
  175. * forward type declaration rewritten
  176. Revision 1.25 1999/09/02 18:47:44 daniel
  177. * Could not compile with TP, some arrays moved to heap
  178. * NOAG386BIN default for TP
  179. * AG386* files were not compatible with TP, fixed.
  180. Revision 1.24 1999/08/04 13:02:50 jonas
  181. * all tokens now start with an underscore
  182. * PowerPC compiles!!
  183. Revision 1.23 1999/07/27 23:42:10 peter
  184. * indirect type referencing is now allowed
  185. Revision 1.22 1999/07/26 09:42:10 florian
  186. * bugs 494-496 fixed
  187. }