pbase.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. const
  23. { forward types should only be possible inside }
  24. { a TYPE statement, this crashed the compiler }
  25. { when trying to dispose local symbols }
  26. typecanbeforward : boolean = false;
  27. { true, if we are after an assignement }
  28. afterassignment : boolean = false;
  29. { sspecial for handling procedure vars }
  30. getprocvar : boolean = false;
  31. getprocvardef : pprocvardef = nil;
  32. var
  33. { size of data segment, set by proc_unit or proc_program }
  34. datasize : longint;
  35. { for operators }
  36. optoken : ttoken;
  37. opsym : pvarsym;
  38. { symtable were unit references are stored }
  39. refsymtable : psymtable;
  40. { true, if only routine headers should be parsed }
  41. parse_only : boolean;
  42. { true, if we are in a except block }
  43. in_except_block : boolean;
  44. { true, if we should ignore an equal in const x : 1..2=2 }
  45. ignore_equal : boolean;
  46. function tokenstring(i : ttoken):string;
  47. { consumes token i, if the current token is unequal i }
  48. { a syntax error is written }
  49. procedure consume(i : ttoken);
  50. { consumes all tokens til atoken (for error recovering }
  51. procedure consume_all_until(atoken : ttoken);
  52. { consumes tokens while they are semicolons }
  53. procedure emptystats;
  54. { reads a list of identifiers into a string container }
  55. function idlist : pstringcontainer;
  56. { inserts the symbols of sc in st with def as definition }
  57. { sc is disposed }
  58. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  59. { just for an accurate position of the end of a procedure (PM) }
  60. var
  61. last_endtoken_filepos: tfileposinfo;
  62. implementation
  63. uses
  64. files,scanner,systems,verbose;
  65. function tokenstring(i : ttoken):string;
  66. begin
  67. tokenstring:=tokeninfo[i].str;
  68. end;
  69. { consumes token i, write error if token is different }
  70. procedure consume(i : ttoken);
  71. begin
  72. if (token<>i) and (idtoken<>i) then
  73. if token=ID then
  74. Message2(scan_f_syn_expected,tokeninfo[i].str,'identifier '+pattern)
  75. else
  76. Message2(scan_f_syn_expected,tokeninfo[i].str,tokeninfo[token].str)
  77. else
  78. begin
  79. if token=_END then
  80. last_endtoken_filepos:=tokenpos;
  81. current_scanner^.readtoken;
  82. end;
  83. end;
  84. procedure consume_all_until(atoken : ttoken);
  85. begin
  86. while (token<>atoken) and (idtoken<>atoken) do
  87. begin
  88. Consume(token);
  89. if token=_EOF then
  90. begin
  91. Consume(atoken);
  92. Message(scan_f_end_of_file);
  93. exit;
  94. end;
  95. end;
  96. end;
  97. procedure emptystats;
  98. begin
  99. while token=SEMICOLON do
  100. consume(SEMICOLON);
  101. end;
  102. { reads a list of identifiers into a string container }
  103. function idlist : pstringcontainer;
  104. var
  105. sc : pstringcontainer;
  106. begin
  107. sc:=new(pstringcontainer,init);
  108. repeat
  109. sc^.insert_with_tokeninfo(pattern,
  110. tokenpos);
  111. consume(ID);
  112. if token=COMMA then consume(COMMA)
  113. else break
  114. until false;
  115. idlist:=sc;
  116. end;
  117. { inserts the symbols of sc in st with def as definition }
  118. { sc is disposed }
  119. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  120. var
  121. s : string;
  122. filepos : tfileposinfo;
  123. ss : pvarsym;
  124. begin
  125. filepos:=tokenpos;
  126. while not sc^.empty do
  127. begin
  128. s:=sc^.get_with_tokeninfo(tokenpos);
  129. ss:=new(pvarsym,init(s,def));
  130. st^.insert(ss);
  131. { static data fields are inserted in the globalsymtable }
  132. if (st^.symtabletype=objectsymtable) and
  133. ((current_object_option and sp_static)<>0) then
  134. begin
  135. s:=lower(st^.name^)+'_'+s;
  136. st^.defowner^.owner^.insert(new(pvarsym,init(s,def)));
  137. end;
  138. end;
  139. dispose(sc,done);
  140. tokenpos:=filepos;
  141. end;
  142. end.
  143. {
  144. $Log$
  145. Revision 1.19 1999-04-08 20:59:42 florian
  146. * fixed problem with default properties which are a class
  147. * case bug (from the mailing list with -O2) fixed, the
  148. distance of the case labels can be greater than the positive
  149. range of a longint => it is now a dword for fpc
  150. Revision 1.18 1998/12/11 00:03:29 peter
  151. + globtype,tokens,version unit splitted from globals
  152. Revision 1.17 1998/09/26 17:45:31 peter
  153. + idtoken and only one token table
  154. Revision 1.16 1998/09/23 15:39:08 pierre
  155. * browser bugfixes
  156. was adding a reference when looking for the symbol
  157. if -bSYM_NAME was used
  158. Revision 1.15 1998/09/07 23:10:21 florian
  159. * a lot of stuff fixed regarding rtti and publishing of properties,
  160. basics should now work
  161. Revision 1.14 1998/07/14 21:46:49 peter
  162. * updated messages file
  163. Revision 1.13 1998/07/14 14:46:52 peter
  164. * released NEWINPUT
  165. Revision 1.12 1998/07/09 23:59:59 peter
  166. * fixed ttypesym bug finally
  167. * fileinfo in the symtable and better using for unused vars
  168. Revision 1.11 1998/07/07 11:20:02 peter
  169. + NEWINPUT for a better inputfile and scanner object
  170. Revision 1.10 1998/06/05 14:37:31 pierre
  171. * fixes for inline for operators
  172. * inline procedure more correctly restricted
  173. Revision 1.9 1998/06/03 22:48:58 peter
  174. + wordbool,longbool
  175. * rename bis,von -> high,low
  176. * moved some systemunit loading/creating to psystem.pas
  177. Revision 1.8 1998/05/23 01:21:18 peter
  178. + aktasmmode, aktoptprocessor, aktoutputformat
  179. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  180. + $LIBNAME to set the library name where the unit will be put in
  181. * splitted cgi386 a bit (codeseg to large for bp7)
  182. * nasm, tasm works again. nasm moved to ag386nsm.pas
  183. Revision 1.7 1998/05/20 09:42:35 pierre
  184. + UseTokenInfo now default
  185. * unit in interface uses and implementation uses gives error now
  186. * only one error for unknown symbol (uses lastsymknown boolean)
  187. the problem came from the label code !
  188. + first inlined procedures and function work
  189. (warning there might be allowed cases were the result is still wrong !!)
  190. * UseBrower updated gives a global list of all position of all used symbols
  191. with switch -gb
  192. Revision 1.6 1998/05/12 10:47:00 peter
  193. * moved printstatus to verb_def
  194. + V_Normal which is between V_Error and V_Warning and doesn't have a
  195. prefix like error: warning: and is included in V_Default
  196. * fixed some messages
  197. * first time parameter scan is only for -v and -T
  198. - removed old style messages
  199. Revision 1.5 1998/05/06 08:38:44 pierre
  200. * better position info with UseTokenInfo
  201. UseTokenInfo greatly simplified
  202. + added check for changed tree after first time firstpass
  203. (if we could remove all the cases were it happen
  204. we could skip all firstpass if firstpasscount > 1)
  205. Only with ExtDebug
  206. Revision 1.4 1998/04/30 15:59:41 pierre
  207. * GDB works again better :
  208. correct type info in one pass
  209. + UseTokenInfo for better source position
  210. * fixed one remaining bug in scanner for line counts
  211. * several little fixes
  212. Revision 1.3 1998/04/29 10:33:57 pierre
  213. + added some code for ansistring (not complete nor working yet)
  214. * corrected operator overloading
  215. * corrected nasm output
  216. + started inline procedures
  217. + added starstarn : use ** for exponentiation (^ gave problems)
  218. + started UseTokenInfo cond to get accurate positions
  219. Revision 1.2 1998/04/07 22:45:05 florian
  220. * bug0092, bug0115 and bug0121 fixed
  221. + packed object/class/array
  222. }