pbase.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. 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 emptystats;
  77. { consume a symbol, if not found give an error and
  78. and return an errorsym }
  79. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  80. { reads a list of identifiers into a string list }
  81. function idlist : tidstringlist;
  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. 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. function tokenstring(i : ttoken):string;
  148. begin
  149. tokenstring:=tokeninfo^[i].str;
  150. end;
  151. { consumes token i, write error if token is different }
  152. procedure consume(i : ttoken);
  153. begin
  154. if (token<>i) and (idtoken<>i) then
  155. if token=_id then
  156. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  157. else
  158. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  159. else
  160. begin
  161. if token=_END then
  162. last_endtoken_filepos:=akttokenpos;
  163. current_scanner.readtoken;
  164. end;
  165. end;
  166. function try_to_consume(i:Ttoken):boolean;
  167. begin
  168. try_to_consume:=false;
  169. if (token=i) or (idtoken=i) then
  170. begin
  171. try_to_consume:=true;
  172. if token=_END then
  173. last_endtoken_filepos:=akttokenpos;
  174. current_scanner.readtoken;
  175. end;
  176. end;
  177. procedure consume_all_until(atoken : ttoken);
  178. begin
  179. while (token<>atoken) and (idtoken<>atoken) do
  180. begin
  181. Consume(token);
  182. if token=_EOF then
  183. begin
  184. Consume(atoken);
  185. Message(scan_f_end_of_file);
  186. exit;
  187. end;
  188. end;
  189. end;
  190. procedure emptystats;
  191. begin
  192. repeat
  193. until not try_to_consume(_SEMICOLON);
  194. end;
  195. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  196. begin
  197. { first check for identifier }
  198. if token<>_ID then
  199. begin
  200. consume(_ID);
  201. srsym:=generrorsym;
  202. srsymtable:=nil;
  203. consume_sym:=false;
  204. exit;
  205. end;
  206. searchsym(pattern,srsym,srsymtable);
  207. if assigned(srsym) then
  208. begin
  209. if (srsym.typ=unitsym) then
  210. begin
  211. { only allow unit.symbol access if the name was
  212. found in the current module }
  213. if srsym.owner.unitid=0 then
  214. begin
  215. consume(_ID);
  216. consume(_POINT);
  217. srsymtable:=tunitsym(srsym).unitsymtable;
  218. srsym:=searchsymonlyin(srsymtable,pattern);
  219. end
  220. else
  221. srsym:=nil;
  222. end;
  223. end;
  224. { if nothing found give error and return errorsym }
  225. if srsym=nil then
  226. begin
  227. identifier_not_found(orgpattern);
  228. srsym:=generrorsym;
  229. srsymtable:=nil;
  230. end;
  231. consume(_ID);
  232. consume_sym:=assigned(srsym);
  233. end;
  234. { reads a list of identifiers into a string list }
  235. function idlist : tidstringlist;
  236. var
  237. sc : tIdstringlist;
  238. begin
  239. sc:=TIdStringlist.Create;
  240. repeat
  241. sc.add(orgpattern,akttokenpos);
  242. consume(_ID);
  243. until not try_to_consume(_COMMA);
  244. idlist:=sc;
  245. end;
  246. {$ifdef fixLeaksOnError}
  247. procedure pbase_do_stop;
  248. var names: PStringlist;
  249. begin
  250. names := PStringlist(strContStack.pop);
  251. while names <> nil do
  252. begin
  253. dispose(names,done);
  254. names := PStringlist(strContStack.pop);
  255. end;
  256. strContStack.done;
  257. do_stop := pbase_old_do_stop;
  258. do_stop{$ifdef FPCPROCVAR}(){$endif};
  259. end;
  260. begin
  261. strContStack.init;
  262. pbase_old_do_stop := do_stop;
  263. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  264. {$endif fixLeaksOnError}
  265. end.
  266. {
  267. $Log$
  268. Revision 1.11 2001-04-13 18:08:37 peter
  269. * scanner object to class
  270. Revision 1.10 2001/04/13 01:22:11 peter
  271. * symtable change to classes
  272. * range check generation and errors fixed, make cycle DEBUG=1 works
  273. * memory leaks fixed
  274. Revision 1.9 2001/04/02 21:20:31 peter
  275. * resulttype rewrite
  276. Revision 1.8 2001/03/11 22:58:49 peter
  277. * getsym redesign, removed the globals srsym,srsymtable
  278. Revision 1.7 2000/12/25 00:07:27 peter
  279. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  280. tlinkedlist objects)
  281. Revision 1.6 2000/10/31 22:02:49 peter
  282. * symtable splitted, no real code changes
  283. Revision 1.5 2000/09/24 15:06:21 peter
  284. * use defines.inc
  285. Revision 1.4 2000/08/27 20:19:39 peter
  286. * store strings with case in ppu, when an internal symbol is created
  287. a '$' is prefixed so it's not automatic uppercased
  288. Revision 1.3 2000/08/27 16:11:51 peter
  289. * moved some util functions from globals,cobjects to cutils
  290. * splitted files into finput,fmodule
  291. Revision 1.2 2000/07/13 11:32:44 michael
  292. + removed logs
  293. }