pbase.pas 5.6 KB

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