pbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. { special for handling procedure vars }
  33. getprocvardef : tprocvardef = nil;
  34. type
  35. { listitem }
  36. tidstringlistitem = class(tlinkedlistitem)
  37. data : pstring;
  38. file_info : tfileposinfo;
  39. constructor Create(const s:string;const pos:tfileposinfo);
  40. destructor Destroy;override;
  41. end;
  42. tidstringlist=class(tlinkedlist)
  43. procedure add(const s : string;const file_info : tfileposinfo);
  44. function get(var file_info : tfileposinfo) : string;
  45. function find(const s:string):boolean;
  46. end;
  47. var
  48. { size of data segment, set by proc_unit or proc_program }
  49. datasize : longint;
  50. { for operators }
  51. optoken : ttoken;
  52. { symtable were unit references are stored }
  53. refsymtable : tsymtable;
  54. { true, if only routine headers should be parsed }
  55. parse_only : boolean;
  56. { true, if we should ignore an equal in const x : 1..2=2 }
  57. ignore_equal : boolean;
  58. {$ifdef fixLeaksOnError}
  59. { not worth it to make a pstack, there's only one data field (a pointer). }
  60. { in the interface, because pmodules and psub also use it for their names }
  61. var strContStack: TStack;
  62. pbase_old_do_stop: tstopprocedure;
  63. {$endif fixLeaksOnError}
  64. procedure identifier_not_found(const s:string);
  65. function tokenstring(i : ttoken):string;
  66. { consumes token i, if the current token is unequal i }
  67. { a syntax error is written }
  68. procedure consume(i : ttoken);
  69. {Tries to consume the token i, and returns true if it was consumed:
  70. if token=i.}
  71. function try_to_consume(i:Ttoken):boolean;
  72. { consumes all tokens til atoken (for error recovering }
  73. procedure consume_all_until(atoken : ttoken);
  74. { consumes tokens while they are semicolons }
  75. procedure consume_emptystats;
  76. { reads a list of identifiers into a string list }
  77. function consume_idlist : tidstringlist;
  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. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  82. { just for an accurate position of the end of a procedure (PM) }
  83. var
  84. last_endtoken_filepos: tfileposinfo;
  85. implementation
  86. uses
  87. globtype,scanner,systems,verbose;
  88. {****************************************************************************
  89. TIdStringlistItem
  90. ****************************************************************************}
  91. constructor TIDStringlistItem.Create(const s:string;const pos:tfileposinfo);
  92. begin
  93. data:=stringdup(s);
  94. file_info:=pos;
  95. end;
  96. destructor TIDStringlistItem.Destroy;
  97. begin
  98. stringdispose(data);
  99. end;
  100. {****************************************************************************
  101. TIdStringlist
  102. ****************************************************************************}
  103. procedure tidstringlist.add(const s : string; const file_info : tfileposinfo);
  104. begin
  105. if find(s) then
  106. exit;
  107. inherited concat(tidstringlistitem.create(s,file_info));
  108. end;
  109. function tidstringlist.get(var file_info : tfileposinfo) : string;
  110. var
  111. p : tidstringlistitem;
  112. begin
  113. p:=tidstringlistitem(inherited getfirst);
  114. if p=nil then
  115. begin
  116. get:='';
  117. file_info.fileindex:=0;
  118. file_info.line:=0;
  119. file_info.column:=0;
  120. end
  121. else
  122. begin
  123. get:=p.data^;
  124. file_info:=p.file_info;
  125. p.free;
  126. end;
  127. end;
  128. function tidstringlist.find(const s:string):boolean;
  129. var
  130. newnode : tidstringlistitem;
  131. begin
  132. find:=false;
  133. newnode:=tidstringlistitem(First);
  134. while assigned(newnode) do
  135. begin
  136. if newnode.data^=s then
  137. begin
  138. find:=true;
  139. exit;
  140. end;
  141. newnode:=tidstringlistitem(newnode.next);
  142. end;
  143. end;
  144. {****************************************************************************
  145. Token Parsing
  146. ****************************************************************************}
  147. procedure identifier_not_found(const s:string);
  148. begin
  149. Message1(sym_e_id_not_found,s);
  150. { show a fatal that you need -S2 or -Sd, but only
  151. if we just parsed the a token that has m_class }
  152. if not(m_class in aktmodeswitches) and
  153. (Upper(s)=pattern) and
  154. (tokeninfo^[idtoken].keyword=m_class) then
  155. Message(parser_f_need_objfpc_or_delphi_mode);
  156. end;
  157. function tokenstring(i : ttoken):string;
  158. begin
  159. tokenstring:=tokeninfo^[i].str;
  160. end;
  161. { consumes token i, write error if token is different }
  162. procedure consume(i : ttoken);
  163. begin
  164. if (token<>i) and (idtoken<>i) then
  165. if token=_id then
  166. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  167. else
  168. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  169. else
  170. begin
  171. if token=_END then
  172. last_endtoken_filepos:=akttokenpos;
  173. current_scanner.readtoken;
  174. end;
  175. end;
  176. function try_to_consume(i:Ttoken):boolean;
  177. begin
  178. try_to_consume:=false;
  179. if (token=i) or (idtoken=i) then
  180. begin
  181. try_to_consume:=true;
  182. if token=_END then
  183. last_endtoken_filepos:=akttokenpos;
  184. current_scanner.readtoken;
  185. end;
  186. end;
  187. procedure consume_all_until(atoken : ttoken);
  188. begin
  189. while (token<>atoken) and (idtoken<>atoken) do
  190. begin
  191. Consume(token);
  192. if token=_EOF then
  193. begin
  194. Consume(atoken);
  195. Message(scan_f_end_of_file);
  196. exit;
  197. end;
  198. end;
  199. end;
  200. procedure consume_emptystats;
  201. begin
  202. repeat
  203. until not try_to_consume(_SEMICOLON);
  204. end;
  205. { reads a list of identifiers into a string list }
  206. function consume_idlist : tidstringlist;
  207. var
  208. sc : tIdstringlist;
  209. begin
  210. sc:=TIdStringlist.Create;
  211. repeat
  212. sc.add(orgpattern,akttokenpos);
  213. consume(_ID);
  214. until not try_to_consume(_COMMA);
  215. consume_idlist:=sc;
  216. end;
  217. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  218. begin
  219. { first check for identifier }
  220. if token<>_ID then
  221. begin
  222. consume(_ID);
  223. srsym:=generrorsym;
  224. srsymtable:=nil;
  225. consume_sym:=false;
  226. exit;
  227. end;
  228. searchsym(pattern,srsym,srsymtable);
  229. if assigned(srsym) then
  230. begin
  231. if (srsym.typ=unitsym) then
  232. begin
  233. { only allow unit.symbol access if the name was
  234. found in the current module }
  235. if srsym.owner.unitid=0 then
  236. begin
  237. consume(_ID);
  238. consume(_POINT);
  239. srsymtable:=tunitsym(srsym).unitsymtable;
  240. srsym:=searchsymonlyin(srsymtable,pattern);
  241. end
  242. else
  243. srsym:=nil;
  244. end;
  245. end;
  246. { if nothing found give error and return errorsym }
  247. if srsym=nil then
  248. begin
  249. identifier_not_found(orgpattern);
  250. srsym:=generrorsym;
  251. srsymtable:=nil;
  252. end;
  253. consume(_ID);
  254. consume_sym:=assigned(srsym);
  255. end;
  256. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  257. begin
  258. try_consume_hintdirective:=false;
  259. if not(m_hintdirective in aktmodeswitches) then
  260. exit;
  261. repeat
  262. case idtoken of
  263. _LIBRARY :
  264. begin
  265. include(symopt,sp_hint_library);
  266. try_consume_hintdirective:=true;
  267. end;
  268. _DEPRECATED :
  269. begin
  270. include(symopt,sp_hint_deprecated);
  271. try_consume_hintdirective:=true;
  272. end;
  273. _PLATFORM :
  274. begin
  275. include(symopt,sp_hint_platform);
  276. try_consume_hintdirective:=true;
  277. end;
  278. else
  279. break;
  280. end;
  281. consume(Token);
  282. until false;
  283. end;
  284. {$ifdef fixLeaksOnError}
  285. procedure pbase_do_stop;
  286. var names: PStringlist;
  287. begin
  288. names := PStringlist(strContStack.pop);
  289. while names <> nil do
  290. begin
  291. dispose(names,done);
  292. names := PStringlist(strContStack.pop);
  293. end;
  294. strContStack.done;
  295. do_stop := pbase_old_do_stop;
  296. do_stop{$ifdef FPCPROCVAR}(){$endif};
  297. end;
  298. begin
  299. strContStack.init;
  300. pbase_old_do_stop := do_stop;
  301. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  302. {$endif fixLeaksOnError}
  303. end.
  304. {
  305. $Log$
  306. Revision 1.18 2002-08-17 09:23:38 florian
  307. * first part of procinfo rewrite
  308. Revision 1.17 2002/05/18 13:34:11 peter
  309. * readded missing revisions
  310. Revision 1.16 2002/05/16 19:46:42 carl
  311. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  312. + try to fix temp allocation (still in ifdef)
  313. + generic constructor calls
  314. + start of tassembler / tmodulebase class cleanup
  315. Revision 1.14 2002/01/06 21:47:32 peter
  316. * removed getprocvar, use only getprocvardef
  317. }