pbase.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains some helper routines for the parser
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. tokens,globals,
  23. symconst,symbase,symtype,symdef,symsym,symtable
  24. ;
  25. const
  26. { tokens that end a block or statement. And don't require
  27. a ; on the statement before }
  28. endtokens = [_SEMICOLON,_END,_ELSE,_UNTIL,_EXCEPT,_FINALLY];
  29. { true, if we are after an assignement }
  30. afterassignment : boolean = false;
  31. { true, if we are parsing arguments }
  32. in_args : boolean = false;
  33. { true, if we got an @ to get the address }
  34. got_addrn : boolean = false;
  35. { special for handling procedure vars }
  36. getprocvardef : tprocvardef = nil;
  37. var
  38. { for operators }
  39. optoken : ttoken;
  40. { symtable were unit references are stored }
  41. refsymtable : tsymtable;
  42. { true, if only routine headers should be parsed }
  43. parse_only : boolean;
  44. { true, if we should ignore an equal in const x : 1..2=2 }
  45. ignore_equal : boolean;
  46. procedure identifier_not_found(const s:string);
  47. { function tokenstring(i : ttoken):string;}
  48. { consumes token i, if the current token is unequal i }
  49. { a syntax error is written }
  50. procedure consume(i : ttoken);
  51. {Tries to consume the token i, and returns true if it was consumed:
  52. if token=i.}
  53. function try_to_consume(i:Ttoken):boolean;
  54. { consumes all tokens til atoken (for error recovering }
  55. procedure consume_all_until(atoken : ttoken);
  56. { consumes tokens while they are semicolons }
  57. procedure consume_emptystats;
  58. { reads a list of identifiers into a string list }
  59. { consume a symbol, if not found give an error and
  60. and return an errorsym }
  61. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  62. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  63. procedure check_hints(const srsym: tsym);
  64. { just for an accurate position of the end of a procedure (PM) }
  65. var
  66. last_endtoken_filepos: tfileposinfo;
  67. implementation
  68. uses
  69. globtype,scanner,systems,verbose;
  70. {****************************************************************************
  71. Token Parsing
  72. ****************************************************************************}
  73. procedure identifier_not_found(const s:string);
  74. begin
  75. Message1(sym_e_id_not_found,s);
  76. { show a fatal that you need -S2 or -Sd, but only
  77. if we just parsed the a token that has m_class }
  78. if not(m_class in aktmodeswitches) and
  79. (Upper(s)=pattern) and
  80. (tokeninfo^[idtoken].keyword=m_class) then
  81. Message(parser_f_need_objfpc_or_delphi_mode);
  82. end;
  83. { Unused:
  84. function tokenstring(i : ttoken):string;
  85. begin
  86. tokenstring:=tokeninfo^[i].str;
  87. end;
  88. }
  89. { consumes token i, write error if token is different }
  90. procedure consume(i : ttoken);
  91. begin
  92. if (token<>i) and (idtoken<>i) then
  93. if token=_id then
  94. Message2(scan_f_syn_expected,tokeninfo^[i].str,'identifier '+pattern)
  95. else
  96. Message2(scan_f_syn_expected,tokeninfo^[i].str,tokeninfo^[token].str)
  97. else
  98. begin
  99. if token=_END then
  100. last_endtoken_filepos:=akttokenpos;
  101. current_scanner.readtoken(true);
  102. end;
  103. end;
  104. function try_to_consume(i:Ttoken):boolean;
  105. begin
  106. try_to_consume:=false;
  107. if (token=i) or (idtoken=i) then
  108. begin
  109. try_to_consume:=true;
  110. if token=_END then
  111. last_endtoken_filepos:=akttokenpos;
  112. current_scanner.readtoken(true);
  113. end;
  114. end;
  115. procedure consume_all_until(atoken : ttoken);
  116. begin
  117. while (token<>atoken) and (idtoken<>atoken) do
  118. begin
  119. Consume(token);
  120. if token=_EOF then
  121. begin
  122. Consume(atoken);
  123. Message(scan_f_end_of_file);
  124. exit;
  125. end;
  126. end;
  127. end;
  128. procedure consume_emptystats;
  129. begin
  130. repeat
  131. until not try_to_consume(_SEMICOLON);
  132. end;
  133. { check if a symbol contains the hint directive, and if so gives out a hint
  134. if required.
  135. }
  136. procedure check_hints(const srsym: tsym);
  137. begin
  138. if not assigned(srsym) then
  139. exit;
  140. if sp_hint_deprecated in srsym.symoptions then
  141. Message1(sym_w_deprecated_symbol,srsym.realname);
  142. if sp_hint_platform in srsym.symoptions then
  143. Message1(sym_w_non_portable_symbol,srsym.realname);
  144. if sp_hint_unimplemented in srsym.symoptions then
  145. Message1(sym_w_non_implemented_symbol,srsym.realname);
  146. end;
  147. function consume_sym(var srsym:tsym;var srsymtable:tsymtable):boolean;
  148. begin
  149. { first check for identifier }
  150. if token<>_ID then
  151. begin
  152. consume(_ID);
  153. srsym:=generrorsym;
  154. srsymtable:=nil;
  155. consume_sym:=false;
  156. exit;
  157. end;
  158. searchsym(pattern,srsym,srsymtable);
  159. check_hints(srsym);
  160. if assigned(srsym) then
  161. begin
  162. if (srsym.typ=unitsym) then
  163. begin
  164. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  165. internalerror(200501154);
  166. { only allow unit.symbol access if the name was
  167. found in the current module }
  168. if srsym.owner.iscurrentunit then
  169. begin
  170. consume(_ID);
  171. consume(_POINT);
  172. srsymtable:=tunitsym(srsym).unitsymtable;
  173. srsym:=searchsymonlyin(srsymtable,pattern);
  174. end
  175. else
  176. srsym:=nil;
  177. end;
  178. end;
  179. { if nothing found give error and return errorsym }
  180. if srsym=nil then
  181. begin
  182. identifier_not_found(orgpattern);
  183. srsym:=generrorsym;
  184. srsymtable:=nil;
  185. end;
  186. consume(_ID);
  187. consume_sym:=assigned(srsym);
  188. end;
  189. function try_consume_hintdirective(var symopt:tsymoptions):boolean;
  190. begin
  191. try_consume_hintdirective:=false;
  192. if not(m_hintdirective in aktmodeswitches) then
  193. exit;
  194. repeat
  195. case idtoken of
  196. _LIBRARY :
  197. begin
  198. include(symopt,sp_hint_library);
  199. try_consume_hintdirective:=true;
  200. end;
  201. _DEPRECATED :
  202. begin
  203. include(symopt,sp_hint_deprecated);
  204. try_consume_hintdirective:=true;
  205. end;
  206. _PLATFORM :
  207. begin
  208. include(symopt,sp_hint_platform);
  209. try_consume_hintdirective:=true;
  210. end;
  211. _UNIMPLEMENTED :
  212. begin
  213. include(symopt,sp_hint_unimplemented);
  214. try_consume_hintdirective:=true;
  215. end;
  216. else
  217. break;
  218. end;
  219. consume(Token);
  220. until false;
  221. end;
  222. end.