pbase.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. type
  33. tblock_type = (bt_general,bt_type,bt_const);
  34. var
  35. { contains the current token to be processes }
  36. token : ttoken;
  37. { size of data segment, set by proc_unit or proc_program }
  38. datasize : longint;
  39. { for operators }
  40. optoken : ttoken;
  41. opsym : pvarsym;
  42. { symtable were unit references are stored }
  43. refsymtable : psymtable;
  44. { true, if only routine headers should be }
  45. { parsed }
  46. parse_only : boolean;
  47. { true, if we are in a except block }
  48. in_except_block : boolean;
  49. { type of currently parsed block }
  50. { isn't full implemented (FK) }
  51. block_type : tblock_type;
  52. { true, if we should ignore an equal in const x : 1..2=2 }
  53. ignore_equal : boolean;
  54. { consumes token i, if the current token is unequal i }
  55. { a syntax error is written }
  56. procedure consume(i : ttoken);
  57. { consumes all tokens til atoken (for error recovering }
  58. procedure consume_all_until(atoken : ttoken);
  59. { consumes tokens while they are semicolons }
  60. procedure emptystats;
  61. { reads a list of identifiers into a string container }
  62. function idlist : pstringcontainer;
  63. { inserts the symbols of sc in st with def as definition }
  64. { sc is disposed }
  65. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  66. { just for an accurate position of the end of a procedure (PM) }
  67. var
  68. last_endtoken_filepos: tfileposinfo;
  69. implementation
  70. uses
  71. files,scanner,systems,verbose;
  72. { consumes token i, if the current token is unequal i }
  73. { a syntax error is written }
  74. procedure consume(i : ttoken);
  75. { generates a syntax error message }
  76. procedure syntaxerror(const s : string);
  77. begin
  78. Message2(scan_f_syn_expected,tostr(get_current_col),s);
  79. end;
  80. { This is changed since I changed the order of token
  81. in cobjects.pas for operator overloading !!!! }
  82. { ttoken = (PLUS,MINUS,STAR,SLASH,EQUAL,GT,
  83. LT,LTE,GTE,SYMDIF,STARSTAR,ASSIGNMENT,CARET,
  84. LECKKLAMMER,RECKKLAMMER,
  85. POINT,COMMA,LKLAMMER,RKLAMMER,COLON,SEMICOLON,
  86. KLAMMERAFFE,UNEQUAL,POINTPOINT,
  87. ID,REALNUMBER,_EOF,INTCONST,CSTRING,CCHAR,DOUBLEADDR,}
  88. const tokens : array[PLUS..DOUBLEADDR] of string[12] = (
  89. '+','-','*','/','=','>','<','>=','<=','is','as','in',
  90. '><','**',':=','^','<>','[',']','.',',','(',')',':',';',
  91. '@','..',
  92. 'identifier','const real.','end of file',
  93. 'ord const','const string','const char','@@');
  94. var
  95. j : integer;
  96. begin
  97. if token<>i then
  98. begin
  99. if i<_AND then
  100. syntaxerror(tokens[i])
  101. else
  102. begin
  103. { um die ProgrammgrӇe klein zu halten, }
  104. { wird f�r ein Schl�sselwort-Token der }
  105. { "Text" in der Schl�sselworttabelle }
  106. { des Scanners nachgeschaut }
  107. for j:=1 to anz_keywords do
  108. if keyword_token[j]=i then
  109. syntaxerror(keyword[j])
  110. end;
  111. end
  112. else
  113. begin
  114. if token=_END then
  115. last_endtoken_filepos:=tokenpos;
  116. token:=yylex;
  117. end;
  118. end;
  119. procedure consume_all_until(atoken : ttoken);
  120. begin
  121. while (token<>atoken) and (token<>_EOF) do
  122. consume(token);
  123. { this will create an error if the token is _EOF }
  124. if token<>atoken then
  125. consume(atoken);
  126. { this error is fatal as we have read the whole file }
  127. Message(scan_f_end_of_file);
  128. end;
  129. procedure emptystats;
  130. begin
  131. while token=SEMICOLON do
  132. consume(SEMICOLON);
  133. end;
  134. { reads a list of identifiers into a string container }
  135. function idlist : pstringcontainer;
  136. var
  137. sc : pstringcontainer;
  138. begin
  139. sc:=new(pstringcontainer,init);
  140. repeat
  141. sc^.insert_with_tokeninfo(pattern,
  142. tokenpos);
  143. consume(ID);
  144. if token=COMMA then consume(COMMA)
  145. else break
  146. until false;
  147. idlist:=sc;
  148. end;
  149. { inserts the symbols of sc in st with def as definition }
  150. { sc is disposed }
  151. procedure insert_syms(st : psymtable;sc : pstringcontainer;def : pdef);
  152. var
  153. s : string;
  154. filepos : tfileposinfo;
  155. ss : pvarsym;
  156. begin
  157. s:=sc^.get_with_tokeninfo(filepos);
  158. while s<>'' do
  159. begin
  160. ss:=new(pvarsym,init(s,def));
  161. ss^.line_no:=filepos.line;
  162. st^.insert(ss);
  163. { static data fields are inserted in the globalsymtable }
  164. if (st^.symtabletype=objectsymtable) and
  165. ((current_object_option and sp_static)<>0) then
  166. begin
  167. s:=lower(st^.name^)+'_'+s;
  168. st^.defowner^.owner^.insert(new(pvarsym,init(s,def)));
  169. end;
  170. s:=sc^.get_with_tokeninfo(filepos);
  171. end;
  172. dispose(sc,done);
  173. end;
  174. end.
  175. {
  176. $Log$
  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. }