pbase.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,tokens,globals,symtable
  23. {$ifdef fixLeaksOnError}
  24. ,comphook
  25. {$endif fixLeaksOnError}
  26. {$IFDEF NEWST}
  27. ,symbols,defs
  28. {$ENDIF NEWST}
  29. ;
  30. const
  31. { true, if we are after an assignement }
  32. afterassignment : boolean = false;
  33. { sspecial for handling procedure vars }
  34. getprocvar : boolean = false;
  35. getprocvardef : pprocvardef = nil;
  36. var
  37. { size of data segment, set by proc_unit or proc_program }
  38. datasize : longint;
  39. { for operators }
  40. optoken : ttoken;
  41. opsym : pvarsym;
  42. { symtable were unit references are stored }
  43. refsymtable : psymtable;
  44. { true, if only routine headers should be parsed }
  45. parse_only : boolean;
  46. { true, if we should ignore an equal in const x : 1..2=2 }
  47. ignore_equal : boolean;
  48. {$ifdef fixLeaksOnError}
  49. { not worth it to make a pstack, there's only one data field (a pointer). }
  50. { in the interface, because pmodules and psub also use it for their names }
  51. var strContStack: TStack;
  52. pbase_old_do_stop: tstopprocedure;
  53. {$endif fixLeaksOnError}
  54. function tokenstring(i : ttoken):string;
  55. { consumes token i, if the current token is unequal i }
  56. { a syntax error is written }
  57. procedure consume(i : ttoken);
  58. {Tries to consume the token i, and returns true if it was consumed:
  59. if token=i.}
  60. function try_to_consume(i:Ttoken):boolean;
  61. { consumes all tokens til atoken (for error recovering }
  62. procedure consume_all_until(atoken : ttoken);
  63. { consumes tokens while they are semicolons }
  64. procedure emptystats;
  65. { reads a list of identifiers into a string container }
  66. function idlist : pstringcontainer;
  67. { just for an accurate position of the end of a procedure (PM) }
  68. var
  69. last_endtoken_filepos: tfileposinfo;
  70. implementation
  71. uses
  72. scanner,systems,verbose;
  73. function tokenstring(i : ttoken):string;
  74. begin
  75. tokenstring:=tokeninfo^[i].str;
  76. end;
  77. { consumes token i, write error if token is different }
  78. procedure consume(i : ttoken);
  79. begin
  80. if (token<>i) and (idtoken<>i) then
  81. if token=_id then
  82. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  83. else
  84. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  85. else
  86. begin
  87. if token=_END then
  88. last_endtoken_filepos:=tokenpos;
  89. current_scanner^.readtoken;
  90. end;
  91. end;
  92. function try_to_consume(i:Ttoken):boolean;
  93. begin
  94. try_to_consume:=false;
  95. if (token=i) or (idtoken=i) then
  96. begin
  97. try_to_consume:=true;
  98. if token=_END then
  99. last_endtoken_filepos:=tokenpos;
  100. current_scanner^.readtoken;
  101. end;
  102. end;
  103. procedure consume_all_until(atoken : ttoken);
  104. begin
  105. while (token<>atoken) and (idtoken<>atoken) do
  106. begin
  107. Consume(token);
  108. if token=_EOF then
  109. begin
  110. Consume(atoken);
  111. Message(scan_f_end_of_file);
  112. exit;
  113. end;
  114. end;
  115. end;
  116. procedure emptystats;
  117. begin
  118. repeat
  119. until not try_to_consume(_SEMICOLON);
  120. end;
  121. { reads a list of identifiers into a string container }
  122. function idlist : pstringcontainer;
  123. var
  124. sc : pstringcontainer;
  125. begin
  126. sc:=new(pstringcontainer,init);
  127. repeat
  128. sc^.insert_with_tokeninfo(orgpattern,tokenpos);
  129. consume(_ID);
  130. until not try_to_consume(_COMMA);
  131. idlist:=sc;
  132. end;
  133. {$ifdef fixLeaksOnError}
  134. procedure pbase_do_stop;
  135. var names: PStringContainer;
  136. begin
  137. names := PStringContainer(strContStack.pop);
  138. while names <> nil do
  139. begin
  140. dispose(names,done);
  141. names := PStringContainer(strContStack.pop);
  142. end;
  143. strContStack.done;
  144. do_stop := pbase_old_do_stop;
  145. do_stop{$ifdef FPCPROCVAR}(){$endif};
  146. end;
  147. begin
  148. strContStack.init;
  149. pbase_old_do_stop := do_stop;
  150. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  151. {$endif fixLeaksOnError}
  152. end.
  153. {
  154. $Log$
  155. Revision 1.5 2000-09-24 15:06:21 peter
  156. * use defines.inc
  157. Revision 1.4 2000/08/27 20:19:39 peter
  158. * store strings with case in ppu, when an internal symbol is created
  159. a '$' is prefixed so it's not automatic uppercased
  160. Revision 1.3 2000/08/27 16:11:51 peter
  161. * moved some util functions from globals,cobjects to cutils
  162. * splitted files into finput,fmodule
  163. Revision 1.2 2000/07/13 11:32:44 michael
  164. + removed logs
  165. }