pbase.pas 9.4 KB

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