pbase.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 should ignore an equal in const x : 1..2=2 }
  43. ignore_equal : boolean;
  44. function tokenstring(i : ttoken):string;
  45. { consumes token i, if the current token is unequal i }
  46. { a syntax error is written }
  47. procedure consume(i : ttoken);
  48. {Tries to consume the token i, and returns true if it was consumed:
  49. if token=i.}
  50. function try_to_consume(i:Ttoken):boolean;
  51. { consumes all tokens til atoken (for error recovering }
  52. procedure consume_all_until(atoken : ttoken);
  53. { consumes tokens while they are semicolons }
  54. procedure emptystats;
  55. { reads a list of identifiers into a string container }
  56. function idlist : pstringcontainer;
  57. { just for an accurate position of the end of a procedure (PM) }
  58. var
  59. last_endtoken_filepos: tfileposinfo;
  60. implementation
  61. uses
  62. files,scanner,systems,verbose;
  63. function tokenstring(i : ttoken):string;
  64. begin
  65. tokenstring:=tokeninfo^[i].str;
  66. end;
  67. { consumes token i, write error if token is different }
  68. procedure consume(i : ttoken);
  69. begin
  70. if (token<>i) and (idtoken<>i) then
  71. if token=_id then
  72. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  73. else
  74. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  75. else
  76. begin
  77. if token=_END then
  78. last_endtoken_filepos:=tokenpos;
  79. current_scanner^.readtoken;
  80. end;
  81. end;
  82. function try_to_consume(i:Ttoken):boolean;
  83. begin
  84. try_to_consume:=false;
  85. if (token=i) or (idtoken=i) then
  86. begin
  87. try_to_consume:=true;
  88. if token=_END then
  89. last_endtoken_filepos:=tokenpos;
  90. current_scanner^.readtoken;
  91. end;
  92. end;
  93. procedure consume_all_until(atoken : ttoken);
  94. begin
  95. while (token<>atoken) and (idtoken<>atoken) do
  96. begin
  97. Consume(token);
  98. if token=_EOF then
  99. begin
  100. Consume(atoken);
  101. Message(scan_f_end_of_file);
  102. exit;
  103. end;
  104. end;
  105. end;
  106. procedure emptystats;
  107. begin
  108. repeat
  109. until not try_to_consume(_SEMICOLON);
  110. end;
  111. { reads a list of identifiers into a string container }
  112. function idlist : pstringcontainer;
  113. var
  114. sc : pstringcontainer;
  115. begin
  116. sc:=new(pstringcontainer,init);
  117. repeat
  118. sc^.insert_with_tokeninfo(pattern,
  119. tokenpos);
  120. consume(_id);
  121. if token=_COMMA then consume(_COMMA)
  122. else break
  123. until false;
  124. idlist:=sc;
  125. end;
  126. end.
  127. {
  128. $Log$
  129. Revision 1.25 1999-09-02 18:47:44 daniel
  130. * Could not compile with TP, some arrays moved to heap
  131. * NOAG386BIN default for TP
  132. * AG386* files were not compatible with TP, fixed.
  133. Revision 1.24 1999/08/04 13:02:50 jonas
  134. * all tokens now start with an underscore
  135. * PowerPC compiles!!
  136. Revision 1.23 1999/07/27 23:42:10 peter
  137. * indirect type referencing is now allowed
  138. Revision 1.22 1999/07/26 09:42:10 florian
  139. * bugs 494-496 fixed
  140. Revision 1.21 1999/04/28 06:02:05 florian
  141. * changes of Bruessel:
  142. + message handler can now take an explicit self
  143. * typinfo fixed: sometimes the type names weren't written
  144. * the type checking for pointer comparisations and subtraction
  145. and are now more strict (was also buggy)
  146. * small bug fix to link.pas to support compiling on another
  147. drive
  148. * probable bug in popt386 fixed: call/jmp => push/jmp
  149. transformation didn't count correctly the jmp references
  150. + threadvar support
  151. * warning if ln/sqrt gets an invalid constant argument
  152. Revision 1.20 1999/04/14 18:41:24 daniel
  153. * Better use of routines in pbase and symtable. 4k code removed.
  154. Revision 1.19 1999/04/08 20:59:42 florian
  155. * fixed problem with default properties which are a class
  156. * case bug (from the mailing list with -O2) fixed, the
  157. distance of the case labels can be greater than the positive
  158. range of a longint => it is now a dword for fpc
  159. Revision 1.18 1998/12/11 00:03:29 peter
  160. + globtype,tokens,version unit splitted from globals
  161. Revision 1.17 1998/09/26 17:45:31 peter
  162. + idtoken and only one token table
  163. Revision 1.16 1998/09/23 15:39:08 pierre
  164. * browser bugfixes
  165. was adding a reference when looking for the symbol
  166. if -bSYM_NAME was used
  167. Revision 1.15 1998/09/07 23:10:21 florian
  168. * a lot of stuff fixed regarding rtti and publishing of properties,
  169. basics should now work
  170. Revision 1.14 1998/07/14 21:46:49 peter
  171. * updated messages file
  172. Revision 1.13 1998/07/14 14:46:52 peter
  173. * released NEWINPUT
  174. Revision 1.12 1998/07/09 23:59:59 peter
  175. * fixed ttypesym bug finally
  176. * fileinfo in the symtable and better using for unused vars
  177. Revision 1.11 1998/07/07 11:20:02 peter
  178. + NEWINPUT for a better inputfile and scanner object
  179. Revision 1.10 1998/06/05 14:37:31 pierre
  180. * fixes for inline for operators
  181. * inline procedure more correctly restricted
  182. Revision 1.9 1998/06/03 22:48:58 peter
  183. + wordbool,longbool
  184. * rename bis,von -> high,low
  185. * moved some systemunit loading/creating to psystem.pas
  186. Revision 1.8 1998/05/23 01:21:18 peter
  187. + aktasmmode, aktoptprocessor, aktoutputformat
  188. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  189. + $LIBNAME to set the library name where the unit will be put in
  190. * splitted cgi386 a bit (codeseg to large for bp7)
  191. * nasm, tasm works again. nasm moved to ag386nsm.pas
  192. Revision 1.7 1998/05/20 09:42:35 pierre
  193. + UseTokenInfo now default
  194. * unit in interface uses and implementation uses gives error now
  195. * only one error for unknown symbol (uses lastsymknown boolean)
  196. the problem came from the label code !
  197. + first inlined procedures and function work
  198. (warning there might be allowed cases were the result is still wrong !!)
  199. * UseBrower updated gives a global list of all position of all used symbols
  200. with switch -gb
  201. Revision 1.6 1998/05/12 10:47:00 peter
  202. * moved printstatus to verb_def
  203. + V_Normal which is between V_Error and V_Warning and doesn't have a
  204. prefix like error: warning: and is included in V_Default
  205. * fixed some messages
  206. * first time parameter scan is only for -v and -T
  207. - removed old style messages
  208. Revision 1.5 1998/05/06 08:38:44 pierre
  209. * better position info with UseTokenInfo
  210. UseTokenInfo greatly simplified
  211. + added check for changed tree after first time firstpass
  212. (if we could remove all the cases were it happen
  213. we could skip all firstpass if firstpasscount > 1)
  214. Only with ExtDebug
  215. Revision 1.4 1998/04/30 15:59:41 pierre
  216. * GDB works again better :
  217. correct type info in one pass
  218. + UseTokenInfo for better source position
  219. * fixed one remaining bug in scanner for line counts
  220. * several little fixes
  221. Revision 1.3 1998/04/29 10:33:57 pierre
  222. + added some code for ansistring (not complete nor working yet)
  223. * corrected operator overloading
  224. * corrected nasm output
  225. + started inline procedures
  226. + added starstarn : use ** for exponentiation (^ gave problems)
  227. + started UseTokenInfo cond to get accurate positions
  228. Revision 1.2 1998/04/07 22:45:05 florian
  229. * bug0092, bug0115 and bug0121 fixed
  230. + packed object/class/array
  231. }