pbase.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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,_EXCEPT,_FINALLY];
  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. { for operators }
  40. optoken : ttoken;
  41. { symtable were unit references are stored }
  42. refsymtable : tsymtable;
  43. { true, if only routine headers should be parsed }
  44. parse_only : boolean;
  45. { true, if we should ignore an equal in const x : 1..2=2 }
  46. ignore_equal : boolean;
  47. procedure identifier_not_found(const s:string);
  48. { function tokenstring(i : ttoken):string;}
  49. { consumes token i, if the current token is unequal i }
  50. { a syntax error is written }
  51. procedure consume(i : ttoken);
  52. {Tries to consume the token i, and returns true if it was consumed:
  53. if token=i.}
  54. function try_to_consume(i:Ttoken):boolean;
  55. { consumes all tokens til atoken (for error recovering }
  56. procedure consume_all_until(atoken : ttoken);
  57. { consumes tokens while they are semicolons }
  58. procedure consume_emptystats;
  59. { reads a list of identifiers into a string list }
  60. { consume a symbol, if not found give an error and
  61. and return an errorsym }
  62. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  63. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  64. procedure check_hints(const srsym: tsym);
  65. { just for an accurate position of the end of a procedure (PM) }
  66. var
  67. last_endtoken_filepos: tfileposinfo;
  68. implementation
  69. uses
  70. globtype,scanner,systems,verbose;
  71. {****************************************************************************
  72. Token Parsing
  73. ****************************************************************************}
  74. procedure identifier_not_found(const s:string);
  75. begin
  76. Message1(sym_e_id_not_found,s);
  77. { show a fatal that you need -S2 or -Sd, but only
  78. if we just parsed the a token that has m_class }
  79. if not(m_class in aktmodeswitches) and
  80. (Upper(s)=pattern) and
  81. (tokeninfo^[idtoken].keyword=m_class) then
  82. Message(parser_f_need_objfpc_or_delphi_mode);
  83. end;
  84. { Unused:
  85. function tokenstring(i : ttoken):string;
  86. begin
  87. tokenstring:=tokeninfo^[i].str;
  88. end;
  89. }
  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. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  166. internalerror(200501154);
  167. { only allow unit.symbol access if the name was
  168. found in the current module }
  169. if srsym.owner.iscurrentunit then
  170. begin
  171. consume(_ID);
  172. consume(_POINT);
  173. srsymtable:=tunitsym(srsym).unitsymtable;
  174. srsym:=searchsymonlyin(srsymtable,pattern);
  175. end
  176. else
  177. srsym:=nil;
  178. end;
  179. end;
  180. { if nothing found give error and return errorsym }
  181. if srsym=nil then
  182. begin
  183. identifier_not_found(orgpattern);
  184. srsym:=generrorsym;
  185. srsymtable:=nil;
  186. end;
  187. consume(_ID);
  188. consume_sym:=assigned(srsym);
  189. end;
  190. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  191. begin
  192. try_consume_hintdirective:=false;
  193. if not(m_hintdirective in aktmodeswitches) then
  194. exit;
  195. repeat
  196. case idtoken of
  197. _LIBRARY :
  198. begin
  199. include(symopt,sp_hint_library);
  200. try_consume_hintdirective:=true;
  201. end;
  202. _DEPRECATED :
  203. begin
  204. include(symopt,sp_hint_deprecated);
  205. try_consume_hintdirective:=true;
  206. end;
  207. _PLATFORM :
  208. begin
  209. include(symopt,sp_hint_platform);
  210. try_consume_hintdirective:=true;
  211. end;
  212. _UNIMPLEMENTED :
  213. begin
  214. include(symopt,sp_hint_unimplemented);
  215. try_consume_hintdirective:=true;
  216. end;
  217. else
  218. break;
  219. end;
  220. consume(Token);
  221. until false;
  222. end;
  223. end.
  224. {
  225. $Log$
  226. Revision 1.30 2005-01-19 22:19:41 peter
  227. * unit mapping rewrite
  228. * new derefmap added
  229. Revision 1.29 2004/08/08 12:06:38 florian
  230. * finally is an "endtoken" as well
  231. Revision 1.28 2004/06/20 08:55:30 florian
  232. * logs truncated
  233. Revision 1.27 2004/02/21 20:10:27 daniel
  234. - Comment out unused tokenstring function
  235. Revision 1.26 2004/01/22 17:24:49 peter
  236. * except is also an end of block token
  237. * after a label don't try to parse a statement when the next token
  238. is an end token
  239. }