pbase.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 consume_emptystats;
  78. { reads a list of identifiers into a string list }
  79. function consume_idlist : tidstringlist;
  80. { consume a symbol, if not found give an error and
  81. and return an errorsym }
  82. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  83. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  84. { just for an accurate position of the end of a procedure (PM) }
  85. var
  86. last_endtoken_filepos: tfileposinfo;
  87. implementation
  88. uses
  89. globtype,scanner,systems,verbose;
  90. {****************************************************************************
  91. TIdStringlistItem
  92. ****************************************************************************}
  93. constructor TIDStringlistItem.Create(const s:string;const pos:tfileposinfo);
  94. begin
  95. data:=stringdup(s);
  96. file_info:=pos;
  97. end;
  98. destructor TIDStringlistItem.Destroy;
  99. begin
  100. stringdispose(data);
  101. end;
  102. {****************************************************************************
  103. TIdStringlist
  104. ****************************************************************************}
  105. procedure tidstringlist.add(const s : string; const file_info : tfileposinfo);
  106. begin
  107. if find(s) then
  108. exit;
  109. inherited concat(tidstringlistitem.create(s,file_info));
  110. end;
  111. function tidstringlist.get(var file_info : tfileposinfo) : string;
  112. var
  113. p : tidstringlistitem;
  114. begin
  115. p:=tidstringlistitem(inherited getfirst);
  116. if p=nil then
  117. begin
  118. get:='';
  119. file_info.fileindex:=0;
  120. file_info.line:=0;
  121. file_info.column:=0;
  122. end
  123. else
  124. begin
  125. get:=p.data^;
  126. file_info:=p.file_info;
  127. p.free;
  128. end;
  129. end;
  130. function tidstringlist.find(const s:string):boolean;
  131. var
  132. newnode : tidstringlistitem;
  133. begin
  134. find:=false;
  135. newnode:=tidstringlistitem(First);
  136. while assigned(newnode) do
  137. begin
  138. if newnode.data^=s then
  139. begin
  140. find:=true;
  141. exit;
  142. end;
  143. newnode:=tidstringlistitem(newnode.next);
  144. end;
  145. end;
  146. {****************************************************************************
  147. Token Parsing
  148. ****************************************************************************}
  149. procedure identifier_not_found(const s:string);
  150. begin
  151. Message1(sym_e_id_not_found,s);
  152. { show a fatal that you need -S2 or -Sd, but only
  153. if we just parsed the a token that has m_class }
  154. if not(m_class in aktmodeswitches) and
  155. (Upper(s)=pattern) and
  156. (tokeninfo^[idtoken].keyword=m_class) then
  157. Message(parser_f_need_objfpc_or_delphi_mode);
  158. end;
  159. function tokenstring(i : ttoken):string;
  160. begin
  161. tokenstring:=tokeninfo^[i].str;
  162. end;
  163. { consumes token i, write error if token is different }
  164. procedure consume(i : ttoken);
  165. begin
  166. if (token<>i) and (idtoken<>i) then
  167. if token=_id then
  168. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  169. else
  170. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  171. else
  172. begin
  173. if token=_END then
  174. last_endtoken_filepos:=akttokenpos;
  175. current_scanner.readtoken;
  176. end;
  177. end;
  178. function try_to_consume(i:Ttoken):boolean;
  179. begin
  180. try_to_consume:=false;
  181. if (token=i) or (idtoken=i) then
  182. begin
  183. try_to_consume:=true;
  184. if token=_END then
  185. last_endtoken_filepos:=akttokenpos;
  186. current_scanner.readtoken;
  187. end;
  188. end;
  189. procedure consume_all_until(atoken : ttoken);
  190. begin
  191. while (token<>atoken) and (idtoken<>atoken) do
  192. begin
  193. Consume(token);
  194. if token=_EOF then
  195. begin
  196. Consume(atoken);
  197. Message(scan_f_end_of_file);
  198. exit;
  199. end;
  200. end;
  201. end;
  202. procedure consume_emptystats;
  203. begin
  204. repeat
  205. until not try_to_consume(_SEMICOLON);
  206. end;
  207. { reads a list of identifiers into a string list }
  208. function consume_idlist : tidstringlist;
  209. var
  210. sc : tIdstringlist;
  211. begin
  212. sc:=TIdStringlist.Create;
  213. repeat
  214. sc.add(orgpattern,akttokenpos);
  215. consume(_ID);
  216. until not try_to_consume(_COMMA);
  217. consume_idlist:=sc;
  218. end;
  219. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  220. begin
  221. { first check for identifier }
  222. if token<>_ID then
  223. begin
  224. consume(_ID);
  225. srsym:=generrorsym;
  226. srsymtable:=nil;
  227. consume_sym:=false;
  228. exit;
  229. end;
  230. searchsym(pattern,srsym,srsymtable);
  231. if assigned(srsym) then
  232. begin
  233. if (srsym.typ=unitsym) then
  234. begin
  235. { only allow unit.symbol access if the name was
  236. found in the current module }
  237. if srsym.owner.unitid=0 then
  238. begin
  239. consume(_ID);
  240. consume(_POINT);
  241. srsymtable:=tunitsym(srsym).unitsymtable;
  242. srsym:=searchsymonlyin(srsymtable,pattern);
  243. end
  244. else
  245. srsym:=nil;
  246. end;
  247. end;
  248. { if nothing found give error and return errorsym }
  249. if srsym=nil then
  250. begin
  251. identifier_not_found(orgpattern);
  252. srsym:=generrorsym;
  253. srsymtable:=nil;
  254. end;
  255. consume(_ID);
  256. consume_sym:=assigned(srsym);
  257. end;
  258. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  259. begin
  260. try_consume_hintdirective:=false;
  261. if not(m_hintdirective in aktmodeswitches) then
  262. exit;
  263. repeat
  264. case idtoken of
  265. _LIBRARY :
  266. begin
  267. include(symopt,sp_hint_library);
  268. try_consume_hintdirective:=true;
  269. end;
  270. _DEPRECATED :
  271. begin
  272. include(symopt,sp_hint_deprecated);
  273. try_consume_hintdirective:=true;
  274. end;
  275. _PLATFORM :
  276. begin
  277. include(symopt,sp_hint_platform);
  278. try_consume_hintdirective:=true;
  279. end;
  280. else
  281. break;
  282. end;
  283. consume(Token);
  284. until false;
  285. end;
  286. {$ifdef fixLeaksOnError}
  287. procedure pbase_do_stop;
  288. var names: PStringlist;
  289. begin
  290. names := PStringlist(strContStack.pop);
  291. while names <> nil do
  292. begin
  293. dispose(names,done);
  294. names := PStringlist(strContStack.pop);
  295. end;
  296. strContStack.done;
  297. do_stop := pbase_old_do_stop;
  298. do_stop{$ifdef FPCPROCVAR}(){$endif};
  299. end;
  300. begin
  301. strContStack.init;
  302. pbase_old_do_stop := do_stop;
  303. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  304. {$endif fixLeaksOnError}
  305. end.
  306. {
  307. $Log$
  308. Revision 1.13 2001-06-03 21:57:35 peter
  309. + hint directive parsing support
  310. Revision 1.12 2001/05/06 14:49:17 peter
  311. * ppu object to class rewrite
  312. * move ppu read and write stuff to fppu
  313. Revision 1.11 2001/04/13 18:08:37 peter
  314. * scanner object to class
  315. Revision 1.10 2001/04/13 01:22:11 peter
  316. * symtable change to classes
  317. * range check generation and errors fixed, make cycle DEBUG=1 works
  318. * memory leaks fixed
  319. Revision 1.9 2001/04/02 21:20:31 peter
  320. * resulttype rewrite
  321. Revision 1.8 2001/03/11 22:58:49 peter
  322. * getsym redesign, removed the globals srsym,srsymtable
  323. Revision 1.7 2000/12/25 00:07:27 peter
  324. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  325. tlinkedlist objects)
  326. Revision 1.6 2000/10/31 22:02:49 peter
  327. * symtable splitted, no real code changes
  328. Revision 1.5 2000/09/24 15:06:21 peter
  329. * use defines.inc
  330. Revision 1.4 2000/08/27 20:19:39 peter
  331. * store strings with case in ppu, when an internal symbol is created
  332. a '$' is prefixed so it's not automatic uppercased
  333. Revision 1.3 2000/08/27 16:11:51 peter
  334. * moved some util functions from globals,cobjects to cutils
  335. * splitted files into finput,fmodule
  336. Revision 1.2 2000/07/13 11:32:44 michael
  337. + removed logs
  338. }