pbase.pas 9.3 KB

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