pbase.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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,cobjects,cclasses,
  23. tokens,globals,
  24. symbase,symdef,symsym
  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 : pprocvardef = 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. opsym : pvarsym;
  54. { symtable were unit references are stored }
  55. refsymtable : psymtable;
  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. { reads a list of identifiers into a string list }
  78. function idlist : tidstringlist;
  79. { just for an accurate position of the end of a procedure (PM) }
  80. var
  81. last_endtoken_filepos: tfileposinfo;
  82. implementation
  83. uses
  84. scanner,systems,verbose;
  85. {****************************************************************************
  86. TIdStringlistItem
  87. ****************************************************************************}
  88. constructor TIDStringlistItem.Create(const s:string;const pos:tfileposinfo);
  89. begin
  90. data:=stringdup(s);
  91. file_info:=pos;
  92. end;
  93. destructor TIDStringlistItem.Destroy;
  94. begin
  95. stringdispose(data);
  96. end;
  97. {****************************************************************************
  98. TIdStringlist
  99. ****************************************************************************}
  100. procedure tidstringlist.add(const s : string; const file_info : tfileposinfo);
  101. begin
  102. if find(s) then
  103. exit;
  104. inherited concat(tidstringlistitem.create(s,file_info));
  105. end;
  106. function tidstringlist.get(var file_info : tfileposinfo) : string;
  107. var
  108. p : tidstringlistitem;
  109. begin
  110. p:=tidstringlistitem(inherited getfirst);
  111. if p=nil then
  112. begin
  113. get:='';
  114. file_info.fileindex:=0;
  115. file_info.line:=0;
  116. file_info.column:=0;
  117. end
  118. else
  119. begin
  120. get:=p.data^;
  121. file_info:=p.file_info;
  122. p.free;
  123. end;
  124. end;
  125. function tidstringlist.find(const s:string):boolean;
  126. var
  127. newnode : tidstringlistitem;
  128. begin
  129. find:=false;
  130. newnode:=tidstringlistitem(First);
  131. while assigned(newnode) do
  132. begin
  133. if newnode.data^=s then
  134. begin
  135. find:=true;
  136. exit;
  137. end;
  138. newnode:=tidstringlistitem(newnode.next);
  139. end;
  140. end;
  141. {****************************************************************************
  142. Token Parsing
  143. ****************************************************************************}
  144. function tokenstring(i : ttoken):string;
  145. begin
  146. tokenstring:=tokeninfo^[i].str;
  147. end;
  148. { consumes token i, write error if token is different }
  149. procedure consume(i : ttoken);
  150. begin
  151. if (token<>i) and (idtoken<>i) then
  152. if token=_id then
  153. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  154. else
  155. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  156. else
  157. begin
  158. if token=_END then
  159. last_endtoken_filepos:=akttokenpos;
  160. current_scanner^.readtoken;
  161. end;
  162. end;
  163. function try_to_consume(i:Ttoken):boolean;
  164. begin
  165. try_to_consume:=false;
  166. if (token=i) or (idtoken=i) then
  167. begin
  168. try_to_consume:=true;
  169. if token=_END then
  170. last_endtoken_filepos:=akttokenpos;
  171. current_scanner^.readtoken;
  172. end;
  173. end;
  174. procedure consume_all_until(atoken : ttoken);
  175. begin
  176. while (token<>atoken) and (idtoken<>atoken) do
  177. begin
  178. Consume(token);
  179. if token=_EOF then
  180. begin
  181. Consume(atoken);
  182. Message(scan_f_end_of_file);
  183. exit;
  184. end;
  185. end;
  186. end;
  187. procedure emptystats;
  188. begin
  189. repeat
  190. until not try_to_consume(_SEMICOLON);
  191. end;
  192. { reads a list of identifiers into a string list }
  193. function idlist : tidstringlist;
  194. var
  195. sc : tIdstringlist;
  196. begin
  197. sc:=TIdStringlist.Create;
  198. repeat
  199. sc.add(orgpattern,akttokenpos);
  200. consume(_ID);
  201. until not try_to_consume(_COMMA);
  202. idlist:=sc;
  203. end;
  204. {$ifdef fixLeaksOnError}
  205. procedure pbase_do_stop;
  206. var names: PStringlist;
  207. begin
  208. names := PStringlist(strContStack.pop);
  209. while names <> nil do
  210. begin
  211. dispose(names,done);
  212. names := PStringlist(strContStack.pop);
  213. end;
  214. strContStack.done;
  215. do_stop := pbase_old_do_stop;
  216. do_stop{$ifdef FPCPROCVAR}(){$endif};
  217. end;
  218. begin
  219. strContStack.init;
  220. pbase_old_do_stop := do_stop;
  221. do_stop := {$ifdef FPCPROCVAR}(){$endif}pbase_do_stop;
  222. {$endif fixLeaksOnError}
  223. end.
  224. {
  225. $Log$
  226. Revision 1.7 2000-12-25 00:07:27 peter
  227. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  228. tlinkedlist objects)
  229. Revision 1.6 2000/10/31 22:02:49 peter
  230. * symtable splitted, no real code changes
  231. Revision 1.5 2000/09/24 15:06:21 peter
  232. * use defines.inc
  233. Revision 1.4 2000/08/27 20:19:39 peter
  234. * store strings with case in ppu, when an internal symbol is created
  235. a '$' is prefixed so it's not automatic uppercased
  236. Revision 1.3 2000/08/27 16:11:51 peter
  237. * moved some util functions from globals,cobjects to cutils
  238. * splitted files into finput,fmodule
  239. Revision 1.2 2000/07/13 11:32:44 michael
  240. + removed logs
  241. }