pbase.pas 7.9 KB

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