pbase.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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,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. { contains the current token to be processes }
  34. token : ttoken;
  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 are in a except block }
  45. in_except_block : boolean;
  46. { true, if we should ignore an equal in const x : 1..2=2 }
  47. ignore_equal : boolean;
  48. { consumes token i, if the current token is unequal i }
  49. { a syntax error is written }
  50. procedure consume(i : ttoken);
  51. function tokenstring(i : ttoken) : string;
  52. { consumes all tokens til atoken (for error recovering }
  53. procedure consume_all_until(atoken : ttoken);
  54. { consumes tokens while they are semicolons }
  55. procedure emptystats;
  56. { reads a list of identifiers into a string container }
  57. function idlist : pstringcontainer;
  58. { inserts the symbols of sc in st with def as definition }
  59. { sc is disposed }
  60. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  61. { just for an accurate position of the end of a procedure (PM) }
  62. var
  63. last_endtoken_filepos: tfileposinfo;
  64. implementation
  65. uses
  66. files,scanner,systems,verbose;
  67. { ttoken = (PLUS,MINUS,STAR,SLASH,EQUAL,GT,
  68. LT,LTE,GTE,SYMDIF,STARSTAR,ASSIGNMENT,CARET,
  69. LECKKLAMMER,RECKKLAMMER,
  70. POINT,COMMA,LKLAMMER,RKLAMMER,COLON,SEMICOLON,
  71. KLAMMERAFFE,UNEQUAL,POINTPOINT,
  72. ID,REALNUMBER,_EOF,INTCONST,CSTRING,CCHAR,DOUBLEADDR,}
  73. const tokens : array[PLUS..DOUBLEADDR] of string[12] = (
  74. '+','-','*','/','=','>','<','>=','<=','is','as','in',
  75. '><','**',':=','^','<>','[',']','.',',','(',')',':',';',
  76. '@','..',
  77. 'identifier','const real.','end of file',
  78. 'ord const','const string','const char','@@');
  79. function tokenstring(i : ttoken) : string;
  80. var
  81. j : longint;
  82. begin
  83. if i<_AND then
  84. tokenstring:=tokens[i]
  85. else
  86. begin
  87. for j:=1 to anz_keywords do
  88. if keyword_token[j]=i then
  89. tokenstring:=keyword[j];
  90. end;
  91. end;
  92. { consumes token i, write error if token is different }
  93. procedure consume(i : ttoken);
  94. begin
  95. if token<>i then
  96. Message1(scan_f_syn_expected,tokenstring(i))
  97. else
  98. begin
  99. if token=_END then
  100. last_endtoken_filepos:=tokenpos;
  101. token:=current_scanner^.yylex;
  102. end;
  103. end;
  104. procedure consume_all_until(atoken : ttoken);
  105. begin
  106. while (token<>atoken) and (token<>_EOF) do
  107. consume(token);
  108. { this will create an error if the token is _EOF }
  109. if token<>atoken then
  110. consume(atoken);
  111. { this error is fatal as we have read the whole file }
  112. Message(scan_f_end_of_file);
  113. end;
  114. procedure emptystats;
  115. begin
  116. while token=SEMICOLON do
  117. 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(pattern,
  127. tokenpos);
  128. consume(ID);
  129. if token=COMMA then consume(COMMA)
  130. else break
  131. until false;
  132. idlist:=sc;
  133. end;
  134. { inserts the symbols of sc in st with def as definition }
  135. { sc is disposed }
  136. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  137. var
  138. s : string;
  139. filepos : tfileposinfo;
  140. ss : pvarsym;
  141. begin
  142. while not sc^.empty do
  143. begin
  144. s:=sc^.get_with_tokeninfo(filepos);
  145. ss:=new(pvarsym,init(s,def));
  146. ss^.fileinfo:=filepos;
  147. st^.insert(ss);
  148. { static data fields are inserted in the globalsymtable }
  149. if (st^.symtabletype=objectsymtable) and
  150. ((current_object_option and sp_static)<>0) then
  151. begin
  152. s:=lower(st^.name^)+'_'+s;
  153. st^.defowner^.owner^.insert(new(pvarsym,init(s,def)));
  154. end;
  155. end;
  156. dispose(sc,done);
  157. end;
  158. end.
  159. {
  160. $Log$
  161. Revision 1.15 1998-09-07 23:10:21 florian
  162. * a lot of stuff fixed regarding rtti and publishing of properties,
  163. basics should now work
  164. Revision 1.14 1998/07/14 21:46:49 peter
  165. * updated messages file
  166. Revision 1.13 1998/07/14 14:46:52 peter
  167. * released NEWINPUT
  168. Revision 1.12 1998/07/09 23:59:59 peter
  169. * fixed ttypesym bug finally
  170. * fileinfo in the symtable and better using for unused vars
  171. Revision 1.11 1998/07/07 11:20:02 peter
  172. + NEWINPUT for a better inputfile and scanner object
  173. Revision 1.10 1998/06/05 14:37:31 pierre
  174. * fixes for inline for operators
  175. * inline procedure more correctly restricted
  176. Revision 1.9 1998/06/03 22:48:58 peter
  177. + wordbool,longbool
  178. * rename bis,von -> high,low
  179. * moved some systemunit loading/creating to psystem.pas
  180. Revision 1.8 1998/05/23 01:21:18 peter
  181. + aktasmmode, aktoptprocessor, aktoutputformat
  182. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  183. + $LIBNAME to set the library name where the unit will be put in
  184. * splitted cgi386 a bit (codeseg to large for bp7)
  185. * nasm, tasm works again. nasm moved to ag386nsm.pas
  186. Revision 1.7 1998/05/20 09:42:35 pierre
  187. + UseTokenInfo now default
  188. * unit in interface uses and implementation uses gives error now
  189. * only one error for unknown symbol (uses lastsymknown boolean)
  190. the problem came from the label code !
  191. + first inlined procedures and function work
  192. (warning there might be allowed cases were the result is still wrong !!)
  193. * UseBrower updated gives a global list of all position of all used symbols
  194. with switch -gb
  195. Revision 1.6 1998/05/12 10:47:00 peter
  196. * moved printstatus to verb_def
  197. + V_Normal which is between V_Error and V_Warning and doesn't have a
  198. prefix like error: warning: and is included in V_Default
  199. * fixed some messages
  200. * first time parameter scan is only for -v and -T
  201. - removed old style messages
  202. Revision 1.5 1998/05/06 08:38:44 pierre
  203. * better position info with UseTokenInfo
  204. UseTokenInfo greatly simplified
  205. + added check for changed tree after first time firstpass
  206. (if we could remove all the cases were it happen
  207. we could skip all firstpass if firstpasscount > 1)
  208. Only with ExtDebug
  209. Revision 1.4 1998/04/30 15:59:41 pierre
  210. * GDB works again better :
  211. correct type info in one pass
  212. + UseTokenInfo for better source position
  213. * fixed one remaining bug in scanner for line counts
  214. * several little fixes
  215. Revision 1.3 1998/04/29 10:33:57 pierre
  216. + added some code for ansistring (not complete nor working yet)
  217. * corrected operator overloading
  218. * corrected nasm output
  219. + started inline procedures
  220. + added starstarn : use ** for exponentiation (^ gave problems)
  221. + started UseTokenInfo cond to get accurate positions
  222. Revision 1.2 1998/04/07 22:45:05 florian
  223. * bug0092, bug0115 and bug0121 fixed
  224. + packed object/class/array
  225. }