pbase.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 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(orgpattern,tokenpos);
  128. consume(_ID);
  129. until not try_to_consume(_COMMA);
  130. idlist:=sc;
  131. end;
  132. {$ifdef fixLeaksOnError}
  133. procedure pbase_do_stop; {$ifdef tp} far; {$endif tp}
  134. var names: PStringContainer;
  135. begin
  136. names := PStringContainer(strContStack.pop);
  137. while names <> nil do
  138. begin
  139. dispose(names,done);
  140. names := PStringContainer(strContStack.pop);
  141. end;
  142. strContStack.done;
  143. do_stop := pbase_old_do_stop;
  144. {$ifdef tp}
  145. do_stop;
  146. {$else tp}
  147. do_stop();
  148. {$endif tp}
  149. end;
  150. begin
  151. strContStack.init;
  152. pbase_old_do_stop := do_stop;
  153. do_stop := {$ifndef tp}@{$endif}pbase_do_stop;
  154. {$endif fixLeaksOnError}
  155. end.
  156. {
  157. $Log$
  158. Revision 1.4 2000-08-27 20:19:39 peter
  159. * store strings with case in ppu, when an internal symbol is created
  160. a '$' is prefixed so it's not automatic uppercased
  161. Revision 1.3 2000/08/27 16:11:51 peter
  162. * moved some util functions from globals,cobjects to cutils
  163. * splitted files into finput,fmodule
  164. Revision 1.2 2000/07/13 11:32:44 michael
  165. + removed logs
  166. }