pbase.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. ;
  26. const
  27. { tokens that end a block or statement. And don't require
  28. a ; on the statement before }
  29. endtokens = [_SEMICOLON,_END,_ELSE,_UNTIL];
  30. { true, if we are after an assignement }
  31. afterassignment : boolean = false;
  32. { special for handling procedure vars }
  33. getprocvardef : tprocvardef = nil;
  34. var
  35. { size of data segment, set by proc_unit or proc_program }
  36. datasize : longint;
  37. { for operators }
  38. optoken : ttoken;
  39. { symtable were unit references are stored }
  40. refsymtable : tsymtable;
  41. { true, if only routine headers should be parsed }
  42. parse_only : boolean;
  43. { true, if we should ignore an equal in const x : 1..2=2 }
  44. ignore_equal : boolean;
  45. procedure identifier_not_found(const s:string);
  46. function tokenstring(i : ttoken):string;
  47. { consumes token i, if the current token is unequal i }
  48. { a syntax error is written }
  49. procedure consume(i : ttoken);
  50. {Tries to consume the token i, and returns true if it was consumed:
  51. if token=i.}
  52. function try_to_consume(i:Ttoken):boolean;
  53. { consumes all tokens til atoken (for error recovering }
  54. procedure consume_all_until(atoken : ttoken);
  55. { consumes tokens while they are semicolons }
  56. procedure consume_emptystats;
  57. { reads a list of identifiers into a string list }
  58. { consume a symbol, if not found give an error and
  59. and return an errorsym }
  60. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  61. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  62. procedure check_hints(const srsym: tsym);
  63. { just for an accurate position of the end of a procedure (PM) }
  64. var
  65. last_endtoken_filepos: tfileposinfo;
  66. implementation
  67. uses
  68. globtype,scanner,systems,verbose;
  69. {****************************************************************************
  70. Token Parsing
  71. ****************************************************************************}
  72. procedure identifier_not_found(const s:string);
  73. begin
  74. Message1(sym_e_id_not_found,s);
  75. { show a fatal that you need -S2 or -Sd, but only
  76. if we just parsed the a token that has m_class }
  77. if not(m_class in aktmodeswitches) and
  78. (Upper(s)=pattern) and
  79. (tokeninfo^[idtoken].keyword=m_class) then
  80. Message(parser_f_need_objfpc_or_delphi_mode);
  81. end;
  82. function tokenstring(i : ttoken):string;
  83. begin
  84. tokenstring:=tokeninfo^[i].str;
  85. end;
  86. { consumes token i, write error if token is different }
  87. procedure consume(i : ttoken);
  88. begin
  89. if (token<>i) and (idtoken<>i) then
  90. if token=_id then
  91. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  92. else
  93. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  94. else
  95. begin
  96. if token=_END then
  97. last_endtoken_filepos:=akttokenpos;
  98. current_scanner.readtoken;
  99. end;
  100. end;
  101. function try_to_consume(i:Ttoken):boolean;
  102. begin
  103. try_to_consume:=false;
  104. if (token=i) or (idtoken=i) then
  105. begin
  106. try_to_consume:=true;
  107. if token=_END then
  108. last_endtoken_filepos:=akttokenpos;
  109. current_scanner.readtoken;
  110. end;
  111. end;
  112. procedure consume_all_until(atoken : ttoken);
  113. begin
  114. while (token<>atoken) and (idtoken<>atoken) do
  115. begin
  116. Consume(token);
  117. if token=_EOF then
  118. begin
  119. Consume(atoken);
  120. Message(scan_f_end_of_file);
  121. exit;
  122. end;
  123. end;
  124. end;
  125. procedure consume_emptystats;
  126. begin
  127. repeat
  128. until not try_to_consume(_SEMICOLON);
  129. end;
  130. { check if a symbol contains the hint directive, and if so gives out a hint
  131. if required.
  132. }
  133. procedure check_hints(const srsym: tsym);
  134. begin
  135. if not assigned(srsym) then
  136. exit;
  137. if sp_hint_deprecated in srsym.symoptions then
  138. Message1(sym_w_deprecated_symbol,srsym.realname);
  139. if sp_hint_platform in srsym.symoptions then
  140. Message1(sym_w_non_portable_symbol,srsym.realname);
  141. if sp_hint_unimplemented in srsym.symoptions then
  142. Message1(sym_w_non_implemented_symbol,srsym.realname);
  143. end;
  144. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  145. begin
  146. { first check for identifier }
  147. if token<>_ID then
  148. begin
  149. consume(_ID);
  150. srsym:=generrorsym;
  151. srsymtable:=nil;
  152. consume_sym:=false;
  153. exit;
  154. end;
  155. searchsym(pattern,srsym,srsymtable);
  156. check_hints(srsym);
  157. if assigned(srsym) then
  158. begin
  159. if (srsym.typ=unitsym) then
  160. begin
  161. { only allow unit.symbol access if the name was
  162. found in the current module }
  163. if srsym.owner.unitid=0 then
  164. begin
  165. consume(_ID);
  166. consume(_POINT);
  167. srsymtable:=tunitsym(srsym).unitsymtable;
  168. srsym:=searchsymonlyin(srsymtable,pattern);
  169. end
  170. else
  171. srsym:=nil;
  172. end;
  173. end;
  174. { if nothing found give error and return errorsym }
  175. if srsym=nil then
  176. begin
  177. identifier_not_found(orgpattern);
  178. srsym:=generrorsym;
  179. srsymtable:=nil;
  180. end;
  181. consume(_ID);
  182. consume_sym:=assigned(srsym);
  183. end;
  184. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  185. begin
  186. try_consume_hintdirective:=false;
  187. if not(m_hintdirective in aktmodeswitches) then
  188. exit;
  189. repeat
  190. case idtoken of
  191. _LIBRARY :
  192. begin
  193. include(symopt,sp_hint_library);
  194. try_consume_hintdirective:=true;
  195. end;
  196. _DEPRECATED :
  197. begin
  198. include(symopt,sp_hint_deprecated);
  199. try_consume_hintdirective:=true;
  200. end;
  201. _PLATFORM :
  202. begin
  203. include(symopt,sp_hint_platform);
  204. try_consume_hintdirective:=true;
  205. end;
  206. _UNIMPLEMENTED :
  207. begin
  208. include(symopt,sp_hint_unimplemented);
  209. try_consume_hintdirective:=true;
  210. end;
  211. else
  212. break;
  213. end;
  214. consume(Token);
  215. until false;
  216. end;
  217. end.
  218. {
  219. $Log$
  220. Revision 1.23 2003-03-17 18:55:30 peter
  221. * allow more tokens instead of only semicolon after inherited
  222. Revision 1.22 2002/12/05 19:28:05 carl
  223. - remove lower in hint
  224. Revision 1.21 2002/11/30 11:12:48 carl
  225. + checking for symbols used with hint directives is done mostly in pexpr
  226. only now
  227. Revision 1.20 2002/11/29 22:31:19 carl
  228. + unimplemented hint directive added
  229. * hint directive parsing implemented
  230. * warning on these directives
  231. Revision 1.19 2002/09/09 17:34:15 peter
  232. * tdicationary.replace added to replace and item in a dictionary. This
  233. is only allowed for the same name
  234. * varsyms are inserted in symtable before the types are parsed. This
  235. fixes the long standing "var longint : longint" bug
  236. - consume_idlist and idstringlist removed. The loops are inserted
  237. at the callers place and uses the symtable for duplicate id checking
  238. Revision 1.18 2002/08/17 09:23:38 florian
  239. * first part of procinfo rewrite
  240. Revision 1.17 2002/05/18 13:34:11 peter
  241. * readded missing revisions
  242. Revision 1.16 2002/05/16 19:46:42 carl
  243. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  244. + try to fix temp allocation (still in ifdef)
  245. + generic constructor calls
  246. + start of tassembler / tmodulebase class cleanup
  247. Revision 1.14 2002/01/06 21:47:32 peter
  248. * removed getprocvar, use only getprocvardef
  249. }