pbase.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. tokens,globals,
  24. symconst,symbase,symtype,symdef,symsym,symtable
  25. {$ifdef fixLeaksOnError}
  26. ,comphook
  27. {$endif fixLeaksOnError}
  28. ;
  29. const
  30. { true, if we are after an assignement }
  31. afterassignment : boolean = false;
  32. { sspecial for handling procedure vars }
  33. getprocvar : boolean = false;
  34. getprocvardef : tprocvardef = nil;
  35. type
  36. { listitem }
  37. tidstringlistitem = class(tlinkedlistitem)
  38. data : pstring;
  39. file_info : tfileposinfo;
  40. constructor Create(const s:string;const pos:tfileposinfo);
  41. destructor Destroy;override;
  42. end;
  43. tidstringlist=class(tlinkedlist)
  44. procedure add(const s : string;const file_info : tfileposinfo);
  45. function get(var file_info : tfileposinfo) : string;
  46. function find(const s:string):boolean;
  47. end;
  48. var
  49. { size of data segment, set by proc_unit or proc_program }
  50. datasize : longint;
  51. { for operators }
  52. optoken : ttoken;
  53. otsym : tvarsym;
  54. { symtable were unit references are stored }
  55. refsymtable : tsymtable;
  56. { true, if only routine headers should be parsed }
  57. parse_only : boolean;
  58. { true, if we should ignore an equal in const x : 1..2=2 }
  59. ignore_equal : boolean;
  60. {$ifdef fixLeaksOnError}
  61. { not worth it to make a pstack, there's only one data field (a pointer). }
  62. { in the interface, because pmodules and psub also use it for their names }
  63. var strContStack: TStack;
  64. pbase_old_do_stop: tstopprocedure;
  65. {$endif fixLeaksOnError}
  66. procedure identifier_not_found(const s:string);
  67. function tokenstring(i : ttoken):string;
  68. { consumes token i, if the current token is unequal i }
  69. { a syntax error is written }
  70. procedure consume(i : ttoken);
  71. {Tries to consume the token i, and returns true if it was consumed:
  72. if token=i.}
  73. function try_to_consume(i:Ttoken):boolean;
  74. { consumes all tokens til atoken (for error recovering }
  75. procedure consume_all_until(atoken : ttoken);
  76. { consumes tokens while they are semicolons }
  77. procedure emptystats;
  78. { consume a symbol, if not found give an error and
  79. and return an errorsym }
  80. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  81. { reads a list of identifiers into a string list }
  82. function idlist : tidstringlist;
  83. { just for an accurate position of the end of a procedure (PM) }
  84. var
  85. last_endtoken_filepos: tfileposinfo;
  86. implementation
  87. uses
  88. globtype,scanner,systems,verbose;
  89. {****************************************************************************
  90. TIdStringlistItem
  91. ****************************************************************************}
  92. constructor TIDStringlistItem.Create(const s:string;const pos:tfileposinfo);
  93. begin
  94. data:=stringdup(s);
  95. file_info:=pos;
  96. end;
  97. destructor TIDStringlistItem.Destroy;
  98. begin
  99. stringdispose(data);
  100. end;
  101. {****************************************************************************
  102. TIdStringlist
  103. ****************************************************************************}
  104. procedure tidstringlist.add(const s : string; const file_info : tfileposinfo);
  105. begin
  106. if find(s) then
  107. exit;
  108. inherited concat(tidstringlistitem.create(s,file_info));
  109. end;
  110. function tidstringlist.get(var file_info : tfileposinfo) : string;
  111. var
  112. p : tidstringlistitem;
  113. begin
  114. p:=tidstringlistitem(inherited getfirst);
  115. if p=nil then
  116. begin
  117. get:='';
  118. file_info.fileindex:=0;
  119. file_info.line:=0;
  120. file_info.column:=0;
  121. end
  122. else
  123. begin
  124. get:=p.data^;
  125. file_info:=p.file_info;
  126. p.free;
  127. end;
  128. end;
  129. function tidstringlist.find(const s:string):boolean;
  130. var
  131. newnode : tidstringlistitem;
  132. begin
  133. find:=false;
  134. newnode:=tidstringlistitem(First);
  135. while assigned(newnode) do
  136. begin
  137. if newnode.data^=s then
  138. begin
  139. find:=true;
  140. exit;
  141. end;
  142. newnode:=tidstringlistitem(newnode.next);
  143. end;
  144. end;
  145. {****************************************************************************
  146. Token Parsing
  147. ****************************************************************************}
  148. procedure identifier_not_found(const s:string);
  149. begin
  150. Message1(sym_e_id_not_found,s);
  151. { show a fatal that you need -S2 or -Sd, but only
  152. if we just parsed the a token that has m_class }
  153. if not(m_class in aktmodeswitches) and
  154. (Upper(s)=pattern) and
  155. (tokeninfo^[idtoken].keyword=m_class) then
  156. Message(parser_f_need_objfpc_or_delphi_mode);
  157. end;
  158. function tokenstring(i : ttoken):string;
  159. begin
  160. tokenstring:=tokeninfo^[i].str;
  161. end;
  162. { consumes token i, write error if token is different }
  163. procedure consume(i : ttoken);
  164. begin
  165. if (token<>i) and (idtoken<>i) then
  166. if token=_id then
  167. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  168. else
  169. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  170. else
  171. begin
  172. if token=_END then
  173. last_endtoken_filepos:=akttokenpos;
  174. current_scanner.readtoken;
  175. end;
  176. end;
  177. function try_to_consume(i:Ttoken):boolean;
  178. begin
  179. try_to_consume:=false;
  180. if (token=i) or (idtoken=i) then
  181. begin
  182. try_to_consume:=true;
  183. if token=_END then
  184. last_endtoken_filepos:=akttokenpos;
  185. current_scanner.readtoken;
  186. end;
  187. end;
  188. procedure consume_all_until(atoken : ttoken);
  189. begin
  190. while (token<>atoken) and (idtoken<>atoken) do
  191. begin
  192. Consume(token);
  193. if token=_EOF then
  194. begin
  195. Consume(atoken);
  196. Message(scan_f_end_of_file);
  197. exit;
  198. end;
  199. end;
  200. end;
  201. procedure emptystats;
  202. begin
  203. repeat
  204. until not try_to_consume(_SEMICOLON);
  205. end;
  206. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  207. begin
  208. { first check for identifier }
  209. if token<>_ID then
  210. begin
  211. consume(_ID);
  212. srsym:=generrorsym;
  213. srsymtable:=nil;
  214. consume_sym:=false;
  215. exit;
  216. end;
  217. searchsym(pattern,srsym,srsymtable);
  218. if assigned(srsym) then
  219. begin
  220. if (srsym.typ=unitsym) then
  221. begin
  222. { only allow unit.symbol access if the name was
  223. found in the current module }
  224. if srsym.owner.unitid=0 then
  225. begin
  226. consume(_ID);
  227. consume(_POINT);
  228. srsymtable:=tunitsym(srsym).unitsymtable;
  229. srsym:=searchsymonlyin(srsymtable,pattern);
  230. end
  231. else
  232. srsym:=nil;
  233. end;
  234. end;
  235. { if nothing found give error and return errorsym }
  236. if srsym=nil then
  237. begin
  238. identifier_not_found(orgpattern);
  239. srsym:=generrorsym;
  240. srsymtable:=nil;
  241. end;
  242. consume(_ID);
  243. consume_sym:=assigned(srsym);
  244. end;
  245. { reads a list of identifiers into a string list }
  246. function idlist : tidstringlist;
  247. var
  248. sc : tIdstringlist;
  249. begin
  250. sc:=TIdStringlist.Create;
  251. repeat
  252. sc.add(orgpattern,akttokenpos);
  253. consume(_ID);
  254. until not try_to_consume(_COMMA);
  255. idlist:=sc;
  256. end;
  257. {$ifdef fixLeaksOnError}
  258. procedure pbase_do_stop;
  259. var names: PStringlist;
  260. begin
  261. names := PStringlist(strContStack.pop);
  262. while names <> nil do
  263. begin
  264. dispose(names,done);
  265. names := PStringlist(strContStack.pop);
  266. end;
  267. strContStack.done;
  268. do_stop := pbase_old_do_stop;
  269. do_stop{$ifdef FPCPROCVAR}(){$endif};
  270. end;
  271. begin
  272. strContStack.init;
  273. pbase_old_do_stop := do_stop;
  274. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  275. {$endif fixLeaksOnError}
  276. end.
  277. {
  278. $Log$
  279. Revision 1.12 2001-05-06 14:49:17 peter
  280. * ppu object to class rewrite
  281. * move ppu read and write stuff to fppu
  282. Revision 1.11 2001/04/13 18:08:37 peter
  283. * scanner object to class
  284. Revision 1.10 2001/04/13 01:22:11 peter
  285. * symtable change to classes
  286. * range check generation and errors fixed, make cycle DEBUG=1 works
  287. * memory leaks fixed
  288. Revision 1.9 2001/04/02 21:20:31 peter
  289. * resulttype rewrite
  290. Revision 1.8 2001/03/11 22:58:49 peter
  291. * getsym redesign, removed the globals srsym,srsymtable
  292. Revision 1.7 2000/12/25 00:07:27 peter
  293. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  294. tlinkedlist objects)
  295. Revision 1.6 2000/10/31 22:02:49 peter
  296. * symtable splitted, no real code changes
  297. Revision 1.5 2000/09/24 15:06:21 peter
  298. * use defines.inc
  299. Revision 1.4 2000/08/27 20:19:39 peter
  300. * store strings with case in ppu, when an internal symbol is created
  301. a '$' is prefixed so it's not automatic uppercased
  302. Revision 1.3 2000/08/27 16:11:51 peter
  303. * moved some util functions from globals,cobjects to cutils
  304. * splitted files into finput,fmodule
  305. Revision 1.2 2000/07/13 11:32:44 michael
  306. + removed logs
  307. }