pbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. otsym : tvarsym;
  53. { symtable were unit references are stored }
  54. refsymtable : tsymtable;
  55. { true, if only routine headers should be parsed }
  56. parse_only : boolean;
  57. { true, if we should ignore an equal in const x : 1..2=2 }
  58. ignore_equal : boolean;
  59. {$ifdef fixLeaksOnError}
  60. { not worth it to make a pstack, there's only one data field (a pointer). }
  61. { in the interface, because pmodules and psub also use it for their names }
  62. var strContStack: TStack;
  63. pbase_old_do_stop: tstopprocedure;
  64. {$endif fixLeaksOnError}
  65. procedure identifier_not_found(const s:string);
  66. function tokenstring(i : ttoken):string;
  67. { consumes token i, if the current token is unequal i }
  68. { a syntax error is written }
  69. procedure consume(i : ttoken);
  70. {Tries to consume the token i, and returns true if it was consumed:
  71. if token=i.}
  72. function try_to_consume(i:Ttoken):boolean;
  73. { consumes all tokens til atoken (for error recovering }
  74. procedure consume_all_until(atoken : ttoken);
  75. { consumes tokens while they are semicolons }
  76. procedure consume_emptystats;
  77. { reads a list of identifiers into a string list }
  78. function consume_idlist : tidstringlist;
  79. { consume a symbol, if not found give an error and
  80. and return an errorsym }
  81. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  82. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  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 consume_emptystats;
  202. begin
  203. repeat
  204. until not try_to_consume(_SEMICOLON);
  205. end;
  206. { reads a list of identifiers into a string list }
  207. function consume_idlist : tidstringlist;
  208. var
  209. sc : tIdstringlist;
  210. begin
  211. sc:=TIdStringlist.Create;
  212. repeat
  213. sc.add(orgpattern,akttokenpos);
  214. consume(_ID);
  215. until not try_to_consume(_COMMA);
  216. consume_idlist:=sc;
  217. end;
  218. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  219. begin
  220. { first check for identifier }
  221. if token<>_ID then
  222. begin
  223. consume(_ID);
  224. srsym:=generrorsym;
  225. srsymtable:=nil;
  226. consume_sym:=false;
  227. exit;
  228. end;
  229. searchsym(pattern,srsym,srsymtable);
  230. if assigned(srsym) then
  231. begin
  232. if (srsym.typ=unitsym) then
  233. begin
  234. { only allow unit.symbol access if the name was
  235. found in the current module }
  236. if srsym.owner.unitid=0 then
  237. begin
  238. consume(_ID);
  239. consume(_POINT);
  240. srsymtable:=tunitsym(srsym).unitsymtable;
  241. srsym:=searchsymonlyin(srsymtable,pattern);
  242. end
  243. else
  244. srsym:=nil;
  245. end;
  246. end;
  247. { if nothing found give error and return errorsym }
  248. if srsym=nil then
  249. begin
  250. identifier_not_found(orgpattern);
  251. srsym:=generrorsym;
  252. srsymtable:=nil;
  253. end;
  254. consume(_ID);
  255. consume_sym:=assigned(srsym);
  256. end;
  257. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  258. begin
  259. try_consume_hintdirective:=false;
  260. if not(m_hintdirective in aktmodeswitches) then
  261. exit;
  262. repeat
  263. case idtoken of
  264. _LIBRARY :
  265. begin
  266. include(symopt,sp_hint_library);
  267. try_consume_hintdirective:=true;
  268. end;
  269. _DEPRECATED :
  270. begin
  271. include(symopt,sp_hint_deprecated);
  272. try_consume_hintdirective:=true;
  273. end;
  274. _PLATFORM :
  275. begin
  276. include(symopt,sp_hint_platform);
  277. try_consume_hintdirective:=true;
  278. end;
  279. else
  280. break;
  281. end;
  282. consume(Token);
  283. until false;
  284. end;
  285. {$ifdef fixLeaksOnError}
  286. procedure pbase_do_stop;
  287. var names: PStringlist;
  288. begin
  289. names := PStringlist(strContStack.pop);
  290. while names <> nil do
  291. begin
  292. dispose(names,done);
  293. names := PStringlist(strContStack.pop);
  294. end;
  295. strContStack.done;
  296. do_stop := pbase_old_do_stop;
  297. do_stop{$ifdef FPCPROCVAR}(){$endif};
  298. end;
  299. begin
  300. strContStack.init;
  301. pbase_old_do_stop := do_stop;
  302. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  303. {$endif fixLeaksOnError}
  304. end.
  305. {
  306. $Log$
  307. Revision 1.17 2002-05-18 13:34:11 peter
  308. * readded missing revisions
  309. Revision 1.16 2002/05/16 19:46:42 carl
  310. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  311. + try to fix temp allocation (still in ifdef)
  312. + generic constructor calls
  313. + start of tassembler / tmodulebase class cleanup
  314. Revision 1.14 2002/01/06 21:47:32 peter
  315. * removed getprocvar, use only getprocvardef
  316. }