pbase.pas 8.7 KB

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