pbase.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. { 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.29 2004-08-08 12:06:38 florian
  225. * finally is an "endtoken" as well
  226. Revision 1.28 2004/06/20 08:55:30 florian
  227. * logs truncated
  228. Revision 1.27 2004/02/21 20:10:27 daniel
  229. - Comment out unused tokenstring function
  230. Revision 1.26 2004/01/22 17:24:49 peter
  231. * except is also an end of block token
  232. * after a label don't try to parse a statement when the next token
  233. is an end token
  234. }